Valueerror异常时使用json模块discord.py

0

的问题

我的意图是设置一道欢迎的消息时立即机器人加入,并能够改变它使用的命令分配。 这是我的代码:

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

我没有完全错误代码当地所以我不得复制和粘贴的选择错误代码我可以找到。

我用同样的结构中另一个命令和它的工作很好。 我不是那么有经验的与使用id模块在蟒蛇,所以如果有人可以帮助,这将是可怕的。 非常感谢

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

welcomechannel[str(ctx.guild.id)] = welcomechannel# this is supposed to be the new variable#
discord.py python
2021-11-24 06:31:21
1

最好的答案

0

错误是提一个圆形的参考,这是你试图参考的对象,你是内部的。 你这样做的时候你写:

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

正如你可以看到,你被分配财产的对象 welcomechannel 它本身。

玛贝你想试试重新命名的功能参数 welcomechannel 因为它是目前含糊不清。e几条线功能后开始复盖它与json对象。 你也可以重新命名的可变:

welcomechannel = json.load(f)
2021-11-25 10:43:15

但是我该做什么来纠正这一错误,同时仍然提取的信息?
Aadit John

我还使用了相同的结构与另一个命令和它的工作。 它是好的,如果你解释的差异? 这是命令客户。命令(name="changeprefix")异步def changeprefix(ctx、前缀):与开放('前缀。json','r')为f:前缀=手机中。负载(f)前缀[str(环磷酰胺.协会。id)]=前缀与开('前缀。json','w')为f:手机中。dump(前缀,f,缩进=4)等待环磷酰胺.发送(f"前缀是现在 {prefix}!")
Aadit John

不同的是,在这个新例子,你使用的功能参数'prefix',并将其分配给一个酒店的java object'前缀'. 我要更新我的答案。
ttrss

啊,谢谢你,但是它想出了另一个错误。 这个时候,一个类型错误. 我已经更新我的问题显示的错误。
Aadit John

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................