DEV Community

Events in discord.py 🐍

terabyte. on March 05, 2021

discord.py has an extensive collection of features. Events are one of the most useful of these. Events are used for welcoming bots, reaction roles,...
Collapse
 
vertigoblip profile image
vertigo-blip

hey, very helpful guide. I was wondering how to trigger an event only if a certain user triggers it? For example, if I wanted to kick a certain user that said 'hello', but not have that kick apply to anyone else who said 'hello', how would I do that?

Collapse
 
mikeywastaken profile image
terabyte.

Events are always sent through to the bot, no matter who it's from. However, you can filter them using an if statement. For example, this snippet would work:

@bot.event
async def on_message(message):
    if message.content.lower() == "hello" and message.author.id == person_you_dont_like_id:
        await message.author.kick(reason="your reason goes here")
Enter fullscreen mode Exit fullscreen mode

You'll want to make sure you use .lower() on the text, because if you don't, they can send hEllo or just change the case of other letters to dodge your system.

Collapse
 
alexanderg profile image
Alexander G

Hi friend! very good post, and how I see you are also pythonist xD, I really like Python hehe, I have a bot in Discord.py, I do not know much every day I'm getting new things,For now Search by: YouTube, Google, Wikipedia, among other things :slight_smile: I plan to write more post and translate them so that everyone can read, to my bot also translates texts

Collapse
 
mikeywastaken profile image
terabyte.

That's actually really cool! So you're making a translation bot?

Collapse
 
alexanderg profile image
Alexander G

Hello, good exactly no, I mean my bot is of general purpose, translates and make searches also that if interacting with users and so, and good at discord.py because I really like python :p dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
mikeywastaken profile image
terabyte.

Well your bot looks really nice!

Collapse
 
ffbboy30 profile image
ffbboy30

HI,
Thanks for all this explanation.
I need to make a py script that only receive message from a server.
The server is not mine, how can I register to this server ?
Do you have tips to help me.
Thanks

Collapse
 
mikeywastaken profile image
terabyte. • Edited

You can see a guide created by the discord.py creator here to add the bot to the server, which is what you want to do. Make sure that you have the Manage Server permission.

Collapse
 
ffbboy30 profile image
ffbboy30

I haven't this permission , the channel is not mine that why I'm looking for another way

Thread Thread
 
mikeywastaken profile image
terabyte.

You'll need to ask a channel admin to add the bot.

Collapse
 
hemantshukla01 profile image
hemantshukla01

hey can any can help how i can add backend trigger url in my code
@client.command(aliases=['AVN'])
async def AVN(ctx):
await ctx.send(f'@everyone Please all join AVN immediately!!!')