In this tutorial we will develop our own Discord bot using Python.
The source code for this bot will be stored in my github repository
About the bot
First we will create a basic discord bot that will greet the message sender, and then we will create a Minecraft Bot, that will enable us to do the following:
1 2 3 4 5 |
|
Let’s get into it.
Dependencies
Create a python virtual environment and install the dependent packages:
1 2 3 4 |
|
Create the Discord Application
We first need to create the application on discord and retrieve a token that our python app will require.
Create a application on discord:
You should see:
Click “New Application” and provide it a name:
Once you create the application you will get a screen to upload a logo, provide a description and most importantly get your application id as well as your public key:
Then select the Bot section:
Then select “Add Bot”:
Select OAuth2 and select the “bot” scope:
At the bottom of the page it will provide you with a URL that looks something like:
1
|
|
Paste the link in your browser and authorize the bot to your server of choice:
Then click authorize, and you should see your bot appearing on Discord:
Developing the Discord Bot
Now we will be building our python discord bot, head back to the “Bot” section and select “Reset Token”, then copy and store the token value to a file .env
:
1
|
|
So in our current working directory, we should have a file .env
with the following content:
1 2 |
|
For this demonstration, I will create a private channel in discord called minecraft-test
and add the bot MinecraftBot
to the channel (this is only for testing, after testing you can add your bot to your other channels for other people to use):
For our first test, a basic bot, where we would like to type hello
and the bot should greet us by our username, in our mc_discord_bot.py
file we will have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Then run the bot:
1 2 |
|
And when we type hello
and goodbye
you can see our bot responds on those values:
Now that we tested our bot, we can clear the mc_discord_bot.py
and write our minecraft bot, the requirements of this bot is simple, but we would like the following:
- use the command
!mc
to trigger our bot and subcommands for what we want - able to see who is playing minecraft on our server at the moment
- able to get the status if the minecraft server is online
- able to get the server load percentage (as the bot runs on the minecraft server)
This is our complete mc_discord_bot.py
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
And now we can start our bot:
1
|
|
And we can run our help command:
1
|
|
Which will prompt our help message, and then test out the others:
Resources
Thank you to the following authors, which really helped me doing this:
- https://www.freecodecamp.org/news/create-a-discord-bot-with-python/
- https://betterprogramming.pub/coding-a-discord-bot-with-python-64da9d6cade7
- https://dev.to/codesphere/create-a-discord-bot-in-minutes-with-python-2jgp
Thank You
Thanks for reading, if you like my content, check out my website, read my newsletter or follow me at @ruanbekker on Twitter.
The source code for this bot will be stored in my github repository: - https://github.com/ruanbekker/discord-minecraft-python-bot
I’ve started a brand new Discord server, not much happening at the moment, but planning to share and distribute tech content and a place for like minded people to hang out. If that’s something you are interested in, feel free to join on this link