在 discord.py 中手动触发事件

时间:2021-02-03 14:35:13

标签: python python-3.x discord discord.py

有没有办法手动触发 on_messageon_command_error 等事件?
类似于手动引发异常

1 个答案:

答案 0 :(得分:6)

是的,使用 Bot.dispatch 方法(这对于创建自定义事件很有用),注意您必须手动传递参数

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

自定义事件示例

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


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

没有关于它的文档,所以我不能给你链接,如果你想了解更多关于它的信息,请查看source code

相关问题