在* any *事件discord.py

时间:2020-07-02 19:41:05

标签: discord.py discord.py-rewrite

我想为每个事件运行特定的代码块。我认为,如果存在类似react-spring的东西,那可能是最容易的。但是,我似乎在文档或网络上找不到任何内容。有谁知道是否有办法做到这一点,如果有,怎么办?

其他信息:

  • discord.py-rewrite

谢谢。

1 个答案:

答案 0 :(得分:1)

我看到两种可能性:

discord.py中的大多数事件afaik是套接字响应的“处理程序”。 您可以尝试使用on_socket_response(message)事件。对于所有基于websocket的事件,这应该足够了。

如果您确实需要 any 事件,则可以尝试覆盖子类中的dispatch函数,并将该类用作您的bot的类。 例如:

from discord.ext import commands

class MyBot(commands.Bot):
    def dispatch(self, event_name, *args, **kwargs):
        super().dispatch("event", event_name)
        super().dispatch(event_name, *args, **kwargs)

bot = MyBot(command_prefix="!")

这将在任何事件上调度其他事件

@bot.event
async def on_event(event_name):
    print(f"{event_name} is dispatched")