DEV Community

Mannu
Mannu

Posted on • Updated on • Originally published at github.com

#3.Events in Discord.py.

Hii Guys!
Welcome to this Discord.py tutorial #3.
Today we will talk about events in discord.py.

What are events in discord.py?

Events in discord.py are specific actions(or events) and when this specific action happens, our function is fired.

Types of Events:

There are several types of events:

  • on_ready(): Triggered when the bot has successfully connected to Discord and is ready to start processing events.

  • on_message(message): Fired when a message is sent in a channel that the bot can access.

  • on_message_edit(before, after): Raised when a message is edited in a channel.

  • on_message_delete(message): Called when a message is deleted in a channel.

  • on_member_join(member): Invoked when a member joins the server.

  • on_member_remove(member): Executed when a member leaves the server.

  • on_member_update(before, after): Triggered when a member's information (e.g., nickname, roles) is updated.

  • on_guild_join(guild): Fired when the bot joins a new guild (server).

  • on_guild_remove(guild): Raised when the bot is removed from a guild.

  • on_reaction_add(reaction, user): Called when a reaction is added to a message.

  • on_reaction_remove(reaction, user): Invoked when a reaction is removed from a message.

  • on_raw_message_delete(payload): Triggered when a message is deleted in a channel, including messages older than two weeks.

  • on_command_error(ctx,error): Triggered when there is error in command or there is no command with that name.

How to Use the Events:

Now its practical stuff to do. Let's see what exactly events do

on_ready event:

To create an event we need to add a decorator that is bot.event, Like this...

@bot.event
Enter fullscreen mode Exit fullscreen mode

and under this we will create our async def with the event name,

async def on_ready():
    # Logic here
Enter fullscreen mode Exit fullscreen mode

now what this event does is fire the above fuction when bot is ready to use ie we are successfully connected with our bot.
So let's create our logic in this def.

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user.name}")
Enter fullscreen mode Exit fullscreen mode

now this will print username of our bot when bot is ready to use.

on_message event:

This event is fired when any user send messages including our bot.

so let's create this event first

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    print(message.content)
Enter fullscreen mode Exit fullscreen mode
  • what are we doing here is defining a function as event, on_message, which is Invoked when someone send any message. here param message is the message that user sent.

  • in this function I awaited bot.process_commands(message). What it does is it checks if there is any command in the message or not, process the command and then run the next code.

  • then I am using print statement to print the content of message.So if I sent any message it will be printed in my terminal.

sending message

getting message content

That's it for today. I want all of you to try all the remaining events.Because when you will try it yourself then you will learn.


Github: https://github.com/MannuVilasara/discord.py-tutorial
Refrance : https://discordpy.readthedocs.io/
Discord: https://discord.gg/pvvJvZU6nS


Up Next:

  • Registering Slash(/) Commands.
  • error handling.

Top comments (1)

Collapse
 
madhavseth profile image
Madhav

❤️