Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
August 16, 2023

Discord Bot

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

What is a bot?

An Automated software application that is programmed to do tasks when triggered. They usually do repetitive tasks and they can complete them much faster than humans.

Different types of bots exist on the internet. These bots serve their purpose they are created for. They could be

  • Chatbots: Bots that simulate human conversations by responding to certain phrases with a programmed response.
  • Web crawlers: Bots that scan content on webpages all over the internet for any and all information they could get their hands on.
  • Social bots: Bots that operate on social media platforms mainly used for manipulating markets, capturing and spreading information.

What is Discord?

From Discord itself:

“Discord is a free voice, video, and text chat app that’s used by tens of millions of people ages 13+ to talk and hang out with their communities and friends.

People use Discord daily to talk about many things, ranging from art projects and family trips to homework. It’s a home for communities of any size, but it’s most widely used by small and active groups of people who talk regularly.”

In short, Players, streamers, and developers use Discord to discuss games, answer questions, chat while they play, and much more.

What would happen if you combine discord and bot?

One of discord’s main attractions is the inclusion of “bots” (In our case, chatbots) which respond based on “trigger” words given by the user (You ).

Bots can either reply to you with data (showing your favorite actor’s career history), reply to your message with a set message (Replying “General Kenobi” to your “Hello there” messages) or even moderate your “server” when things get ugly..

Bots are a great way to pass time in Discord on a deserted Sunday evening when all your friends are on a break. They can also be informative and educational when you interact with the right bot.4

How can I do this?

Setting up your discord account is required in order to create a bot and integrate it in your server.

  1. Go to discord.com, In the website, click on the “Login” at the top right.
  2. It shows you a login page where there’s an option to register a new account.
  3. Fill the form, verify your email address and create an account.

In the Discord sense, a bot needs an account too.

Time to create the bot account.

  • Now comes the toughest part of the job, Naming your bot.
  • It navigates you to your bot’s General information page where you can add a bot picture, description, tags etc. It also has important information like Application ID and Public Key.

In the side panel, move over to the Bot panel where you will “Add Bot” as a bot user. It asks for confirmation because this action can not be undone.

The token is always hidden. In order to view/copy your token, click on the “Reset Token” button, like it says, the token can only be viewed once. So make sure you copy the token right away, otherwise it will be gone forever and you’ll have to reset the token again.

Time to create a sandbox server to play with your bot. A server is a group of messaging channels where users can talk to each other about anything they like.

  • From your generally empty homepage, you can create a server by selecting the “+” icon on the top left hand side of the page.
  • You can either create a server (From scratch or with a template) or join a server that has sent you their invitation code. Let’s start a server from scratch.
  • Create a server for you and your bot by providing a name to the server (Not as hard as naming your bot)
  • Once it’s created, you’re the king of the jungle!

Wow, that looks easy! What can I do with it?

Well, anything actually, that’s the neat part, there are different bots and to give a small glimpse of its powers, there are bots that

  • Moderate servers with welcome message, profanity censoring, events organizer, roles manager
  • Entertain users with music, memes, messages, generating comics.
  • Provide information such as getting github commits of a branch, hooking to another channel that’s on a different server.

To start off, let’s make an 8 ball bot which gives you a reply whenever you ask a question to it.

Now that the Bot user has been created, now it’s time to invite them to your server.

  1. Navigate to the Oauth2 panel and click the URL generator option.
  2. Select “bot” checkbox. This opens a new section called “Bot permissions”.
  3. Select the required permissions, in our case, “Send Messages” in text permissions.
  4. If you notice at the bottom, there will be a URL generated based on the permissions you’ve selected.
  5. Copy and paste the URL into your browser and choose a server to invite the bot to. And click “Authorize”. In our case, it’s the server we just created.

The Bot is alive!!!

Now it’s time to get your hands dirty with some coding. Install the discord library from your language of choice. This example is built on Python 3.9.

This would install discord.py and its dependencies.
Dotenv package helps in retrieving data from “.env” file which is where you hold all your important secret information, in our case, the bot’s token.

  • Create a python file 8ball.py to have your bot’s brain power.Have the connection to Discord created with the help of “discord.py”. The basic snippet is provided below.

Create a new file called “.env” and save your DISCORD_TOKEN and DISCORD_SERVER in it

  • Once you’ve done this, execute your 8ball.py file and you’ll see a message like this
    Medium8BallBot#5387 has connected to Discord!
  • You can check out all the servers
    that this bot is running on using the below snippet inside on_ready() function.
  • for server_rec in client.guilds:
    print(f’{client.user} is connected to the following server: \n’ f’{server_rec.name}(id: {server_rec.id})’)
  • Everything that discord reads is being emitted to various streams of listeners, one such listener is our bot.To actually process something that’s being emitted is when we use @client.event decorator and its appropriate event function .
  • You can either continue using functions, and handling them, or overwrite the Client class’s handler methods. We’ll continue with decorators due to the size/complexity of this project.
  • To read messages and respond to them, on_message() event function is required to capture the discord emit when a message is sent in a channel that our bot has read access to.
  • Now with that in mind, we have several questions.
  • How do I read the question?
  • Reading the question is as simple as checking if the message that was transmitted has a question mark at the end.
  • How do I answer the question?
  • Using .send() method.
    await message.channel.send(response)
  • Once you’ve got the algorithm sorted, it’s time to visualize how the code would look.
  • Message is the parameter that is being sent by discord and captured by our on_message() event function. It would have information about the channel that the message was sent on, the message content, the author, among many other things.

The whole program would look like this if everything went smoothly.

“Well we’ve seen the code, how does it work though?”

All you need is to ask your question, (P.S, it’ll give you a favorable answer if you are courteous)

I did it! Now how do I show off this bot to others?

  1. Click the instant invite button next to a text channel
  2. Invite your friends or share them your server’s invitation link
  3. When a friend joins your server, they will show up

Conclusion

After a hard day’s work, it’s time to look back at your achievements.

To summarize, we have learnt

  • What a discord bot is.
  • How to create a bot user
  • How to create a server/guild and invite your friends.
  • What events are primarily needed for responding to a message.

There’s still a lot of information that has not been mentioned for the purpose of keeping this article to the point. That’s up to you and your curious mind to discover the potential of the bot you’re looking to create.

CodeStax.Ai
Profile
August 16, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles