Install latest from the GitHub repository:
$ pip install git+https://github.com/AnswerDotAI/cordslite.gitor from pypi
$ pip install cordsliteGetting a bot token:
- Go to the Discord Developer Portal.
- Create an application, then add a bot under “Bot”.
- Copy the token and set it as an environment variable:
export DISCORD_BOT_TOKEN='your_token_here'- Under “OAuth2”, generate an invite URL with the
botscope and the permissions you need, and open it to invite the bot to your server.
Then create the client:
from cordslite import *dc = DiscordClient()Fetch a guild (server) and explore its channels:
gid = '1493461895615873044'
gld = await dc.guild(gid)
gldGuild(id=1493461895615873044, name='AAI-test-dev')chs = await gld.channels()
chs| ID | Name | Type |
|---|---|---|
| 1493461896139903026 | Text Channels | 4 |
| 1493461896139903027 | Voice Channels | 4 |
| 1493461896139903028 | general | 0 |
| 1493461896139903029 | General | 2 |
Fetch recent messages from a channel and send one:
ch = chs[2]
msgs = await ch.messages(5)
msgs| ID | Author | Content | Date |
|---|---|---|---|
| 1545126723224600577 | share2self | Watch this ma! | 2026-09-03 |
| 1545127267020054540 | share2self | Hello from cordslite! 🍺 | 2026-09-03 |
| 1545127273374552074 | share2self | Watch this ma! | 2026-09-03 |
| 1545127676359082054 | share2self | Hello from cordslite! 🍺 | 2026-09-03 |
| 1545127678238003282 | share2self | Watch this ma! | 2026-09-03 |
msg = await ch.send('Hello from cordslite! 🍺')
msgMessage(id=1545128287922159727, author='share2self', content='Hello from cordslite! 🍺')Connect, then register handlers for events such as MESSAGE_CREATE:
intents = Intent.GUILDS | Intent.GUILD_VOICE_STATES | Intent.GUILD_MESSAGES | Intent.MESSAGE_CONTENT
gc = GatewayClient(intents, dc)
await gc.start()async def on_msg(msg): print(f"{msg.author['username']}: {msg.content}")
gc.on('MESSAGE_CREATE', on_msg)msg = await ch.send('Watch this ma!')
msgMessage(id=1545128289734107176, author='share2self', content='Watch this ma!')await gc.stop()Bot ties REST and Gateway together with a decorator-based command router. The function name becomes the command name, prefixed with ! in Discord:
bot = Bot(intents)
await bot.start()@bot.cmd
async def echo(msg, args): await (await msg.channel).send(f'You said: {args}')
botBot(cmds=['echo'])
The router catches errors in command handlers and stores them in bot.errors. You can also register a handler that runs as they happen:
@bot.on_error
async def handle_err(msg, e): print(f'Error: {e}')import asyncioJoin a voice channel, record audio, and leave:
vch = next(c for c in await gld.channels() if c.type == 2)
vc = await bot.join_voice(vch)
await asyncio.wait_for(vc.sess.wait(), 15)
vc.start_recording(path='/tmp/recording.mp3')voice json 15 {'any': 100}
'/tmp/recording.mp3'
await asyncio.sleep(5)
res = await vc.stop_recording()
await bot.leave_voice()
await bot.stop()
res{'speakers': {}, 'mixed': None}
from IPython.display import AudioAudio(res['mixed'])