<i id='wHXld'><tr id='wHXld'><dt id='wHXld'><q id='wHXld'><span id='wHXld'><b id='wHXld'><form id='wHXld'><ins id='wHXld'></ins><ul id='wHXld'></ul><sub id='wHXld'></sub></form><legend id='wHXld'></legend><bdo id='wHXld'><pre id='wHXld'><center id='wHXld'></center></pre></bdo></b><th id='wHXld'></th></span></q></dt></tr></i><div id='wHXld'><tfoot id='wHXld'></tfoot><dl id='wHXld'><fieldset id='wHXld'></fieldset></dl></div>
    <tfoot id='wHXld'></tfoot>

      • <bdo id='wHXld'></bdo><ul id='wHXld'></ul>

      <small id='wHXld'></small><noframes id='wHXld'>

      <legend id='wHXld'><style id='wHXld'><dir id='wHXld'><q id='wHXld'></q></dir></style></legend>
      1. 为什么多个 on_message 事件不起作用?

        时间:2023-09-11
        <legend id='zbzIK'><style id='zbzIK'><dir id='zbzIK'><q id='zbzIK'></q></dir></style></legend>
            <tbody id='zbzIK'></tbody>
          <i id='zbzIK'><tr id='zbzIK'><dt id='zbzIK'><q id='zbzIK'><span id='zbzIK'><b id='zbzIK'><form id='zbzIK'><ins id='zbzIK'></ins><ul id='zbzIK'></ul><sub id='zbzIK'></sub></form><legend id='zbzIK'></legend><bdo id='zbzIK'><pre id='zbzIK'><center id='zbzIK'></center></pre></bdo></b><th id='zbzIK'></th></span></q></dt></tr></i><div id='zbzIK'><tfoot id='zbzIK'></tfoot><dl id='zbzIK'><fieldset id='zbzIK'></fieldset></dl></div>

          <tfoot id='zbzIK'></tfoot>
            <bdo id='zbzIK'></bdo><ul id='zbzIK'></ul>

            <small id='zbzIK'></small><noframes id='zbzIK'>

                  本文介绍了为什么多个 on_message 事件不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  为什么我不能有多个 on_message 事件?

                  Why can't I have multiple on_message events?

                  import discord
                  
                  client = discord.Client()
                  
                  @client.event
                  async def on_ready():
                      print('in on_ready')
                  
                  @client.event
                  async def on_message(message):
                      print("in on_message #1")
                  
                  @client.event
                  async def on_message(message):
                      print("in on_message #2")
                  
                  @client.event
                  async def on_message(message):
                      print("in on_message #3")
                  
                  client.run("TOKEN")
                  

                  例如,如果我输入了任何不和谐的内容,它总是只有最后一个 on_message 被触发.我怎样才能让这三个都工作?

                  For example, if I typed anything in discord, it's always only the last on_message that gets triggered. How can I get all three to work?

                  推荐答案

                  原生Client

                  是不行的

                  你只能有一个 on_message,如果你有多个,on_message事件只会调用最后一个.你只需要结合你的三个 on_message.

                  It's not possible with the native Client

                  You can only have one on_message, if you have multiple, only the last one will be called for the on_message event. You'll just need to combine your three on_message.

                  import discord
                  
                  client = discord.Client()
                  
                  @client.event
                  async def on_message(message):
                      print("in on_message #1")
                      print("in on_message #2")
                      print("in on_message #3")
                  
                  client.run("TOKEN")
                  

                  与任何 Python 变量/函数一样(除非装饰器存储您的函数,@client.event 仅保留最近的回调),如果多个名称相同,则最近的将被保留,所有其他的都会被覆盖.

                  Like any Python variable/function (unless the decorator stores your function, @client.event does it by keeping only the most recent callback), if multiple names are the same, the most recently will be kept, and all others will get overwritten.

                  这是我编写的一个简单示例,旨在让您广泛了解 discord.py 中的事件如何工作(注意:实际代码与此不完全相同,因为它已被重写并显着减少).

                  This is a simple example I wrote to give you a broad understanding of how events in discord.py work (note: the actual code isn't exactly like this, as it's rewritten and significantly reduced).

                  class Client:
                      def event(self, func):               
                          if func.__name__ == "on_message":
                              self.on_message_handle = func
                              return func
                  
                      def receive_message(self, msg):
                          func = getattr(self, "on_message_handle", None)
                          if func is not None:
                              func(msg)
                          else:
                              self.process_commands(msg)
                  
                  client = Client()
                  
                  @client.event
                  def on_message(msg):
                      print("in on_message #1")
                  
                  @client.event
                  def on_message(msg):
                      print("in on_message #2")
                  
                  client.receive_message("hello")
                  # "in on_message #2"
                  

                  如您所见,client.event 只保留一个 on_message 实例.

                  As you can see client.event only keep one instance of on_message.

                  或者,如果您使用 discord.py 的 ext.commands 扩展,则可以通过本机方式获得多个 on_message 回调.您可以通过将它们定义为 listener 来实现.您最多可以有一个 on_message 事件和无限数量的 on_message 侦听器.

                  Alternatively, if you're using the ext.commands extension of discord.py, there is a native way to have multiple on_message callbacks. You do so by using defining them as a listener. You can have at most one on_message event, and infinite amounts of on_message listeners.

                  from discord.ext import commands
                  
                  bot = commands.Bot('.')
                  
                  @bot.event
                  async def on_message(msg):
                      print("in on_message #1")
                      await bot.process_commands(msg)  # so `Command` instances will still get called
                  
                  
                  @bot.listen()
                  async def on_message(msg):
                      print("in on_message #2")
                  
                  
                  @bot.listen()
                  async def on_message(msg):
                      print("in on_message #3")
                  
                  bot.run("TOKEN")
                  

                  收到消息后,所有on_message #1-3都会被打印出来.

                  When a message is received, all on_message #1-3 will all get printed.

                  这篇关于为什么多个 on_message 事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何让 discord.py 意图发挥作用? 下一篇:如何在 discord.py 中提及用户?

                  相关文章

                  最新文章

                    <bdo id='GcraO'></bdo><ul id='GcraO'></ul>

                  <small id='GcraO'></small><noframes id='GcraO'>

                  1. <i id='GcraO'><tr id='GcraO'><dt id='GcraO'><q id='GcraO'><span id='GcraO'><b id='GcraO'><form id='GcraO'><ins id='GcraO'></ins><ul id='GcraO'></ul><sub id='GcraO'></sub></form><legend id='GcraO'></legend><bdo id='GcraO'><pre id='GcraO'><center id='GcraO'></center></pre></bdo></b><th id='GcraO'></th></span></q></dt></tr></i><div id='GcraO'><tfoot id='GcraO'></tfoot><dl id='GcraO'><fieldset id='GcraO'></fieldset></dl></div>
                  2. <tfoot id='GcraO'></tfoot>

                      <legend id='GcraO'><style id='GcraO'><dir id='GcraO'><q id='GcraO'></q></dir></style></legend>