【问题标题】:discord.py | check for 2 or more messages不和谐.py |检查 2 条或更多消息
【发布时间】:2020-10-19 22:47:59
【问题描述】:

我尝试编写一个不和谐的机器人程序,它会一个接一个地检查多条消息。 所以如果有人写“你好”,机器人应该写“你好”并检查以下消息“你好吗?”是。

    @client.event
async def on_message(message):
    if 'hi' in message.content:
        await message.channel.send('Hello there.')
        if 'How are you?' in message.content:
            await message.channel.send('I am fine.')

但是机器人忽略了第二个命令。为什么?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    当这样写它时,它会检查消息中是否有一个你好,那里有一个你好。要让您的机器人响应然后等待响应,请使用 wait_for

    看起来像这样:

    @client.event
    async def on_message(message):
        if 'hi' in message.content:
            await message.channel.send('Hello there.')
            await bot.wait_for("How are you", check=check)
    

    这是做什么的,如果一条消息中有 hi,机器人会响应,然后等待一条带有 How are you 的消息

    【讨论】:

    • 机器人正在等待,但它无法回答你好吗?我该怎么做?
    • 在等待之后就行了,你会写 ctx.send("great hbu")
    【解决方案2】:

    您发送的第二条消息将再次作为消息传递给 on_message() 函数。 试试这个

    async def on_message(message):
        if 'hi' in message.content:
            await message.channel.send('Hello there.')
        if 'How are you?' in message.content:
            await message.channel.send('I am fine.')
    

    【讨论】:

      【解决方案3】:

      为了完成这篇文章并帮助其他人,我在此处发布了正确且已完成的代码。

      @client.event
      async def on_message(message):
          if message.content.startswith('Hi'):
              channel = message.channel
              await channel.send('Hello!')
      
              def check(message1):
                  return message1.content == 'How are you?' and message1.channel == channel
      
              msg = await client.wait_for('message', check=check)
              await channel.send("I am fine.".format(msg))
      

      我希望这篇文章可以帮助其他人!祝你有美好的一天!

      【讨论】:

        猜你喜欢
        • 2021-06-28
        • 2021-06-11
        • 2021-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-18
        • 2022-01-17
        相关资源
        最近更新 更多