我正在研究一种更先进的调平系统.我希望any服务器上的用户,只要他们是管理员,就能够更改XP值.我正在使用服务器数据库中的一个值来实现这一点.每次它对该命令进行编辑时,我注意到它会截断整个文件并应用所做的编辑.我已经用了同样的5行代码来做这件事,但它开始不起作用了.我用来切换关卡系统的另一个命令运行得非常好,即使在相同的5行代码中也是如此.我甚至try 将正在更改的值设置为另一个值,但它仍然存在相同的问题,所以这不可能是值的问题.有人知道如何解决这个问题吗?请和谢谢:)站着真的很讨厌
代码
@levelmds.command(name="set_xp_addition", description="Choose how many XP you want to give to your members. Set 0 or blank to go to the default. (4)")
async def setxpaddition(interaction: discord.Interaction, xp: int = 0):
with open("database\\wae.json", encoding="utf-8") as f:
data = json.load(f)
if str(interaction.guild.id) in data:
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message(f":x: Only administrators of {interaction.guild.name} can execute this command.")
else:
if xp:
if xp == 0:
with open("database\\wae.json", encoding="utf-8") as f:
data = json.load(f)
data[str(interaction.guild.id)]['xpaddition'] = 0
with open("database\\wae.json", "w+") as fa:
json.dump(data, fa)
await interaction.response.send_message("✅ Successfully set XP addition to the default (4)")
else:
with open("database\\wae.json", encoding="utf-8") as f:
data = json.load(f)
data[str(interaction.guild.id)]['xpaddition'] = xp
with open("database\\wae.json", "w+") as fa:
json.dump(data, fa)
await interaction.response.send_message(f"✅ Successfully changed XP addition to **{xp}.**")
else:
with open("database\\wae.json", encoding="utf-8") as f:
data = json.load(f)
data[str(interaction.guild.id)]['xpaddition'] = 0
with open("database\\wae.json", "w+") as fa:
json.dump(data, fa)
await interaction.response.send_message("✅ Successfully set XP addition to the default (4)")
(顺便说一句,此命令位于一个组中,因此为了进行测试,请考虑将levelmds
更改为bot.tree.command
.)