我正在研究一种更先进的调平系统.我希望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.)

推荐答案

首先,第10行的IF语句中的代码是无法到达的原因0 is evaluated as false,因此第9行的IF语句只有在XP不为0时才被满足.

以下是简化的代码:

@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 != 0:
                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)")

关于文件被截断并通过修改写入的问题,when using w+ the existing content in the file is cleared,这就是它被擦除的原因.这对于将新数据转储到文件是必要的,但如果许多服务器中的许多用户正在使用该命令,则为maybe you are going to lose data.

Python相关问答推荐

如何在PIL、Python中对图像应用彩色面膜?

Python中的负前瞻性regex遇到麻烦

如何在Python中使用io.BytesIO写入现有缓冲区?

我在使用fill_between()将最大和最小带应用到我的图表中时遇到问题

Pystata:从Python并行运行stata实例

用合并列替换现有列并重命名

C#使用程序从Python中执行Exec文件

OR—Tools中CP—SAT求解器的IntVar设置值

组/群集按字符串中的子字符串或子字符串中的字符串轮询数据框

Pandas DataFrame中行之间的差异

使用密钥字典重新配置嵌套字典密钥名

什么是最好的方法来切割一个相框到一个面具的第一个实例?

在嵌套span下的span中擦除信息

以逻辑方式获取自己的pyproject.toml依赖项

python sklearn ValueError:使用序列设置数组元素

PYTHON、VLC、RTSP.屏幕截图不起作用

30个非DATETIME天内的累计金额

为什么t sns.barplot图例不显示所有值?'

为罕见情况下的回退None值键入

有了Gekko,可以创建子模型或将模型合并在一起吗?