First Bot Command | Steem Discord Bot Python #4

in Steem Dev9 months ago

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.

lecture-4.png

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'

image.png

  • Now create another object commands, it will be an array of commands, and our first command will be !info.
 # bot commands
        self.commands = ['!info']

image.png

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()

image.png

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

image.png

  • 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]:

image.png

  • 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!')

image.png

  • 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.

image.png

Github
Steem Discord Bot Series
SteemPro Official

Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01
Cc: @pennsif
Cc: @future.witness
Cc: @stephenkendal
Cc: @justyy


Best Regards @faisalamin

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.032
BTC 61672.72
ETH 2996.85
USDT 1.00
SBD 3.78