简体   繁体   English

Discord.py 禁用模块命令

[英]Discord.py Disable Module Command

I made a "disable module" which is working, but when it loads into a json file I get a error message.我制作了一个正在运行的“禁用模块”,但是当它加载到 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

After doing the command it loads into a json file like this: {}{"750485129436201023": "Channel ID"} and the error message I get is: End of file expected .执行命令后,它会加载到一个 json 文件中,如下所示: {}{"750485129436201023": "Channel ID"}并且我收到的错误消息是: End of file expected The error is between the 2nd {.错误在第二个 { 之间。 Can someone help?有人可以帮忙吗?

{}{"750485129436201023": "Channel ID"} is not valid JSON. {}{"750485129436201023": "Channel ID"}不是有效的 JSON。

Valid JSON can only have one root element, such as {}有效的 JSON 只能有一个根元素,例如{}

Change your JSON file to just:将您的 JSON 文件更改为:

{"750485129436201023": "Channel ID"}

Python is appending to the file, instead of overwriting it, the easiest way to fix this is to seek to the beginning of the file before writing: Python 附加到文件,而不是覆盖它,解决这个问题的最简单方法是在写入之前seek文件的开头:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM