【问题标题】:Manually trigger an event in discord.py在 discord.py 中手动触发事件
【发布时间】:2021-05-07 19:28:24
【问题描述】:

有没有办法手动触发像on_messageon_command_error 这样的事件?
类似于手动引发异常

【问题讨论】:

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


    【解决方案1】:

    是的,使用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

    【讨论】:

    • 哇,谢谢!为什么官方文档上没有?
    • 我也不知道可以创建一个突出显示某些行的 github 链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多