【问题标题】:Value error circular reference detected Python检测到值错误循环引用 Python
【发布时间】:2021-09-01 21:58:53
【问题描述】:

我在 discord.py 上写了一个简单的函数

我曾尝试使用在线数据库来托管机器人

import discord
from discord.ext import commands
from replit import db

client = discord.Client()
bot = commands.Bot(">", case_insensitive = True)

db['Plaksha'] = {'Server_Specific_Stuff':{'Channel for commands':0}}

chan_for_commands = 1

@bot.command()
async def set_commands_channel(ctx):
  global chan_for_commands
  
  print(chan_for_commands)

  temp = ctx.message.channel

  db['Plaksha']['Server_Specific_Stuff']['Channel for commands'] = temp
  
  chan_for_commands = temp

  print(chan_for_commands)
  pass


token = ""

bot.run(token)

这个函数应该更新变量 chan_for 命令。 我尝试过 Replit.com 以外的其他客户端,但它不起作用!

有没有办法避免这个错误?如果是这样,是否有另一种方法可以在本地和在线数据库(数据库)中更新 chan_for_commands?

错误提示:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: Circular reference detected

【问题讨论】:

  • 你确定ctx.message.channel 只是一个标识符,而不是例如一个东西?也许你需要ctx.message.channel.id 或类似的?

标签: python json circular-reference repl.it


【解决方案1】:

我之前遇到过这个错误,解决方法是将变量更改为字符串。数据库不会接受未指定的值。

import discord
from discord.ext import commands
from replit import db

client = discord.Client()
bot = commands.Bot(">", case_insensitive = True)

db['Plaksha'] = {'Server_Specific_Stuff':{'Channel for commands':0}}

chan_for_commands = 1

@bot.command()
async def set_commands_channel(ctx):
  global chan_for_commands
  
  print(chan_for_commands)

  temp = ctx.message.channel

  db['Plaksha']['Server_Specific_Stuff']['Channel for commands'] = str(temp)
  
  chan_for_commands = temp

  print(chan_for_commands)
  pass


token = ""

bot.run(token)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多