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

मैं नहीं था पूरा त्रुटि कोड स्थानीय रूप से तो मैं कॉपी और पेस्ट heroku त्रुटि कोड मुझे मिल सकता है.

मैं प्रयोग किया जाता एक ही संरचना में एक और कमांड और यह अच्छी तरह से काम किया. मैं नहीं हूँ तो अनुभवी के साथ का उपयोग कर 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 करने के लिए ही है ।

Mabey आप चाहते हैं की कोशिश करने के लिए नाम बदलने के समारोह पैरामीटर welcomechannel के रूप में यह वर्तमान में अस्पष्ट मैं.ई कुछ लाइनों के बाद समारोह शुरू होता है, आप इसे मिटाकर के साथ एक json वस्तु है । तुम भी का नाम बदलें चर यहाँ है:

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

लेकिन क्या मैं इसे ठीक करने के लिए जबकि अभी भी निकालने की जानकारी?
Aadit John

मैं भी प्रयोग किया जाता एक ही संरचना के साथ एक अन्य आदेश और यह काम किया. यह ठीक है अगर आप अंतर की व्याख्या? यह आदेश है ग्राहक.आदेश(नाम="changeprefix") async डेफ changeprefix(ctx, उपसर्ग): के साथ('उपसर्ग.json', 'r') के रूप में एफ: उपसर्ग = json.लोड(एफ) उपसर्ग[एसटीआर(ctx.गिल्ड.आईडी)] = उपसर्ग के साथ ('उपसर्ग.json', 'w') के रूप में एफ: json.डंप(उपसर्ग एफ, indent=4) का इंतजार ctx.भेजें(एफ"उपसर्ग है अब {उपसर्ग}!")
Aadit John

अंतर यह है कि इस नए उदाहरण के लिए, आप उपयोग कर रहे हैं, समारोह पैरामीटर 'उपसर्ग', और यह बताए के लिए एक संपत्ति के json वस्तु 'उपसर्ग'. मैं अद्यतन हूँ मेरा जवाब.
ttrss

आह, धन्यवाद, लेकिन यह के साथ आया था, एक और त्रुटि है । इस समय, एक TypeError. मैं अद्यतन किया है मेरे सवाल को दिखाने के लिए त्रुटि ।
Aadit John

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

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

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में