【发布时间】: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