In this lesson, you’ll clean up your code by using more functions from
discord.py, namely the utility functions. The first utility,
discord.utils.find(), can improve the simplicity and readability of this code by replacing the
for loop with an intuitive, abstracted function.
find() takes a function, called a predicate, which identifies some characteristic of the element in the iterable that you’re looking for.
The other utility you’ll practice with is
get(), which takes the iterable and some keyword arguments. The keyword arguments represent attributes of the elements in the iterable that must all be satisfied for
get() to return the element.
00:00 This is part seven, where you will begin exploring utility functions. Let’s start by taking another look at the example from the last video, which printed the name and identifier of the bot’s guild.
You could potentially clean up this code by using some of the utility functions from
discord.py. One of these utility functions is called
discord.utils.find(), which can improve the simplicity and readability of this code by replacing the
for loop with an intuitive abstracted function.
What happens here is
find() takes a function, called a predicate, which identifies some characteristic of the element in the iterable that you’re looking for. Here, you will use a particular type of anonymous function, called a lambda, as the predicate. In this case, you’re trying to find the guild with the same name as the one that is stored in the
DISCORD_GUILD environment variable. Once
find() locates an element within the iterable that satisfies the predicate, it will return the element.
This is essentially equivalent to the
break statement in the previous example, but quite a bit cleaner. If you’re unsure what a lambda function is, there will be a link below the video to a tutorial on the Real Python site about it. Let’s confirm that this change works.
And there we go, we get the same output as we did previously. Now,
discord.py has actually abstracted this concept that we just implemented one step further with the
get() utility. So, we can change this
find() function to
The keyword arguments represent attributes of the elements in the iterable that must all be satisfied for
get() to return the element. In this particular example, you have identified
name=guild, just here, as the attribute that must be satisfied. Now, if you take a brief look under the hood here and explore a technical detail, the
get() function, here, actually uses the
attrs keyword arguments to build a predicate, which it then uses to call
find(). In case you missed it, I do mean
attrs, like that. Don’t leave that there, because it will break your code.
Let’s now take a look at how to respond to events. You have already learned that
on_ready() is an event. You may have even noticed that it is identified as such in the code by the
What you have done here is, just like before, created a
client variable and called the
run() method with your Discord token. However, the actual
Client is different. Instead of using the normal base class,
client is an instance of
CustomClient, just here, which has an overridden
There is no real difference between the two implementation styles. However, moving forward in this tutorial course, the decorator version is the one that will be primarily used, as it looks quite similar to how you implement
Bot commands, which is something you will explore how to do in due time.
A quick technical detail that should be mentioned at this point is that regardless of how you implement your event handler, one thing must be consistent: all event handlers in
discord.py must be coroutines.
04:52 In case you are unfamiliar with the term, a coroutine is a specialized version of a Python generator function. There will be a link to a tutorial on it below the video. Before moving any further, we should double check that this version works, so if we run it, it’s connected to Discord. There you go.
05:10 We’ve got the bot name and it’s telling us it’s successfully connected. Very good! That’s what we wanted to see. This is the point where it is now time for you to start walking through examples of different event handlers that you can create.
Become a Member to join the conversation.