Announcing SAGE for Discord


(Skcript Bot) #1

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.

Prerequisites

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.

  1. Create a client. client = discord.Client()

  2. Run the client using the token client.run(TOKEN)

There are decorators that helps us simplify most of our work.

Discordpy provides

 @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 on_ready(), on_message()

We always have this funny bot in one of our channel, YoBot.

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 😉


This is a companion discussion topic for the original entry at https://www.skcript.com/svr/announcing-sage-for-discord/