【发布时间】:2020-10-01 21:42:29
【问题描述】:
我有一个用 python 编码的不和谐机器人。我希望能够将用户数据保存在 JSON 文件中,但由于某种原因,我编写的这段代码不起作用。这是给我带来问题的代码。
@client.command()
async def slash(ctx):
amount = random.randint(1,10)
with open("data.json", "r") as f:
users = json.load(f)
await userupdateslash(users, ctx.author, amount)
with open("data.json", "w") as f:
json.dump(users, f, indent=4)
f.close()
embed = discord.Embed(
title = "Slash!",
description = (f"{ctx.author} slashed through a stormtrooper and gained {amount}xp"),
colour = discord.Colour.green()
)
embed.set_footer(text='Created by [VG] Xezo#6969')
embed.set_thumbnail(url='https://cdn.discordapp.com/app-icons/760648752435822613/c1d0d4451e8e3123373709d31b0bffba.png?size=128')
embed.set_author(name='Baby Yoda',
icon_url='https://cdn.discordapp.com/app-icons/760648752435822613/c1d0d4451e8e3123373709d31b0bffba.png?size=128')
await ctx.send(embed=embed)
async def userupdateslash(users, user, xp):
if user.id in users:
try:
users[user.id]["xp"] += xp
except Exception as error:
raise(error)
【问题讨论】:
-
“它不工作”是什么意思?你能帮我修正一下你的代码块的缩进吗?