简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM