Welcome back, developers.
Greetings to all Steem enthusiasts! Today we are going create our very first bot command we will also create a separate file to keep all the settings of the Bot. Let's dive into it.

Summary |
---|
We established the necessary environment for developing a Python-based Discord bot during our first lecture. In our second lecture we create a discord bot token, configure the bot and generate the invite link. We use the invite link and added the bot to our server. In our third lecture, we will bring the Community-Bot online in our local environment. Our objective for today is to create our initial bot command.
Procedure |
---|
- First, we need to create a new file
utils.py
in our project directory and We create a Utils class and initialize it. We will use it to set our bot settings so that we can use it in our project and can make changes easily in the future. We will create an object **botToken ** and put our bot token here as shown here.
class Utils:
def __init__(self):
self.botToken = 'BoT_TOKEB _HERE'
- Now create another object commands, it will be an array of commands, and our first command will be !info.
# bot commands
self.commands = ['!info']
Now open the main.py file and import this Utils file and add the **botToken **from it as shown here.
from utils import Utils
# creating utils instance
utils = Utils()
In the on_message
event, we will define our first command. However, before that, we need to verify if the message is from a real user or our bot. If the message is from the bot, we will not return anything and the program will not execute further.
if message.author == client.user:
return
- Now we will create an object props and get the message content in it. We will split the message content by spaces and get the first word in an object commands. This will the command that is sent by the end user,
Next, we need to validate the command and check if the command is !info or not as shown here.
props = message.content.split(maxsplit=6)
command = props[0]
# command validation
if command in utils.commands:
# !info command
if command == utils.commands[0]:
- Now we can send a message in the server when a user types !info at the start of its message. We will use await to send the message otherwise we will get an error.
# !info command
if command == utils.commands[0]:
await message.channel.send('Hello I am Robo!')
- Let's run our project and test the command. Here you can see we successfully implement our very first bot command. In the next lecture, we will learn how to create functions to fetch data over the internet and later show it via bot message. Stay tuned.
Github |
---|
Steem Discord Bot Series |
---|
Lecture #1: Develop a Discord bot for your Steem Community
Lecture #2: How to Add a Bot to Your Discord Server
Lecture #3: Bring the Steem Discord Bot Online | Steem Discord Bot Python
SteemPro Official |
---|
Download SteemPro Mobile
https://play.google.com/store/apps/details?id=com.steempro.mobileVisit here.
https://www.steempro.comSteemPro Discord
Official Discord Server
Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01
Cc: @pennsif
Cc: @future.witness
Cc: @stephenkendal
Cc: @justyy
Best Regards @faisalamin