【问题标题】:For some reason I get an error message when I try to run my balance command in discord.py whilst having the .json code in my number game出于某种原因,当我尝试在 discord.py 中运行 balance 命令同时在我的数字游戏中有 .json 代码时收到错误消息
【发布时间】:2020-12-03 19:39:39
【问题描述】:

我刚刚开始使用 .json,使用 discord.py 制作经济型机器人。在我的代码中,我有一个数字猜谜游戏,它很容易解释,当我玩游戏时,一切都很顺利,但是当我尝试检查我的余额时,我得到了这个错误:

line 59, in numguess
    guess = int(response.content)
ValueError: invalid literal for int() with base 10: 'game.balance'

这是我的实际代码:

@bot.command()
async def numguess(ctx):
    global numguess_earnings

    await open_account(ctx.author)

    user = ctx.author
    users = await get_bank_data()

    #main--------------------
    number = random.randint(0, 100)
    await ctx.send("I am thinking of a number from 1-100. What is it?")

    for i in range(0, 100):
        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!')

            numguess_earnings = 100 - (i + 1)
    #endmain-------------------

    users[str(user.id)]["wallet"] += numguess_earnings

    with open("main.json", "w") as f:
        json.dump(users,f)



#ECONOMY COMMANDS===============================================================================================================================================
#balance-----------------------------------------------------------------------------------------------------------------
@bot.command()
async def balance(ctx):
    await open_account(ctx.author)

    user = ctx.author
    users = await get_bank_data()

    wallet_amt = users[str(user.id)]["wallet"]

    em = discord.Embed(title = f"{ctx.author.name}'s balance", color = discord.Color.red())
    em.add_field(name = "Wallet balance", value = wallet_amt)


    await ctx.send(embed = em)


#helper commands---------------------------------------------------------------------------------------------------------------------------------------
#open account-----------------------------------------------------------------------------
async def open_account(user):

    users = await get_bank_data()

    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 0


    with open("main.json", "w") as f:
        json.dump(users,f)
    return True

#get bank data-----------------------------------------------------------------------------
async def get_bank_data():
    with open("main.json", "r") as f:
        users = json.load(f)

    return users

我还想指出,当我将两个命令分开时,该命令运行良好,但是一旦我开始在数字游戏中使用经济命令,错误就开始出现。谢谢。

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    机器人收到一条消息,其内容为game.balance,无法转换为整数。确保键入一个数字。

    另外,使用check只接受作者为ctx.author,频道为ctx.channel,内容为数字的消息。

    def check(m):
        return m.author == ctx.author and m.channel == ctx.channel and m.content.isdigit()
    response = await ctx.bot.wait_for('message', check=check)
    guess = int(response.content)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多