#3.Events in Discord.py.
Tags: discord, python, beginners, tutorial
Published: 2023-07-08 01:07:55

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...
```python
@bot.event
```
and under this we will create our async def with the event name,
```python
async def on_ready():
    # Logic here
```
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.

```python
@bot.event
async def on_ready():
    print(f"Logged in as {bot.user.name}")
```
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
```python
@bot.event
async def on_message(message):
    await bot.process_commands(message)
    print(message.content)
```
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdsovwzu7ibh0i85cp8d.png)

![getting message content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pyckgszz3ziodsq0ujgm.png)

> 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.