1 回答

TA貢獻1877條經驗 獲得超6個贊
該input()功能用于console輸入,而不是 Discord。要在 Discord 中等待消息,請使用client.wait_for():
message = await client.wait_for("message")
您還可以編寫一個check函數來檢查消息是否符合您的條件:
def checkfunction(message):
? ? return message.author == ctx.author and ctx.channel == message.channel and message.content.isdigit()?
message = await client.wait_for("message", check=checkfunction)
如果 check 函數返回 True,則代碼將繼續,否則它將等待另一條消息。
您現在可以將其實現到您的代碼中:
@client.command()
async def guessnumber(ctx):
? ? await ctx.send(f"Hello {ctx.author.name}! I'm thinking of a number between 1 and 20. You are given 6 tries to find the number. Good luck!")
? ? secretNumber = random.randint(1,20)
? ? def check(message):
? ? ? ? return message.author == ctx.author and message.channel == ctx.channel? and message.content.isdigit()
? ? for guessesTaken in range(6):
? ? ? ? guess = int((await client.wait_for('message', check=check)).content)
? ? ? ? if guess < secretNumber:
? ? ? ? ? ? await ctx.send("Your guess is too low")
? ? ? ? elif guess > secretNumber:
? ? ? ? ? ? await ctx.send("Your guess is too high")
? ? ? ? else:
? ? ? ? ? ? await ctx.send(f"GG! You correctly guessed the number in {guessesTaken + 1} guesses!")
? ? else:
? ? ? ? await ctx.send(f"Nope, sorry, you took too many guesses. The number I was thinking of was {secretNumber}")
添加回答
舉報