Make Your Own Python Discord Bot (Full Tutorial)

Johnathan Sewell
7 min readOct 1, 2021

Learn Python’s Diskord and build your first bot!

Discord servers are incredible hubs for building and personalizing a community. From a small friend group chat to share memes in, to large organizations, you can truly make anything on Discord. Which is why you might want to go that extra step in making a server something special: by making your OWN discord bot.

This gives you the freedom to customize features or make your own version of those locked behind a paywall in popular bots (*cought* Mee6 *cough*). It turns out, large groups aren’t the only ones that can create bots and provide quality service. It is suprisingly easy to do, and considering how much it can benefit your community, there is not reason not to get started right away!

Discord Developer Portal

The first step to creating a bot is to go to the Discord Developer Portal. Click “New Application” in the top right of the window, and choose a name.

Go to Settings > Bot on the left side of the screen, and press “Add Bot”. You can name your bot by typing in the username field, if you want to change it from the application name. Copy and keep the Token from this page, we will need it in a little. Do not share it with anyone; anyone that knows the token can control your bot.

Now go to Settings > OAuth2 and scroll down to “Scopes”. Select “bot” and in “Bot Permissions” select “Adminstrator”.

All you have to do now is visit the link provided, and invite the bot to your server. It is offline and won’t do anything, but don’t worry, we are about to change that!

Installing Python

Before you can get started building your bot, you need to install Python and the Diskord library. It is pretty simple to do, and if you already have Python on your computer feel free to skip ahead.

To install Python, simply go here and download the latest version for your operating system. Make sure you check “Add Python to PATH” during installation. Default values for everything else are ok.

Installing Diskord

Diskord is a Python wrapper for the Discord API that lets you make bots quickly and easily. It is a successor to the popular discord.py library, which was discontinued earlier this year (So now is the perfect time to start learning it!)

Once you have Python installed, simply open your command line and type “pip install diskord”. I am using Windows Command Prompt on my computer.

Once this is complete, type “pip freeze”. You should see something like this:

If you don’t get any errors, everything is working and you are ready to start coding!

Opening IDLE

IDLE is an example of an IDE (Integrated Learning Environment). It comes along with Python during installation and allows you to write complex code, instead of submitting everything through the command line. If you do not already know where it is on your computer, you can open it by searching “IDLE”.

Congratulations! You are ready to begin building your first Discord Bot!

Writing your first bot

To begin writing your first Python code, open IDLE and go to File > New File. This will open a blank file where we will begin writing code. You can save this file wherever you would like, just make sure you can find it later.

For this section, I will post an image of some code, and then explain what it does and why underneath it.

The first line of the file, “import diskord”, does exactly what it says - it imports the Diskord library you installed earlier and lets the rest of your code know it can be accessed.

The second line, “from diskord.ext import commands”, is a little more complicated but the same idea. Inside of diskord, there is a folder called “ext”. Inside of that folder, there is a folder called “commands”. So you are importing the commands folder.

The third line is where things start to get interesting. We are creating a bot (named simply, “bot”) by running the command commands.Bot(command_prefix=“!”). This will basically be the brain of your discord bot, and is how it will interact with your server and the people in it.

This new fourth line is what actually runs the bot. The green text here is the token you got from the Discord Developer Portal earlier, contained in quotation marks. This will always be the last line of your discord bot program, since it basically creates an endless loop.

Now we get into the heart of what you can do with diskord. “@bot.command()” is called a decorator. In this case, you can think of it as a prefix that tells the code that the next line will be a command for the discord bot (They are a lot more than that, and I would highly encourage you to learn more about decorators themselves)

The line “async def hello(ctx):” may look like a lot, but it is actually pretty simple. You are creating a method called “hello” that takes the argument “ctx”. “ctx” stands for “context”, and is the first argument in all bot commands to tell the method who sent the command, where it was sent, and other cool information.

“def” is simply the word used to tell python that you are creating a method, and “async” tells python that it is an asynchronous method (you don’t have to know what that means for now, just that it is something Diskord needs you to put).

Notice that the line “await ctx.send(“Nice to meet you!”)” is indented. This tells python that it is part of the hello method. “ctx.send()” tells the bot to send the given message (in this case, “Nice to meet you!”) to the channel where the command was run.

Go to Run > Run Module and run the code. Your bot is now live and can be interacted with on Discord!

As you can see, when I type the command “!hello” in discord, the bot responds! Try changing the name of the method, and the message the bot will send in response. The best way to learn is to just play around and see what happens.

Don’t be intimidated by this next bit.

“bot.event” is another type of method that your bot can respond to. Except the difference is events are things that happen in discord, not commands explicitly sent by someone in the server. The “on_message” event is triggered any time someone sends a message in your server, but there are many others.

The line “author = message.author” gets us the author of the message sent in discord. We then check with “if author.bot is False” to make sure that the person who sent the message is NOT a bot. Think about it, if we didn’t check if the message was sent by a person or a bot, then the bot could just keep responding to its own message!

Next we get the channel that the message was sent in, and have the bot send the message “Hi!” to that channel.

Now any time someone says something in your server, the bot will make sure to greet them. Perhaps this might be a little annoying after a while, so you always change or delete that feature.

Conclusion

And that’s it! You have now build your very first Discord Bot, ready to invite to your own servers. You can browse through the diskord documentation to continue to learn and add more features to your bots.

I have been building python discord bots for a few years and have loved learning to build bigger and more complex ones for my servers. If this is something other people find as interesting as I do, I will write another article going more in depth into the different features and things you can do with diskord (Hint: there is a lot), as well as hosting your bot from a third party (if you don’t want it running on your computer).

Also, this is my first time writing any sort of tutorial online, so whether this helped or not, any feedback would be greatly appreicated so I can learn how to better help people.

Thank you for reading if you still are, and have a great day.

--

--