Responding to Events
In this lesson, you’ll modify your code to listen for and then respond to events. An event is something that happens on Discord that you can use to trigger a reaction in your code.
Continuing to build on the previous code you’ve been working with, the on_ready()
event handler handles the event that the Client
has made a connection to Discord and prepared its response data. So, when Discord fires an event, discord.py
will route the event data to the corresponding event handler on your connected Client
.
00:00 This is part eight, and this is where you will begin to look at examples of different event handlers that you can create. Let’s start with how to welcome a new member to the guild.
00:10 Previously, you saw the example of responding to the event where a member joins a guild. In that example, your bot user could send them a message, welcoming them to your Discord community.
00:21
Now, you will implement that behavior in your client using event handlers and verify its behavior in Discord. Just like before, you will handle the on_ready()
event by printing the bot user’s name in a formatted string, this line here. What is new, however, is the implementation of the on_member_join()
event handler.
00:43
As the name suggests, on_member_join()
handles the event of a new member joining the guild. In this example, you use member.create_dm()
to create a direct message channel.
00:55
Then, you use that channel to send()
a direct message to that new member, just there.
01:02
As a quick technical aside, take note of the await
keyword before member.create_dm()
and member.dm_channel.send()
, on these two lines here. await
suspends
01:15
the execution of the surrounding coroutine until the execution of each coroutine has finished. Now it’s time to test the behavior of your bot. Firstly, run the new version of bot.py
and wait for the on_ready()
event to fire, logging your message to stdout
. So let’s do that now. You can see it’s running.
01:39 It’s now connected to Discord. Next up, head over to Discord, log in, and navigate to your guild by selecting it from the left-hand side of the screen.
01:49 Select Invite People just beside the guild list where you selected the guild, just here. Check the box that says Set this link to never expire and copy the link.
02:01 Now, with the invite link copied, create a new account and join the guild using your invite link.
02:08
Firstly, you’ll see that Discord introduced you to the guild by default with an automated message. If I click here, you can see RealPythonTutorialBot
has come up! It says, “Hi ST3V3N5, welcome to my Discord server!” That’s the default automated message that you set up earlier.
02:29
More importantly though, you would have noticed the badge on the left side of the screen, which informs you that you have a new unread message. When you selected it, you would have seen this message come up, because this is the DM channel with the RealPythonTutorialBot
that was set up. Once you selected it, you would have seen the new private message from your bot user, which is what you can see here. That is perfect.
02:54 That’s exactly what you want to see. Your bot user is now interacting with other users with pretty minimal code. In the next video, you will learn how to respond to specific messages within the chat.
blank on Jan. 30, 2022
Thanks a bunch Jeff this fixed my issue with the DM not sending.
Become a Member to join the conversation.
Jeff on Dec. 21, 2020
If you’re not receiving a DM from your bot, and you are using discord.py v1.5.0 or above, see the docs for Gateway Intents.
You’ll need to add: