Discord.py 禁用模块命令
Discord.py Disable Module Command
我制作了一个可以正常工作的“禁用模块”,但是当它加载到 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
。错误在第二个 {.有人可以帮忙吗?
{}{"750485129436201023": "Channel ID"}
无效 JSON.
有效的JSON只能有一个根元素,比如{}
将您的 JSON 文件更改为:
{"750485129436201023": "Channel ID"}
Python 是附加到文件,而不是覆盖它,解决这个问题的最简单方法是在写入之前 seek
到文件的开头:
f.seek(0)
json.dump(channel, f)
我制作了一个可以正常工作的“禁用模块”,但是当它加载到 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
。错误在第二个 {.有人可以帮忙吗?
{}{"750485129436201023": "Channel ID"}
无效 JSON.
有效的JSON只能有一个根元素,比如{}
将您的 JSON 文件更改为:
{"750485129436201023": "Channel ID"}
Python 是附加到文件,而不是覆盖它,解决这个问题的最简单方法是在写入之前 seek
到文件的开头:
f.seek(0)
json.dump(channel, f)