python - Discord.py-经济系统
问题描述
我的代码:
@client.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"]
bank_amt= users[str(user.id)]["Bank"]
em = discord.Embed(title=f"{ctx.author.name}'s balance.", color=discord.Color.teal())
em.add_field(
name="Wallet Balance",value=wallet_amt
)
em.add_field(
name="Bank Balance",value=bank_amt
)
await ctx.send(embed=em)
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
users[str(user.id)]["Bank"] = 0
with open("bank.json",'w') as f:
users = json.dump(users,f)
return True
async def get_bank_data():
with open("bank.json",'r') as f:
users = json.load(f)
return users
@client.command()
async def beg(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
earnings = random.randrange(101)
await ctx.send(f"Someone gave your {earnings} coins")
users[str(user.id)]["Wallet"] += earnings
with open("bank.json",'r') as f:
users = json.dump(users,f)
错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)
所以我正在为我的不和谐机器人制作一个经济系统,并遇到了这个错误。我真的不知道如何解决这个问题,做事只会导致更多错误。如果有人可以纠正会很酷,并且可能会解释我哪里出错了。
解决方案
它只是 { } 写它它之所以起作用是因为 json 将文件存储为字典,并且还在 beg 命令中使用这一行与 open("bank.json", 'r') as f: users = json.dump(users, f)将 R(读取模式)更改为 W(写入模式),这样可以存储您拥有的硬币数量
推荐阅读
- c - 错误:多重定义和第一次在这里定义
- python - PIL.ImageDraw.arc 什么也没画
- python - 需要帮助过滤和合并两个熊猫数据框
- image - 上传图片 - 找不到 404 错误 - 使用 ant design Form 和 Upload 组件
- javascript - Javascript 中是否可以接受 PIN 缓存?
- angular - Angular:如何使用服务从服务器获取数据并存储在数组中?
- python-3.x - 编译时使用 flatpak-pip-generator 导致“错误:找不到版本”
- python - Tkinter Python(宏)
- mysql - 读取文件系统或运行数据库查询
- apache-spark-sql - 如何在 aws-glue 雪花上访问我的 spark SQL 查询