1. <small id='JAI8V'></small><noframes id='JAI8V'>

    <legend id='JAI8V'><style id='JAI8V'><dir id='JAI8V'><q id='JAI8V'></q></dir></style></legend>

      <bdo id='JAI8V'></bdo><ul id='JAI8V'></ul>
  2. <tfoot id='JAI8V'></tfoot>
  3. <i id='JAI8V'><tr id='JAI8V'><dt id='JAI8V'><q id='JAI8V'><span id='JAI8V'><b id='JAI8V'><form id='JAI8V'><ins id='JAI8V'></ins><ul id='JAI8V'></ul><sub id='JAI8V'></sub></form><legend id='JAI8V'></legend><bdo id='JAI8V'><pre id='JAI8V'><center id='JAI8V'></center></pre></bdo></b><th id='JAI8V'></th></span></q></dt></tr></i><div id='JAI8V'><tfoot id='JAI8V'></tfoot><dl id='JAI8V'><fieldset id='JAI8V'></fieldset></dl></div>

      discord.py 如何使用 wait_for 等待作者消息?

      时间:2023-09-11

      • <bdo id='ug943'></bdo><ul id='ug943'></ul>
        <legend id='ug943'><style id='ug943'><dir id='ug943'><q id='ug943'></q></dir></style></legend>
            <tbody id='ug943'></tbody>
            1. <i id='ug943'><tr id='ug943'><dt id='ug943'><q id='ug943'><span id='ug943'><b id='ug943'><form id='ug943'><ins id='ug943'></ins><ul id='ug943'></ul><sub id='ug943'></sub></form><legend id='ug943'></legend><bdo id='ug943'><pre id='ug943'><center id='ug943'></center></pre></bdo></b><th id='ug943'></th></span></q></dt></tr></i><div id='ug943'><tfoot id='ug943'></tfoot><dl id='ug943'><fieldset id='ug943'></fieldset></dl></div>

              <small id='ug943'></small><noframes id='ug943'>

              <tfoot id='ug943'></tfoot>
                本文介绍了discord.py 如何使用 wait_for 等待作者消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在发出等待用户回复机器人的命令,但我希望机器人只接受作者的回复.

                I am making a command which waits for a user to reply to the bot, but I would like the bot to only accept the author's reply.

                @client.command(name='numgame',
                            brief='Guess a number between 1 and 100',
                            pass_context=True)
                async def numgame(context):
                number = random.randint(1,100)
                guess = 4
                while guess != 0:
                    await context.send('Pick a number between 1 and 100')
                    msg = await client.wait_for('message', check=check, timeout=30)
                    attempt = int(msg.content)
                    if attempt > number:
                        await context.send(str(guess) + ' guesses left...')
                        await asyncio.sleep(1)
                        await context.send('Try going lower')
                        await asyncio.sleep(1)
                        guess -= 1
                    elif attempt < number:
                        await context.send(str(guess) + ' guesses left...')
                        await asyncio.sleep(1)
                        await context.send('Try going higher')
                        await asyncio.sleep(1)
                        guess -=1
                    elif attempt == number:
                        await context.send('You guessed it! Good job!')
                        break
                

                我的问题是任何人都可以响应选择一个数字",而我只希望启动命令的人能够响应.

                My issue is that anyone can respond to "Pick a number," whereas I would only like the person who started the command to be able to respond.

                我不太确定该尝试什么,但我认为这可能与争论有关.不过,我不知道从哪里开始,所以一个解决方案将不胜感激!非常感谢.

                I am not too sure what to try, but I think it may have something to do with arguments. I have no idea where to begin, though, so a solution would be appreciated! Thanks a ton.

                推荐答案

                你需要重写你的 check 让它知道作者是谁.一种方法是使用闭包.假设您有一张现有的支票

                You need rewrite your check so that it knows who the author is. One way of doing this is to use a closure. Let's say you have an existing check

                def check(message):
                    return message.content == "Hello"
                

                您可以将其替换为生成等效检查函数的函数,并将您要检查的作者注入其中

                You can replace this with a function that generates equivalent check functions with the author you want to check for injected into them

                def check(author):
                    def inner_check(message):
                        return message.author == author and message.content == "Hello"
                    return inner_check
                

                然后,您可以通过使用适当的参数调用外部检查,将内部检查传递给 wait_for:

                Then you would pass the inner check to wait_for by calling the outer check with the appropriate argument:

                msg = await client.wait_for('message', check=check(context.author), timeout=30)
                

                为了您的检查,这将是

                def check(author):
                    def inner_check(message): 
                        if message.author != author:
                            return False
                        try: 
                            int(message.content) 
                            return True 
                        except ValueError: 
                            return False
                    return inner_check
                

                这篇关于discord.py 如何使用 wait_for 等待作者消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:带有 async def 的 Python [无效语法] 下一篇:SSL:CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本

                相关文章

                最新文章

                <legend id='pX1XY'><style id='pX1XY'><dir id='pX1XY'><q id='pX1XY'></q></dir></style></legend>

                  <tfoot id='pX1XY'></tfoot>

                1. <i id='pX1XY'><tr id='pX1XY'><dt id='pX1XY'><q id='pX1XY'><span id='pX1XY'><b id='pX1XY'><form id='pX1XY'><ins id='pX1XY'></ins><ul id='pX1XY'></ul><sub id='pX1XY'></sub></form><legend id='pX1XY'></legend><bdo id='pX1XY'><pre id='pX1XY'><center id='pX1XY'></center></pre></bdo></b><th id='pX1XY'></th></span></q></dt></tr></i><div id='pX1XY'><tfoot id='pX1XY'></tfoot><dl id='pX1XY'><fieldset id='pX1XY'></fieldset></dl></div>

                    • <bdo id='pX1XY'></bdo><ul id='pX1XY'></ul>
                  1. <small id='pX1XY'></small><noframes id='pX1XY'>