【问题标题】:Is there a way to directly take the content of a user's message in Discord.py?有没有办法直接在 Discord.py 中获取用户消息的内容?
【发布时间】:2021-03-22 00:01:03
【问题描述】:

我正在制作一个 Discord Bot,我需要它自动从输入“o-convo”的发件人那里获取输入,而不需要前缀。然后,机器人将响应该消息并准备再次等待问题。有没有办法直接获取用户消息的内容?我对此进行了尝试,但机器人没有响应。控制台中没有错误。如果有人可以提供详细信息,将不胜感激!

if message.content.lower().startswith('o-convo'):
          channel = message.channel
          BotUser = message.author
          await channel.send("Conversation locked with " + str(BotUser) + ". Type o-quit to exit.")
          while True:
            async def on_message(message):
              if message.author == BotUser:
                convo_message = message.content()
                response = bot.get_response(convo_message)
                await message.channel.send(response)
              elif message.content.lower().startswith('o-quit'):
                return
              else:
                return
            break

【问题讨论】:

  • 可能是因为您的 while 循环末尾有一个 break

标签: discord discord.py


【解决方案1】:

在循环内创建 async def on_message 将不起作用,因为没有任何东西告诉库调用它。

相反,该库提供了一种通过 Bot.wait_for 执行此操作的方法

其中提供了有关如何等待特定消息的示例,这很像您当前的if message.author == BotUser: 行。

因此,您可以执行以下类似操作,而不是创建异步 def:

    if message.content.lower().startswith('o-convo'):
        channel = message.channel
        BotUser = message.author
        await channel.send("Conversation locked with " + str(BotUser) + ". Type o-quit to exit.")
        while True:
            def check(m):
                return m.author == BotUser or m.content.lower().startswith('o-quit')
            response = await bot.wait_for("message",check=check)
            if response.content.startswith("o-quit"):
                break
            else:
                await message.channel.send(response.content)

【讨论】:

  • 对他来说,一个很好的提示可能是他正在搜索的是message.content,他还使用了message.content(),这将调用不存在的函数,因为它是一个变量
  • 好吧,很抱歉回复晚了,代码确实会监听何时发送消息以及是否返回 o-quit。但是,我不知道如何获取作为对提示的响应而发送的消息的值/文本。
  • response.content 就是这样做的
  • 啊,我明白了,我现在可以使用它了,我只需要稍微更改一下代码。非常感谢!
猜你喜欢
  • 2021-08-22
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多