【问题标题】:Checking message.content for certain text检查某些文本的 message.content
【发布时间】:2020-05-13 07:53:41
【问题描述】:

我正在尝试制作一个多项选择的琐事后台任务。我试图等待 client.wait_for_message 只为正确答案或不正确的选择。我正在检查 message.content 但我错过了一些东西,因为无论任何人输入什么,它都会显示“错误答案”,除非它是正确答案。如果有人输入的不是错误答案或正确答案,那么我希望它忽略他们的文字。

async def trivia_loop():
    await client.wait_until_ready()
    channel = client.get_channel("123456778999533")
    triviamonies = random.randint(1250, 2550)
    while not client.is_closed:
        await client.send_message(channel, '**Category**: {}\n**Difficulty**: {}\n\n{}\n\n**Potential Answers:**\n{}'.format(category, formatdiff, formatquestion, '\n'.join(tup)))
        winner = ''
        def check(m):
            return m.content.lower() == formatanswer.lower() and m.content.lower() != incorrectanswer1 or incorrectanswer2 or incorrectanswer3
        while winner == '':
            answerid = await client.wait_for_message(timeout=420, channel=channel, check=check)
            if answerid is None:
                winner = 1
                await client.delete_message(questionsend)
                await client.send_message(channel, 'No one answered correctly! The answer was {}'.format(formatanswer))
                await asyncio.sleep(840)
            elif get_triviaguessesTaken(answerid.author) < 1 and answerid.content.lower() == formatanswer.lower():
                winner = 1
                await client.delete_message(questionsend)
                await client.send_message(channel, '**{}** is correct!\n{} earns **${:,}** !'.format(formatanswer, answerid.author.mention, triviamonies))
                add_dollars(answerid.author, triviamonies)
                add_trivias(answerid.author, 1)
                await asyncio.sleep(900)
            elif get_triviaguessesTaken(answerid.author) < 1 and answerid.content.lower() == incorrectanswer1 or incorrectanswer2 or incorrectanswer3:
                await client.send_message(channel, '**{}** that is incorrect! Please try again on the next trivia question'.format(answerid.author.mention))
                add_triviaguessesTaken(answerid.author, 1)
            elif get_triviaguessesTaken(answerid.author) > 0 and answerid.content.lower() == incorrectanswer1 or incorrectanswer2 or incorrectanswer3:
                await client.send_message(channel, '{} you have already tried to guess this trivia question. Try again on the next one!'.format(answerid.author.mention))

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    Discord.py 版本:

    从您的代码的外观来看,您仍在使用 d.py (v0.16.12) 的异步版本,并且您还没有迁移到更新和稳定的 rewrite version of d.py

    我强烈建议为最新的稳定版本运行 pip install --upgrade discord.py。确保你这样做并尽快重写你的代码,因为当你有更少的代码可以使用时,它会减少你的痛苦。

    Wait_for() 重写中的用法:

    文档在 on_message 事件中使用了一个示例,但我将在此处使用命令来创建一个示例以适应您的情况,因此您可以使用两种方法并且可以混合搭配:

    @bot.command(aliases=["trivia"])
    async def starttrivia(ctx):
        answers = ["paris", "london", "oslo"]
        await ctx.send(f"What's the capital of France?\n\n*Potential answers:*\n{', '.join(answers)}")
        correct = False
        
        def check(m):
            for a in answers:
                if a in m.content.lower():
                    return True
    
        while not correct:
            try:    
                reply = await bot.wait_for("message", check=check, timeout=30)
                if "paris" in reply.content.lower():
                    await ctx.send(f"Correct! {reply.author.mention} got the right answer!")
                    correct = True
                else:
                    await ctx.send("Incorrect, try again!")
            except asyncio.TimeoutError: # exception thrown when no reply is sent in time
                await ctx.send("Nobody guessed for 30 seconds! The answer was: Paris")
                break
    

    【讨论】:

    • 感谢您帮助我。我添加了你放在那里的检查,这是有道理的,但将 .lower() 添加到 a 并且它与 if/else 语句完美配合
    • @hardestchat 很高兴听到您成功了!如果您在migrating to rewrite 期间不确定任何事情,请不要害怕寻求帮助!祝你好运:)
    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2018-06-17
    • 2012-11-08
    • 2013-05-20
    相关资源
    最近更新 更多