Latest update on May 27, 2021 at 12:18 PM by Elena Keracheva .
Discord has become the one of the most famous and preferred platforms for live gaming. However, you don’t have to be a gamer to create a server and enjoy all of Discord’s cool features. One of these features is the option to create a bot and to add it to your personal server(s). This may sound complicated but if you follow the steps in the present article, you will be able to code your first bot message, so keep reading and make the most out of the platform!
Before you start
There are a few steps to follow and a few things to know before creating your bot.
Firstly, if this hasn’t been already done, you have to download and install Discord from the link above. This way, you will be able to create your account.
You will also need to create a server beforehand. If you don’t know how to do so, we recommend referring to our dedicated article.
Creating a bot requires coding. In the present tutorial, we use a programming language called Python.
Make a Discord bot
Next, go to Discord’s official website and scroll down to Developers. Select this option.
Now, a new page will open. Select Applications from the upper left.
After that, select New Application from the upper right corner.
Choose a name and after that, select Create.
You can now fill some General information.
Now click on Bot.
Choose Add bot.
Confirm your choice by selecting Yes, do it!.
Your bot has now been created. You can still manage the avatar if you want. We recommend keeping the default settings, that is keeping the Public bot box active while the Requires OAUTH2 code grant is inactive.
The next step is to copy your bot’s password: the token. Keep it to yourself and regenerate it if needed.
Add a Discord bot to a server
Permissions
Now that your bot is created, you have to join it to your servers via an invitation.
Select OAuth2.
Next, scroll down and choose bot.
Scroll down again and choose (wisely!) the Permissions. We recommend avoiding the Administrator permission.
Scroll back up to the URL address and then copy it.
Next, open a new tab and paste the URL. A new page will open. Choose the server you want to add your bot to and confirm by selecting Continue.
Scroll down and choose Authorize.
Confirm that you are not a bot.
Coding your bot with Python
The next step is to write a basic code for your bot.
Go to the Replit website and choose Start coding. You will need to log in - you can use your Google account for that. Next, choose Python from the list.
Name it and choose create repl.
Now type import discord and select Run.
Wait till the installation process is done (the Stop button will turn into a Run button again). If you want your bot to reply automatically to a message, this means that you have to create an event. Now, on main.py copy and paste the following code (right under import discord):
import os client = discord.Client() @clienent async def on_ready(): print('We have logged in as {0.userormat(client)) @clienent async def on_message(message): if message.author == client.user: return if messageontent.startswith('$hello'): await messagehannel.send('Hello!') clienun(os.getenv('TOKEN'))
Here “$hello” is how the user’s message will start. When the bot reads it, it will reply with “Hello!”. Feel free to experiment with other messages.
You will now have to create an environment variable. This is no longer possible to do by adding a file. Instead, go to the locker icon. In the key box type TOKEN. In the value box paste the Bot token you copied earlier. After that, click Add new secret. Next, click on the following boxes: Insert, Token, Insert.
Go back to Files. Your code should look like this (you can edit it manually):
Select Run again and wait. The button will become active again and you will see the following message:
It is now time to test your bot. Go to your Discord server and type the message you added in your code.
You have now coded your first bot-message!
Final notes
When you turn off your PC or you stop the testing process, the bot will go offline. In order to keep it online, you have to keep the script hosted at all times. A possible solution for that would be remote hosting services. For example, there is Heroku that can be used for free. You can find plenty of Internet tutorials to see how to host your Discord bot on Heroku.
Leave a Comment