Creating First Command
Last updated
Last updated
You will be creating the code that will create your first Discord bot command. A command is something you enter into your Discord server and the bot will perform a certain task.
It's like talking to the bot, and the bot will respond with an action or text depending on what you told the bot.
For this section of the sushi card we will be making a "!hello" command that will make the bot respond with "Hello, {user}" with {user} being replaced with the username of the person who sent the command.
The command template we will be following:
The first line @client.command(pass_context=True)
is similar to how we used @client.event
. The only difference is that this only triggers when a command is used. How does the code know if a command is used? Well, we specified the prefix for the command in the previous section, so any message following that prefix will be counted as a command. Following that in parenthesis, we set another "setting". In this case, we are telling the event to give us context on the message. This is the actual message that is sent and can help us figure out who sent the message, the channel it was sent in, etc.
The second line asnc def command_name(message):
is used to specify the specific command. The phrase command_name is where you would put the command and it can only be one word long. No spaces. Following that in parenthesis, we define how we reference the context variable . You can name this whatever you want. In normal practice, either message or ctx is used to reference the context. Either way this will allow us to reference the context when making a response to the command.
The third line await message.channel.send(response)
is used to send a response following the command. Remember how we defined the variable message as the context? Well, we can now find the channel the message was sent in by using message.channel
and then using the send()
function, we can send a message to that channel. In the parentheses we can input what we want to send. Usually this will be text, which is known as a string.
Now we can fill out that template to make our hello command. We can replace command_name
with hello because that it will be our command. That means our command will be triggered by typing !hello
in Discord.
Now we are going to enter our response. We want the Discord bot to say "Hello name". This is a bit trickier but nothing we can't handle. Like we got the channel that the message was sent in before, we can actually get the author of the message, aka the person who sent the message. This is done by using message.author
. The thing is, we want to get the name of the author, not the actual author.
Think of it like this, when using message.author
, you are referencing the actual person. It would be like me pointing to someone and saying they sent the message. It's not very helpful in this case. We want to find the name of the person. We can do this by using message.author.name
.
Now we can replace response with "Hello message.author.name"
. Wait a minute, this doesn't work. It just sends the actual text "Hello message.author.name"
. We want it to replace message.author.name
with the name of the person who sent the message. To do this we can create something called an F string. This allows us to put nonliteral text into our string. Basically that means we can get information put into our string. If you add an f before the opening quote, you create an F string. Now we can reference nonliteral text by surrounding it in braces {}. When text is surrounded by braces, Python knows that you don't want to literally put message.author.name
in the output but instead find the name of the author and put it there instead.
The completed code:
Only select the Final Code tab once you have completed your hello command. If you are stuck please see a mentor.
Your code should look something like the following at this point in the sushi card:
Only select the Final Code tab once you have completed this section. If you are stuck please see a mentor.
If your code is not like this or is not working, please either read through the sushi card again OR come talk to one of the mentors who will guide you through the issue.