【发布时间】:2019-06-26 19:09:28
【问题描述】:
我正在创建一个 Discord 版主机器人,并且我想创建一个“每个服务器的自定义前缀”。
我的问题是我正在尝试导入 json 库,但它不起作用。它返回一个错误。
我正在使用以下代码(测试代码)
from discord.ext import commands
import discord
import json
import asyncio
with open("prefixes.json") as json_data:
prefixes = json.load(json_data)
default_prefix = "!"
def prefix(bot, message):
id = message.server.id
return prefixes.get(id, default_prefix)
bot = commands.Bot(command_prefix=prefix)
@bot.command(name="prefix", pass_context=True)
async def _prefix(ctx, new_prefix):
# Do any validations you want to do
prefixes[ctx.message.server.id] = new_prefix
with open("prefixes.json", "w") as f:
json.dump(prefixes, f)
. . .
我从上面的代码中得到这个错误:
===== RESTART: C:\Users\Raphaël\Desktop\Python\Moderator Bot\coreTest.py =====
Traceback (most recent call last):
File "C:\Users\Raphaël\Desktop\Python\Moderator Bot\coreTest.py", line 6, in <module>
prefixes = json.load(json_data)
File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 2 (char 30)
>>>
【问题讨论】:
-
我在 prefixes.json 文件中预填了两个不和谐服务器的信息和要使用的前缀
-
"prefixes.json"看起来像什么?该错误抱怨它无法加载,因此文件的格式可能正确为json -
错误在您的
prefixes.json中。它应该类似于{"1234": "$", "5678": "@"}。见json.org
标签: json python-3.6 discord.py