Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python discord.py重写|如何等待作者消息?_Python_Python 3.x_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python discord.py重写|如何等待作者消息?

Python discord.py重写|如何等待作者消息?,python,python-3.x,discord.py,discord.py-rewrite,Python,Python 3.x,Discord.py,Discord.py Rewrite,我正在发出一个命令,等待用户回复bot,但我希望bot只接受作者的回复 @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

我正在发出一个命令,等待用户回复bot,但我希望bot只接受作者的回复

@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
@client.command(name='numgame',
猜一个介于1和100之间的数字,
通过(上下文=真)
异步def numgame(上下文):
number=random.randint(1100)
猜测=4
猜猜看!=0:
wait context.send('选择一个介于1和100之间的数字')
msg=wait client.wait_for('message',check=check,timeout=30)
尝试=int(msg.content)
如果尝试>编号:
wait context.send(str(guess)+“猜测左…”)
等待asyncio.sleep(1)
等待上下文。发送('尝试降低')
等待asyncio.sleep(1)
猜测-=1
elif尝试<次数:
wait context.send(str(guess)+“猜测左…”)
等待asyncio.sleep(1)
等待上下文。发送('尝试更高')
等待asyncio.sleep(1)
猜测-=1
elif尝试==编号:
等待上下文。发送('你猜对了!干得好!')
打破
我的问题是,任何人都可以响应“选择一个数字”,而我只希望启动命令的人能够响应


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

您需要重写您的
检查,以便它知道作者是谁。一种方法是使用闭包。假设你有一张支票

def check(message):
    return message.content == "Hello"
您可以将其替换为一个函数,该函数生成与要检查的作者相同的检查函数,并将其注入其中

def check(author):
    def inner_check(message):
        return message.author == author and message.content == "Hello"
    return inner_check
然后,通过使用适当的参数调用外部检查,将内部检查传递给
wait_for

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

我们能看看你的
支票吗?您需要在其中包含作者的支票function@PatrickHaugh,我编辑了我的帖子以显示整个命令。希望这有帮助。您正在将
check=check
传递到
等待
。什么是
检查
?哦,我使用的检查完全不相关。哈哈,我应该怎样才能找到作者?我现有的检查是:
def check(message):try:int(message.content)return True,ValueError除外:return False
我应该如何在这个实例中合并闭包?感谢所有帮助!嘿,我知道这个答案已经2年了,但我尝试了一下,得到了:
discord.ext.commands.errors.CommandInvokeError:Command引发了一个异常:TypeError:rolecheck()接受1个位置参数,但给出了2个
没关系,我忘了传递self,因为该命令在一个cog lol中