Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Connecting a Bot

In this lesson, you’ll use Bot, which is a subclass of Client that adds a little bit of extra functionality that is useful when you’re creating bot users. For example, a Bot can handle events and commands, invoke validation checks, and more. Before you can get into the features specific to Bot, you’ll convert bot.py to use a Bot instead of a Client.

You’ll also work with a new component, the Command. In general terms, a command is an order that a user gives to a bot so that it will do something. As you work with them, you’ll see how commands are different from events.

00:00 This is part 11, and here you will look at the subclass of Client called Bot. Firstly, what is Bot in this case? Well, previously you have learnt that a bot is an automated user that can react and respond like a human user. In this particular case, though, a Bot is a subclass of Client that adds a little bit of extra functionality that is useful when creating a bot user. For example, a Bot can handle events and commands, invoke validation checks, and more.

00:32 Before you get into the details of Bot, you need to convert bot.py in order to use a Bot instead of a Client.

00:40 As you can see, Bot can handle events much the same way that Client does. However, note two differences between Bot and Client. One, Bot it is imported from the discord.ext.commands module, and two, the Bot

00:57 initializer requires a command_prefix, which you will learn more about in the next section. The extensions library, ext, offers several interesting components to help you create a Discord Bot.

01:11 One such component is Command. What are commands? In general terms, a command is an order that our user gives to a Bot so that it will do something.

01:21 Commands are different from events because they are arbitrarily defined, directly called by the user, and flexible, in terms of their interface. In technical terms, a Command is an object that wraps a function that is invoked by a text command in Discord.

01:39 Now, let’s take a look at an old event to better understand what this looks like. If you look at one of the bot implementations from earlier in the course, you can see where you created an on_message() event handler, which receives the message string and compares it with a predefined-option, specifically '99!'. If you were to use a Command, you can convert this example to be more specific.

02:06 There are several important characteristics to understand about using Command. The first thing is that instead of using bot.event, like before, you use bot.command(), passing the invocation command name as its

02:21 argument. Secondly, the function will now only be called when !99 is mentioned in chat, which is the opposite to what it was earlier. This is different than the on_message() event, which was executed anytime a user sent a message, regardless of the content of that message.

02:40 Thirdly, the command must be prefixed with the exclamation point, because that’s the command_prefix that you defined in the initializer for your Bot, just here.

02:51 Finally, any Command function, technically called the callback, must accept at least one parameter, called ctx, which is the Context surrounding the invoked Command.

03:03 A Context holds data, such as the channel and guild that the user called the Command from. Now, run your program. With the bot running, you can head over to Discord to try out your new command.

03:19 From the user’s point of view, the practical difference is that the prefix helps formalize the command, rather than simply reacting to a particular on_message() event.

03:28 This comes with other great benefits, as well. For example, you can invoke the !help command to see all the commands that your Bot handles.

03:37 If you want to add a description to your command so that the help message is more informative, simply pass a help description to the .command() decorator, which would look something like this:

03:52 'Responds with a random quote from Brooklyn 99'.

04:02 Now when the user invokes the !help command, your bot will present a description of your command. Keep in mind that this functionality exists only in the Bot subclass, not the Client superclass. Command has another useful functionality: the ability to use a Converter to change the types of its arguments.

04:22 To find out more about this, you’ll have to tune into the next video.

Become a Member to join the conversation.