DEV Community

Cover image for How To Handle Events In Discord.py
Raghav Mrituanjaya
Raghav Mrituanjaya

Posted on • Edited on

1 2

How To Handle Events In Discord.py

Hello, Guys today we are gonna see How to Handel Events in Discord.py

What is An event

An Event is like which takes place when a person leaves/enters a server or a person's role changes or Someone boosts a server which is a rare thing I am joking but anyways jokes apart so basically what and all happens in a server is basically a sort of event so I will be listing all the events available in Discord.py

New member joins

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.event
async def on_member_join(member):
print(f"{member} has joined the server")
@bot.command()
async def hello(ctx):
await ctx.send('Hello Friend')
bot.run(" Your Discord Bot Token")

Some person Leaves

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.event
async def on_member_remove(member):
print(f"{member} has left the server")
@bot.command()
async def hello(ctx):
await ctx.send('Hello Friend')
bot.run(" Your Discord Bot Token")

Conclusion

Thanks For Reading my post till the end

Do Support me on Patreon - https://www.patreon.com/gogamic

Top comments (1)

Collapse
 
sarmqn profile image
Sarmqn

Can you leave multiple @events on the same python file?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay