Discord.py Therapy Bot指定用户且未输入While-Loop

时间:2020-02-23 18:30:14

标签: python bots discord discord.py

我是python的新用户,对此感到抱歉。我一直在尝试为不相关的用户进入一个while循环,在该循环中,机器人不断询问基本问题,直到他们说出“谢谢”为止。

dissociation_event = False
dissociated_user = ""

@client.event
async def on_message(message):
    if ('dissocia' in message.content):
        dissociation_event = True 
        dissociated_user = message.author

        if dissociation_event==True and dissociated_user == message.author:
            response = 'It sounds like you may be dissociating, would you like help with grounding
 techniques?\n(yes.joi/no.joi)'
            await message.channel.send(response)

        if message.content.lower() == 'no.joi' and dissociation_event==True and dissociated_user == message.author:
            dissociation_event=False
            dissociated_user=""
            response = 'Sorry for misunderstanding'
            await message.channel.send(response)
        if message.content.lower() == 'yes.joi' and dissociation_event==True and dissociated_user == message.author:
            while not ('thanks' in message.content):
                questions = ['What is something you can see?(... etc),
                ]
                response = random.choice(questions)
                await message.channel.send(response)
            if 'thanks' in message.content:
                response = 'Hope that you feel better soon :)'
                await message.channel.send(response)

当提到“ dissocia”时,机器人将询问最初的问题: 'It sounds like you may be dissociating, would you like help with grounding techniques?'

我希望用户能够说是/否,并且机器人要进入while循环,询问问题,直到他们说出“谢谢”为止。该机器人目前不询问任何问题,我不确定为什么。我在指定机器人只应响应最初触发初始问题并启动问题循环的用户方面遇到麻烦。任何帮助将不胜感激,我已经坚持了一段时间。

1 个答案:

答案 0 :(得分:0)

我没有在此答案中给出完整的解决方案,但我会指出您正在犯的逻辑错误。

您的message对象始终引用第一个发送的消息。您必须告诉它等待新的消息,并且必须检查该新消息的是,否和谢谢。

https://discordpy.readthedocs.io/en/latest/api.html?highlight=wait_for#discord.Client.wait_for

@client.event
async def on_message(message):
  if message.content.startswith('$greet'):
    channel = message.channel
    await channel.send('Say hello!')

    def check(m):
      return m.content == 'hello' and m.channel == channel

    msg = await client.wait_for('message', check=check)
    await channel.send('Hello {.author}!'.format(msg))

您可以在此处用$greet来召唤机器人。它将以Say hello!回答。发送不相关消息的其他用户将被忽略,因为他们未通过check。它会对从同一文本通道发送的评论hello做出反应。

您可以扩大支票的范围,以包括原始作者:

author = message.author
def check(m):
  return m.content == 'hello' and m.channel == channel and m.author == author

您需要将while循环调整为重复await client.wait_for新消息,直到满足条件为止。