Search code examples
pythonpython-3.xdiscorddiscord.py

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


Solution

  • 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