该命令如何正常运行,但是当我运行它时,它允许您猜测两次?(Discord.py)

时间:2020-08-17 16:48:12

标签: discord.py

基本上该命令运行两次,但仅在版本上 如果有人知道为什么它运行两次,您能解释一下吗,因为这个错误似乎使我感兴趣

@bot.command()
async def numguess(ctx):
    primary_id = str(ctx.message.author.id)
    number = random.randint(0, 40)
    await ctx.send("I am thinking of a number from 1-40. What is it?")

    for i in range(0, 50):
        response = await ctx.bot.wait_for('message')

        guess = int(response.content)

        if guess > number:
            await ctx.send('That guess is too big! Try again!')

        elif guess < number:
            await ctx.send('That guess is too small! Try again!')

        else:
            await ctx.send(f'You got it right! It only took you {i + 1} attempts! {100 - (i+1)} coins have been earned!')

            amount = 100 - (i + 1)
            amounts[primary_id] += amount

def _save():
    with open('amounts.json', 'w+') as f:
        json.dump(amounts, f)```

1 个答案:

答案 0 :(得分:0)

我发现,如果最后让它生成一个新数字,是的,它将仍然运行两次,但是数字会有所不同,这意味着您不能永远将钱用于耕作

@bot.command()
async def numguess(ctx):
    primary_id = str(ctx.message.author.id)
    number = random.randint(0, 40)
    await ctx.send("I am thinking of a number from 1-40. What is it?")

    for i in range(0, 50):
        response = await ctx.bot.wait_for('message')

        guess = int(response.content)

        if guess > number:
            await ctx.send('That guess is too big! Try again!')

        elif guess < number:
            await ctx.send('That guess is too small! Try again!')

        else:
            await ctx.send(f'You got it right! It only took you {i + 1} attempts! {100 - (i+1)} coins have been earned!')
            number = random.randint(0, 40)

            amount = 100 - (i + 1)
            amounts[primary_id] += amount
            _save()
def _save():
    with open('amounts.json', 'w+') as f:
        json.dump(amounts, f)```