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

을 가지고 있지 않았 전체 오류 코드 로컬 그래서 나는 복사 및 붙여넣기만큼의 계정을 오류 코드가 내가 수 찾을 수 있습니다.

내가 사용하는 동일한 구조에서 다른 명령했습니다. 나는 그렇지 않을 경험과 함께 사용하여 json 모듈에서,그렇게 만약 누군가가 도움이 될 수,그것은으로 인해 발생하는 것으로 나타났다 많은 감사

편집:마지막으로 깨달은 내가 무엇을 잘못했는지,나는 했는데 사용하는 다른 변수

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 그것의 현재 모 i.e 몇 줄 후기능이 시작되 당신은 그것을 덮어 json 개체입니다. 당신은 또한 변수 이름을 바꾸기:

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

하지만 나는 무엇을 해결하는 동안 오류가 여전히 추출한 정보?
Aadit John

또한 사용되는 동일한 구조를 가진 다른 명령하고 일했다. 그것은 좋아하면 당신 둘 사이의 차이점에 대해 설명합? 이것은 명령을 클라이언트입니다.명령(name="changeprefix")비동기 def changeprefix(ctx,접두사):open('접두어.json','r')f:국번=json.드(f)접두사[str(ctx.연히 스트러밍도 가능합니다.id)]=prefix open('접두어.json','w')f:json.덤프(접두사,f,indent=4)기 ctx.send(f"접두사를 지금 {prefix}!")
Aadit John

차이가 있다는 것이 새를 들어,당신은 당신을 사용하여 기능 매개 변수는'접두사',그리고 할당하의 속성 json'접두사'. I'll 업데이트 내 대답이다.
ttrss

Ah,당신을 감사하나 그것은 또 다른 오류가 있습니다. 이 시 TypeError. 나 업데이트 내 질문을 보여주 오류가 있습니다.
Aadit John

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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