![](/img/trans.png)
[英](discord.py) wait_for function not checking if author reacted to message
[英]discord.py how to wait for author message using wait_for?
我正在發出一個等待用戶回復機器人的命令,但我希望機器人只接受作者的回復。
@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
我的問題是任何人都可以響應“選擇一個號碼”,而我只希望啟動命令的人能夠響應。
我不太確定該嘗試什么,但我認為這可能與爭論有關。 但是,我不知道從哪里開始,因此將不勝感激! 萬分感謝。
你需要重寫你的check
以便它知道作者是誰。 一種方法是使用閉包。 假設您有一張現有支票
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.