【发布时间】:2021-05-07 19:28:24
【问题描述】:
有没有办法手动触发像on_message 或on_command_error 这样的事件?
类似于手动引发异常
【问题讨论】:
标签: python python-3.x discord discord.py
有没有办法手动触发像on_message 或on_command_error 这样的事件?
类似于手动引发异常
【问题讨论】:
标签: python python-3.x discord discord.py
是的,使用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
【讨论】: