【问题标题】:Discord.py Disable Module CommandDiscord.py 禁用模块命令
【发布时间】:2020-10-08 00:35:07
【问题描述】:

我制作了一个正在工作的“禁用模块”,但是当它加载到 json 文件中时,我收到一条错误消息。

@client.command(aliases=["disable economy"])   
async def disable_economy(message): 
    with open('disable economy.json', 'r+') as f:
        channel = json.load(f)
        if not f'{message.channel.id}' in channel:
            channel[f'{message.channel.id}'] = "Channel ID"
            json.dump(channel, f)
            await message.channel.send(f"Economy has been disabled in <#{message.channel.id}>. ")
            return
        if f'{message.channel.id}' in channel:
            await message.channel.send("Economy is already disabled in this channel.")
            return

执行命令后,它会加载到一个 json 文件中,如下所示: {}{"750485129436201023": "Channel ID"},我得到的错误信息是:End of file expected。错误在第二个 {.有人可以帮忙吗?

【问题讨论】:

    标签: python python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    {}{"750485129436201023": "Channel ID"} 不是有效的 JSON。

    有效的JSON只能有一个根元素,例如{}

    将您的 JSON 文件更改为:

    {"750485129436201023": "Channel ID"}
    

    Python 正在追加文件,而不是覆盖它,解决此问题的最简单方法是在写入之前将seek 附加到文件的开头:

    f.seek(0)
    json.dump(channel, f)
    

    【讨论】:

    • 是的,但是当我再次执行命令时,它会像这样存储在 json 文件中:{"739683588408082465": "Channel ID"}{"739683588408082465": "Channel ID", "750485129436201023": "Channel ID"} 并且我收到相同的错误消息
    • @drew 谢谢!
    猜你喜欢
    • 2018-11-07
    • 2021-07-27
    • 2021-07-27
    • 2020-05-26
    • 2020-09-11
    • 1970-01-01
    • 2022-01-23
    • 2021-04-06
    • 2021-05-11
    相关资源
    最近更新 更多