【问题标题】:Issue in Discord.py while linking JSON链接 JSON 时 Discord.py 中的问题
【发布时间】:2021-07-18 22:17:09
【问题描述】:

我在将 Discord.py 与 JSON 链接时遇到问题。这是我的代码:

@client.event
async def on_guild_join(guild):
    role = discord.utils.get(guild.roles, name="Muted")

    with open("muted_roles.json", "r") as f:
        muted_roles = json.load(f)
    muted_roles[str(guild.id)] = str(role.id)

    with open("muted_roles.json", "w") as f:
        json.dump(muted_roles, f, indent=4)

这是我的错误:

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "C:\Users\angel\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\angel\Documents\python\Bots\dave\dave.py", line 59, in on_guild_join
    muted_roles = json.load(f)
  File "C:\Users\angel\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\angel\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\angel\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\angel\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我很早就感谢您的帮助。

【问题讨论】:

  • 你的 json 文件是空的,它应该至少包含左括号和右括号 ({})
  • 不起作用,而且在 cmets 中写在这里太长了
  • @aleissues 然后用新信息更新您的帖子
  • 您可能会收到一个 KeyError 指示 str(guild.id) 键不在字典中。
  • 你能告诉我们你的json文件吗?

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


【解决方案1】:

你的 JSON 文件是空的,你应该用 {} 填充它,它会修复!

【讨论】:

猜你喜欢
  • 2019-03-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 2013-08-19
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多