如何让 discord.py bot 最近查找来自特定用户的消息?

时间:2021-03-30 19:35:43

标签: discord discord.py

我有一个用 python 编写的机器人,我想在机器人中加入一个数字游戏。游戏代码如下。 (nl 是表示 os.linesep 的变量)

    secret_number = random.randint(0, 100)
    guess_count = 0
    guess_limit = 5
    print(f'Welcome to the number guessing game! The range is 0 - 100 and you have 5 attempts to guess the correct number.')
    while guess_count < guess_limit:
        guess = int(input('Guess: '))
        guess_count += 1
        if guess > secret_number:
            print('Too High.', nl, f"You have {guess_limit - guess_count} attempts remaining.")
        elif guess < secret_number:
            print('Too Low.', nl, f"You have {guess_limit - guess_count} attempts remaining.")
        elif guess == secret_number:
            print("That's correct, you won.")
            break

    else:
        print("Sorry, you failed.")
        print(f'The correct number was {secret_number}.')

所以我希望能够在不和谐消息传递系统中使用它。我的问题是我需要机器人扫描来自发起游戏的特定用户的号码的最新消息。我怎么能这样? 为了简化我的问题,我该怎么说:

if message from same message.author = int():
    guess = that^

1 个答案:

答案 0 :(得分:0)

此处的解决方案是wait_for 来自该用户的另一条消息。你可以这样做:

msg = await client.wait_for('message', check=message.author == PREVIOUS_SAVED_MSG_AUTHOR_AS_VARIABLE, timeout=TIMEOUT) 
# You don't really need a timeout but you can add one if you want 
# (then you'd need a except asyncio.TimeoutError)
if msg.content == int():
    guess = msg
else:
    ...