ValueError เมื่อใช้ language ในศูนย์ควบคุม kde ในโมดูล discord.py

0

คำถาม

ความตั้งใจของฉันคือเพื่อตั้งค่าเป็นช่องสำหรับยินดีต้อนรัข้อความทันทีเมื่อตอนที่ bot จะมาร่วมกันและจะเป็นสามารถเปลี่ยนแปลงมันโดยใช้คำสั่งที่กำหนด. นี่คือรหัส:

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 เกิดข้อผิดพลาดรหัสฉันหามาได้

ฉันเคยเดียวกับโครงสร้างในอีกคำสั่งและมันได้ผลอย่างดี ฉันไม่ค่อยมีประสบการณ์กับการใช้ language ศูนย์ควบคุม kde ในโมดูลภาษาไพธอน,ดังนั้นถ้าใครบางคนสามารถช่วยมันคงจะเยี่ยมมาก ขอบคุณมาก

แก้ไข:ในที่สุดฉันก็รู้ว่าฉันทำอะไรผิดปกติฉันควรจะใช้ตัวแปรอื่นอยู่

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 เพื่อตัวมันเอง

Mabey คุณอยากจะลองเปลี่ยนชื่อฟังก์ชันพารามิเตอร์ welcomechannel อย่างที่มันเป็นปัจจุบันยังกำกวมเนื่องจาผมก็ไม่ต่างกันหรอกอีกไม่กี่บรรทัดหลังจากฟังก์ชันเริ่มต้นคุณเขียนทับมันหรือไม่ด้ว language เดาใจ คุณอาจเปลี่ยนชื่อตัวแปรได้ที่นี่:

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

แต่ฉันต้องทำอะไรเพื่อ rectify ข้อผิดพลาดขณะที่ยัง extracting ข้อมูล?
Aadit John

ฉันยังใช้คนเดียวกับโครงสร้างกับอีกคำสั่งและมันได้ผล มันโอเคถ้าเธออธิบายเรื่องความแตกต่าง? นี่เป็นคำสั่นลูกค้าของเราคำสั่ง(ชื่อ="changeprefix")async def changeprefix(ctx,ส่วนนำหน้า):กับเปิด('prefixes.language','f')เป็น f:prefixes=language.โหลด(f)prefixes[str(ctx.guild.หมายเลข)]=ส่วนนำหน้ากับเปิด('prefixes.language','เมน')เป็น f:language.ทิ้ง(prefixes f ระยะเยื้อง=4)จะรอ ctx.ส่ง(f"นำหน้าตอนนี้ {นำหน้า}!")
Aadit John

ความแตกต่างคือที่อยู่ใหม่ตัวอย่างเช่นคุณโดยใช้ฟังก์ชันพารามิเตอร์'นำหน้าด้วย'และกำลังกำหนดมันให้เป็นทรัพย์สินของ language วัตถุ'prefixes'. ฉันจะปรับปรุงคำตอบของฉันแล้ว
ttrss

โอ้ว,ขอบคุณ,แต่มันมากับอีกข้อผิดพลาด ครั้งนี้เป็น TypeError. ฉันต้องปรับปรุงของฉันคำถามเพื่อให้มีการแสดงข้อผิดพลาด
Aadit John

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่