Discord is a large communication platform that has been immensely popular among gamers, but in recent years has expanded its reaches to anyone looking to create or get involved in a community of any size. It has a wide range of different features and is extensible with bots, which is partly why it’s so popular.

I’ve had experience making Discord bots before, and I am actually a verified bot developer (which just means I was among the first people to get a bot verified on the platform). Whilst I’m definitely a long way from being a top developer, I do have intermediate experience with making bots in discord.py, and I’ll be walking you through getting a bot setup and making your first few basic commands in this simple tutorial.

For this tutorial, you’ll require:

  • A Discord account
  • An IDE with Python support (I’ll be using VSCode)
  • Python installed on your device, with access to Pip

I would also recommend:

  • Basic Python knowledge
  • Moderate knowledge of how to use Discord

(If you have any problems, feel free to reach out to me through the comments and I’ll do my best to try and fix your issue)

💻 Creating the application

Before we can even start writing code, we need to create and setup an application. This is basically the user account that our bot will be using to login and interact with Discord.

The first thing you’ll need to do is head over to https://discord.com/developers/applications and login with your Discord account. After you’ve done that, you’ll want to create your app by pressing the New Application button to the top right of the dashboard.

Give your application a name (this can be different from the bot name) and then hit create. You can change this later. While you’re here, you should probably make a note of your bot’s application ID, as you’ll be needing this later.

After you’ve done that, you can go over to the Bot section of the dashboard.

Once there, you can press Add Bot and then confirm that you want to add a bot.

From the Bot tab, you’re free to set your bot’s username, profile picture, intents and more. You’re fine to leave this as it is and you can change it later.

📩 Adding the bot to your server

Now that you’ve created your bot, you’re ready to add it to your server. The first thing you’ll need is a server where you have the Manage Server permission. I would recommend using a personal or testing server for the purposes of following this tutorial.

You can now head over to https://discordapi.com/permissions.html and enter your application ID from earlier, as well as selecting the basic permissions such as reading and sending messages, plus anything else you may want in future. You should be presented with a screen that resembles this:

Next, you can use the link provided at the bottom of the page to add the bot to your server. Alternatively, you could replace APP_ID in the following link with your bot’s application ID and then use that to invite your bot.

https://discord.com/oauth2/authorize?client_id=APP_ID&scope=bot&permissions=3072

Once you’ve followed the link, logged in with Discord and then selected a server, you should be able to confirm and then return to Discord. If you go to your server you should see your bot offline in the server list.

Server McServerFace and Testy McTestFace have finally been reunited!

If you’re seeing a screen that resembles that, then you’re doing it right - congrats!

🌐 Making your bot login to Discord

Next thing you should do is open up a command line, on Windows you can do this with Windows + R and then cmd. Once in a command line you’ll want to type:

pip install discord.py

Once this has completed, you can open up your favourite IDE (in my case VSCode, bare in mind you’ll need the Python plugin if you want it to work properly) and then make a new file called main.py, in this file paste the following code:

import discord
from discord.ext import commands


prefix = "!"
bot = commands.Bot(prefix)


@bot.event
async def on_ready():
    print(f"We have logged in as {bot.user}")
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name =f"{bot.command_prefix}help"))

bot.run("Your bot token")

Bare in mind that you’ll need to replace Your bot token with your bot’s token. You can find that in the Bot tab of your application’s dashboard.

Warning: Your token is your bot’s password. Do not share it with anyone, ever. It will let them login to your bot.

If you navigate to the directory in which you saved that code, and type python main.py, or just press CTRL + F5 in VSCode, you should be able to run the bot and get this result:

Typing !help in chat should bring up this message too:

Okay, so it’s great we’ve got that working - but what does the code actually mean? Let’s break it down.

This snippet imports the modules we need to run our bot.

import discord
from discord.ext import commands

Next, we use this code to create an instance of the Bot class with a prefix property of !

You can change the prefix by changing the text within the ""

prefix = "!"
bot = commands.Bot(prefix)

Now, we need to do something a bit more complicated. We create an event decorator, and then an asynchronous function using a system event (on_ready triggers when the bot has been created). The function then prints to the console saying that we’ve logged in as {username of your bot} and changes the bot’s status to be playing your prefix + help.

You can modify this easily. Try changing playing to watching or listening for instance.

@bot.event
async def on_ready():
    print(f"We have logged in as {bot.user}")
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name =f"{bot.command_prefix}help"))

💬 Creating your first commands

So we’ve covered the code we’ve written to make our bot login and set it’s status, but I imagine you probably want to create at least one command before we finish here. So, let’s do it.

Paste this code just above the line where you run your bot:

@bot.command()
async def echo(ctx, arg):
    await ctx.send(arg)

Here, we tell the bot instance we’ve made that we want to register a command. Then when we define an async function we name the command echo (you can override the command name manually), and we collect two parameters from the command. The first is ctx, or context. This includes information about the user posting the message, the channel the message was posted in etc. The second is arg, this is the first word after the command (so if we run !echo Hello world, then the bot would take Hello from that). Finally, we send a message to the channel we got the message from initially, with the content of arg. If you run the command now, you should get this response:

Okay, let’s do one more command for today. This one will be a simple flip a coin command. Here’s the code for it:

@bot.command()
async def flip(ctx):
    randNum = random.randint(1,2)
    if randNum == 1:
        await ctx.send("Heads")
    else:
        await ctx.send("Tails")

For this to work, you’re also going to need to import the random module, so up near the top of your code, add this line:

import random

In this command, we use the command decorator to say we want to create a new command, and we call it flip in the function definition. This time we only take context, as we don’t need any arguments from the user.

Then we generate a random integer between 1 and 2. If the number we generate is 1, we send heads, if it is anything else (in this case it can only be 2), we send tails.

Here’s an example of me using this command

I hope you found this article helpful! Feel free to comment with your opinions, questions or issues and I’ll get back to you as soon as I can.