在 discord.py 中手动触发事件

Wie*_*eRd 6 python python-3.x discord discord.py

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

Łuk*_*ski 6

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

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

自定义事件示例

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


@bot.event
async def on_custom_event(ctx):
    print("Custom event")
Run Code Online (Sandbox Code Playgroud)

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