【问题标题】:How do I make the bot respond when someone mentions it? discord.py当有人提到它时,我如何让机器人做出回应?不和谐.py
【发布时间】:2022-02-23 02:36:08
【问题描述】:

这是我尝试过的代码:

@client.event
async def on_message(message):
    if client.user.mention in message.content.split():
        await client.say("You can type `!vx help` for more info.")

但它似乎不起作用。

【问题讨论】:

  • 您能否详细说明它对您不起作用的原因?你有错误吗?你有没有学习任何教程?

标签: discord.py discord.py-rewrite


【解决方案1】:

当使用命令装饰器时,你可以这样做:

from discord.ext import commands # necessary for this task

client = commands.Bot(command_prefix=commands.when_mentioned_or("!"))

或者使用on_message() 事件,这是您可以检查提及的众多方法之一:

@client.event
async def on_message(message):
    if client.user.mentioned_in(message):
        await message.channel.send("You can type `!vx help` for more info")

另外,我注意到您向频道发送消息的方法不太正确。

在 d.py rewrite (v1.x) 中,您有一个 abc.Messageable 对象,顾名思义,它类似于服务器的文本频道、DM 或群聊。

这个对象有一个叫做send()的方法,它允许你发送内容。您会发现这种情况的一些常见情况是; ctx.send() 当你使用命令装饰器时 - 它们有 Context 作为第一个参数 - 和 message.channel.send() 当你像你一样使用 on_message() 事件时。它也会出现在其他一些地方,但这些将是最常见的。

你对它是一个协程有正确的想法,因此需要await它。在文档中,它会说明某些东西是否是协程。


参考资料:

【讨论】:

    【解决方案2】:
     @client.event async def on_message(message):
         if client.user.mention in message.content.split():
             await client.say("You can type `!vx help` for more info.")
    

    而不是 content.split() 使用内容: 而不是 client.say 使用 message.channel.send() 所以我们会得到

     @client.event
     async def on_message(message):
         if client.user.mention in message.content:
             await message.channel.send("You can type `!vx help` for more info.")
    

    【讨论】:

      【解决方案3】:

      这是一个使用类的类似解决方案。我遇到了确切情况的问题并最终设法解决了它,但找不到任何这样的例子。关键是if self.user in message.mentions: 行。

      message.mentions 指的是用户列表,因此您可以检查自己或其他人。

      class MyClient(discord.Client):
          async def on_ready(self):
              print('Logged on as', self.user)
      
          async def on_message(self, message):
              # Don't respond to ourselves
              if message.author == self.user:
                  return
      
              # If bot is mentioned, reply with a message
              if self.user in message.mentions:
                  await message.channel.send("You can type `!vx help` for more info.")
                  return
      
      def main():
          client = MyClient()
          client.run(DISCORD_TOKEN)
      
      if __name__ == "__main__":
          main()
      

      【讨论】:

        【解决方案4】:
        @bot.event#ping reply
        async def on_message(message):
          if message.author.bot == False and bot.user.mentioned_in(message) and len(message.content) == len(bot.user.mention)+1:
            await message.channel.send(f'Hello! I am the {bot.user.mention}!\nMy Prefix is $')
          else:
            await bot.process_commands(message)
        

        方法很简单,在bot.event中bot会读取每条消息,检查以下内容

        1. ping 不是来自其他机器人,而只是来自用户
        2. 消息中提到了该机器人
        3. 只提到了机器人,它避免了在 convo 中意外提及/ping。 +1 的长度是换行符。

        bot.process_commands(message) 在那里,如果不是 ping,那么机器人可以进一步处理消息/命令。

        【讨论】:

          猜你喜欢
          • 2021-07-07
          • 2020-12-30
          • 2021-03-03
          • 1970-01-01
          • 1970-01-01
          • 2020-09-03
          • 1970-01-01
          • 2020-12-18
          • 1970-01-01
          相关资源
          最近更新 更多