How to make a Discord bot

How to make a Discord bot

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.

How to make a Discord bot?

© Discord
  • Now, a new page will open. Select Applications from the upper left.

© Discord
  • After that, select New Application from the upper right corner.

© Discord
  • Choose a name and after that, select Create.

© Discord
  • You can now fill some General information.

© Discord
  • Now click on Bot.

© Discord
  • Choose Add bot.

© Discord
  • Confirm your choice by selecting Yes, do it!.

© Discord
  • 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.

© Discord
  • The next step is to copy your bot’s password: the token. Keep it to yourself and regenerate it if needed.

© Discord

How to 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.

© Discord
  • Next, scroll down and choose bot.

© Discord
  • Scroll down again and choose (wisely!) the Permissions. We recommend avoiding the Administrator permission.

© Discord
  • Scroll back up to the URL address and then copy it.

© Discord
  • 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.

© Discord
  • Scroll down and choose Authorize.

© Discord
  • Confirm that you are not a bot.

© Discord

How to code 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.

© Discord
  • Name it and choose create repl.

© Discord
  • Now type

    import discord

    and select Run.

© Discord
  • 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()
 
@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
 
@client.event
async def on_message(message):
    if message.author == client.user:
        return
 
    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')
 
client.run(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.

© Discord
  • Go back to Files. Your code should look like this (you can edit it manually):

© Discord
  • Select Run again and wait. The button will become active again and you will see the following message:

© Discord
  • It is now time to test your bot. Go to your Discord server and type the message you added in your code.

© Discord

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.

Around the same subject