首页 > 解决方案 > discord.py 中的 JSON RecursionError

问题描述

我在尝试运行此代码以获取用户不和谐数据时遇到 RecursionError:

@bot.command(name="create")
async def create(ctx):
  users = await open()
  # some other code

调用此函数:

def open(name="users.json", t="r"):
  with open(name, t) as f:
    return json.load(f)

收到以下错误:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 112, in create
    users = await open()
  File "main.py", line 54, in open
    with open(name, t) as f:
  File "main.py", line 54, in open
    with open(name, t) as f:
  File "main.py", line 54, in open
    with open(name, t) as f:
  [Previous line repeated 1483 more times]
RecursionError: maximum recursion depth exceeded

注意:这仅在我运行 create 命令时发生。

我猜这是因为 discords 的 api 试图永远打开 JSON 文件。但我真的不知道如何解决它。

标签: pythonjsondiscord.pyfilesystemsrecursionerror

解决方案


你调用了你的函数open并且你在它内部调用它,这称为递归,只需给它另一个名字,一切都应该工作。


推荐阅读