Create a Minecraft Discord bot!

Qoder
The Startup
Published in
4 min readJan 18, 2021

--

Note: This guide assumes you have some Python knowledge and that you have a Discord bot setup already and on an existing server of your own. There are plenty of guides on how to set one up already.

In this guide we will create a Minecraft bot named Stevey that will give us some control over our server.

Index:

  1. Configuring Our Minecraft Server settings.
  2. Creating the Discord Bot.
  3. Creating our Minecraft commands.

1. Configuring our Minecraft server settings:

Here we assume you already have a Minecraft server up and running and we will be showing you the required settings to allow our bot to communicate with the server.

On your server find and open your server.properties config file. This file contains the fields we need to edit and take note of to allow our bot to communicate with our server. Enabling some of these fields and assigning a password will allow us to use RCON and QUERY with our server.

query.port=25565 # find your port
enable-query=true # enable this to query your server
enable-status=true # enable this to get your server status
rcon.port=25575 # find your rcon port
rcon.password=<custom_password> # set your own password for RCON
enable-rcon=true # enable RCON
server-ip=127.0.0.1 # IP: Yours will be Unique

Once you have all your Ports, IP and enabled the required fields. Save your configurations and restart the server. Fill the following fields in your .env file for our bot to access our Minecraft server.

QUERY_PORT=<your_query_port>
RCON_PORT=<your_rcon_port>
RCON_PASSWORD=<your_server_rcon_password>
SERVER_IP=<your_server_ip>

Save your .env file and now let’s setup our Discord bot.

2. Creating the Discord bot:

We will be building a bot from scratch, but if you have an existing bot, you are more than welcome to use that file.

Run the following commands to get started:
- touch bot.py .env
- pip install discord.py python-dotenv mcron mcstatus

In your .env file add a new variable like follow:

...
DISCORD_BOT_TOKEN="<your_discord_bot_token>" # <= NEW

In your bot.py file paste the following code:

your bot.py file

What we did is create our own discord bot class and named it Stevey . Here we add the token variable to be accessed when start_bot() gets called and to make sure our bot connects successfully we added an event called on_ready() which will print to our terminal with our bot’s name and that it has successfully connected to Discord.

Not let’s test it out and you should see the following in your terminal:

$ python bot.py
>> Starting bot(Stevey)
>> Stevey has connected to Discord!

“There comes a point in your bot’s development when you want to organize a collection of commands, listeners, and some state into one class. Cogs allow you to do just that. The gist: Each cog is a Python class that subclasses commands.”

To keep all our commands, listeners and state in one class we need to create what is called a Cog. Each cog is a Python class that subclasses commands and is useful for keeping things organized as your bot has more commands. So the next step we need is to create a Cog for our bot to manage our Minecraft specific commands.

Run the following commands in your terminal to create the following directory and files

$ mkdir cogs
$ touch cogs/minecraft.py cogs/__init__.py

In our minecraft.py file paste the following:

In our bot.py edit the file to add our new Cog:

We now added a new command to our bot called test this will allow us to test that our bot can get our messages from discord and relay them back to us. Run the bot with python bot.py and go to your discord server where your bot has been invited to. Send a message to our bot in one of the channels with the following s:test "Hello Stevey!” and you should get a reply from the bot saying you send me: Hello Stevey! .

Now that we know our bot can receive commands and send them back let’s give it a Minecraft specific functionality.

3. Creating our Minecraft commands:

Now that we can communicate with our bot we want to be able to give our bot some useful Minecraft skills. We also want to limit our bot from being spammed so for now let’s make it so that only us the admins can run bot commands.

Let’s start by adding some permission check so only you can send your bot the msg command assuming you are admin on your server. Open minecraft.py and add the following to your file:

We edit our Minecraft class to add our ports, host and password fields. We also add a new status() method that allows us to query our server status using the mcstatus library. Response allows us to see number of Players that are online and the latency. Note how we added a permission check to only allow admins to use this command. This is optional.

Now let’s add a new command that gives us a messaging ability. Open minecraft.py and edit the file to add the following lines:

Now using the mcron library we take advantage of enabling the rcon feature on our server which allows us to send commands. With the msg() method we allow admin to send a message to everyone on the server or a specific Minecraft player that is online using built-in Minecraft commands.

Conclusions:

Now that you have a Minecraft Discord bot you can create more commands that allow you to manage and maintain your server. For example restarting the server from Discord and kicking players off that aren’t following rules :)

You can access the source code for this project by clicking here.

--

--

Qoder
The Startup

A Python developer wanting to share some personal project ideas.