在不协调.py中使用json模块时出现ValueError



我的意图是在机器人加入时立即为欢迎消息设置一个通道,并能够使用指定的命令进行更改。这是我的代码:

import discord
from discord.ext import commands
import json
def get_welcomechannel(client, message,):
with open('welcomechannel.json', 'r') as f:
welcomechannel = json.load(f)

return welcomechannel[str(message.guild.id)]
@client.event
async def on_guild_join(guild):
general = find(lambda x: x.name == 'general',  guild.text_channels)
if general and general.permissions_for(guild.me).send_messages:
await general.send(f'Hello {guild.name}! My name is {client.user.name} and my prefix is ``?``! run ``?help`` to begin using me!')
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)   
prefixes[str(guild.id)] = "?"
with open ('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
with open('welcomechannel.json', 'r') as f:
welcomechannel = json.load(f)   
welcomechannel[str(guild.id)] = "general"
with open ('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)   
prefixes.pop(str(guild.id))
with open ('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
with open('welcomechannel.json', 'r') as f:
welcomechannel = json.load(f)   
welcomechannel.pop(str(guild.id))
with open ('welcomechannel.json', 'w') as f:
json.dump(welcomechannel, f, indent=4)
print(f"{guild.name} kicked me!")

#rest of code here...
@client.command(name="changewelcomechannel")
async def changewelcomechannel(ctx, welcomechannel):
with open('welcomechannel.json', 'r') as f:
welcomechannel = json.load(f)   

welcomechannel[str(ctx.guild.id)] = welcomechannel
with open ('welcomechannel.json', 'w') as f:
json.dump(welcomechannel, f, indent=4)
await ctx.send(f"Welcome channel is now **{welcomechannel}**!"

但我得到了这个错误:

Ignoring exception in command changewelcomechannel:
2021-11-24T09:38:41.247854+00:00 app[worker.1]: Traceback (most recent call last):
2021-11-24T09:38:41.247927+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2021-11-24T09:38:41.247928+00:00 app[worker.1]:     ret = await coro(*args, **kwargs)
2021-11-24T09:38:41.247943+00:00 app[worker.1]:   File "/app/bot.py", line 352, in changewelcomechannel
2021-11-24T09:38:41.247943+00:00 app[worker.1]:     json.dump(welcomechannel, f, indent=4)
2021-11-24T09:38:41.247957+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/json/__init__.py", line 179, in dump
2021-11-24T09:38:41.247958+00:00 app[worker.1]:     for chunk in iterable:
2021-11-24T09:38:41.247976+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/json/encoder.py", line 431, in _iterencode
2021-11-24T09:38:41.247976+00:00 app[worker.1]:     yield from _iterencode_dict(o, _current_indent_level)
2021-11-24T09:38:41.248004+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
2021-11-24T09:38:41.248005+00:00 app[worker.1]:     yield from chunks
2021-11-24T09:38:41.248018+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/json/encoder.py", line 340, in _iterencode_dict
2021-11-24T09:38:41.248019+00:00 app[worker.1]:     raise ValueError("Circular reference detected")
2021-11-24T09:38:41.248041+00:00 app[worker.1]: ValueError: Circular reference detected

我在本地没有完整的错误代码,所以我必须复制并粘贴我能找到的heroku错误代码。

我在另一个命令中使用了相同的结构,效果很好。我对在python中使用json模块没有太多经验,所以如果有人能帮忙,那就太棒了。非常感谢

编辑:我终于意识到我做错了什么,我应该在中使用另一个变量

welcomechannel[str(ctx.guild.id)] = welcomechannel# this is supposed to be the new variable#

错误是提到了一个循环引用,这是您试图引用其内部对象的地方。当你写时,你会这样做

welcomechannel[str(ctx.guild.id)] = welcomechannel

正如您所看到的,您正在将对象welcomechannel的属性指定给它自己。

也许你想尝试重命名函数参数welcomechannel,因为它目前不明确,即在函数开始后几行,您用json对象覆盖它。您也可以在此处重命名变量:

welcomechannel = json.load(f)

相关内容

最新更新