Announcing SAGE for Discord
When we made the move to Discord, the everything was new to us. We started off with setting up a few bots - and this is how we wrapped our heads around the concept.
As an add on to all the rebranding moves, adapting a new communication channel was a part too.
We explored some of the unexplored tech and liked what we saw in Discord. Discord is a communication application with the ability to create channels, groups and direct messages.
This also led our backend team to endorse bots into it. As an AI engineer and the bot developer, this is what I thought and did,
When the backend team migrated to Discord, the very first thing I tried to do was to snoop around the scope of endorsing bots and code to trigger events.
We wanted to set timely remainders like checking-in to our work management tool Allt, logging in work, or Development related tasks like, ‘Perform unit testing’ or ‘Alert deadlines’ and the time is customized to any time zone.
Now moving on to the technical part.
So, to create a Discord bot, we chose our favorite language Python. The easy thing to start is with Discordpy, a wrapper for discord. You can find it’s documentation here.
- Download the Discord app.
- Sign up and join/create a server.
- Go to https://discordapp.com/developers/applications/me and create an application.
- Create a Bot User.
- Bot token is the way to initialize the Discord bot
This Discord app works on two core python libraries, discord and asynio.
First step is to install both the libraries.
pip install discord pip install asyncio
Next, we need to understand how Discord library works.
Create a client. client = discord.Client()
Run the client using the token client.run(TOKEN)
There are decorators that helps us simplify most of our work.
@client.event async def on_ready(): # Block to be executed when the discord bot is live # Like text a message to everyone in a channel channel = client.get_channel(CHANNEL_ID) message_to_send = '@everyone I am alive' client.send_message(channel, message_to_send)
The above code snippet helps you send message whenever the bot is alive.
Also there are lots of events like
We always have this funny bot in one of our channel,
This works with this small code snippet.
@client.event async def on_message(): # Block to be executed when the discord bot is live # Like text a message to everyone in a channel channel = client.get_channel(CHANNEL_ID) if channel == message.channel message_to_send = 'Yo Bro' client.send_message(channel, message_to_send)
What it does is, check whether the message comes from particular channel and send a Yo back. Simple and funny!
You can do a lot of things like this. But we end up creating too many bots in a single night. That’s a funny story! One bot we all loved is, reminder bot.
The simple lifecycle of our ReminderBot is,
Load the database of time schedules → Check whether current time is in time schedule → Execute the reminder if Yes
When a user adds a new reminder, load the database again and continue the workflow.
But to make the UX easier, we added commands like !remind, !list and !delete.
Check our Sage (reminder-bot) in GitHub. It’s
#OpenSourced. Feel free to contribute, fork and raise issues 😉