Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually trigger an event in discord.py

Is there a way to manually trigger events like on_message or on_command_error?
Similar to manually raising a Exception

like image 924
WieeRd Avatar asked Feb 03 '21 14:02

WieeRd


1 Answers

Yes there is, use the Bot.dispatch method (this is useful for creating custom events), note that you have to pass the arguments manually

bot.dispatch("message", message) # You need to pass an instance of `discord.Message`
bot.dispatch("command_error", ctx, error) # Remember to pass all the arguments

Example of a custom event

@bot.command()
async def dispatch_custom(ctx):
    bot.dispatch("custom_event", ctx)


@bot.event
async def on_custom_event(ctx):
    print("Custom event")

There's no docs about it so I can't give you the link, if you want to know more about it take a look at the source code

like image 125
Łukasz Kwieciński Avatar answered Oct 27 '22 00:10

Łukasz Kwieciński