有没有办法手动触发 on_message
或 on_command_error
等事件?
类似于手动引发异常
答案 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