discord.py重写|如何等待作者留言? [英] discord.py rewrite | How to wait for author message?
问题描述
我正在执行一条命令,等待用户回复该漫游器,但我希望该漫游器仅接受作者的回复.
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重写|如何等待作者留言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!