Json modülünü kullanırken ValueError discord.py

0

Soru

Amacım, bot katıldığında hemen hoş geldiniz mesajları için bir kanal ayarlamak ve atanan komutu kullanarak değiştirebilmektir. Bu benim kodum:

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}**!"

ama bu hatayı alıyorum:

 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

Yerel olarak tam hata kodum yoktu, bu yüzden bulabildiğim heroku hata kodunu kopyalayıp yapıştırdım.

Aynı yapıyı başka bir komutta kullandım ve iyi çalıştı. Python'da json modülünü kullanma konusunda çok deneyimli değilim, bu yüzden birisi yardım edebilirse harika olurdu. Çok teşekkürler

düzenleme: Sonunda neyi yanlış yaptığımı anladım, başka bir değişken kullanmam gerekiyordu

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

En iyi cevabı

0

Hata, içinde bulunduğunuz nesneye başvurmaya çalıştığınız dairesel bir referanstan bahsediyor. Bunu yazarken yapıyorsun.:

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

Gördüğünüz gibi, nesnenin bir özelliğini atarsınız welcomechannel kendisi için.

Mabey işlev parametresini yeniden adlandırmayı denemek ister misiniz welcomechannel şu anda belirsiz olduğu için, yani işlev başladıktan birkaç satır sonra bir json nesnesiyle üzerine yazarsınız. Değişkeni buradan da yeniden adlandırabilirsiniz:

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

ancak, bilgileri ayıklarken hatayı düzeltmek için ne yapmalıyım?
Aadit John

Aynı yapıyı başka bir komutla da kullandım ve işe yaradı. Farklılıkları açıklarsan sorun olur mu? Bu komut istemcisidir.command (name="changeprefix") async def changeprefix( ctx, prefix): açık('öneklerle.json', 'r') olarak f: önekleri = json.yük (f) önekleri[str(ctx.guild.id)] = açık olan önek ('önekler.json', 'w') f: json olarak.döküm (önekler, f, girinti = 4) ctx'i bekliyor.gönder(f " öneki şimdi {önek}!")
Aadit John

Aradaki fark, bu yeni örnekte 'prefix' işlev parametresini kullanıyor olmanız ve bunu 'prefixes' json nesnesinin bir özelliğine atamanızdır. Cevabımı güncelleyeceğim.
ttrss

Teşekkür ederim ama başka bir hatayla karşılaştım. Bu sefer, bir TypeError. Hatayı göstermek için sorumu güncelledim.
Aadit John

Diğer dillerde

Bu sayfa diğer dillerde

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