【问题标题】:I'm getting a discord.py error saying AttributeError: 'str' object has no attribute 'read'我收到一个 discord.py 错误,说 AttributeError: 'str' object has no attribute 'read'
【发布时间】:2021-05-24 09:06:58
【问题描述】:
async def open_account(user):

    users = await get_bank_data()

    if (user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 1000
        users[str(user.id)]["bank"] = 0

    with open("mainbank.json", "w") as f:
        json.dump(users,f)
    return True

完整的错误是:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, 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: AttributeError: 'str' object has no attribute 'read'

【问题讨论】:

  • 哪一行引发了这个错误? get_bank_data 是做什么的?
  • 用户[str(user.id)] = {} 用户[str(user.id)]["wallet"] = 1000 个用户[str(user.id)]["bank"] = 0 这些并注意银行数据youtube.com/watch?v=HPaadO_sRD4&t=129s
  • 为什么会引发这个错误? users 是什么?看起来它的行为应该像 dict,不会引发这种异常
  • 您能否包含引发此错误所需的确切代码?我不确定提供的堆栈跟踪是否完整,代码也不完整。请提供MCVE

标签: python string discord discord.py attributeerror


【解决方案1】:

仅从您的代码 sn-p 和我还观看的视频中,我发现了以下错误:

if (user.id) in users:

这也必须是str,所以将其更改为if str(user.id) in users:

您的else: 论点也有某种错误。您将1000 设置为"wallet" 数量,但它必须是0

    else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 0
        users[str(user.id)]["bank"] = 0

如果它仍然抛出错误,您的代码中的其他内容一定是错误的。

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    相关资源
    最近更新 更多