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}")
添加回答
举报