【问题标题】:discod py json user not defined issuediscord py json用户未定义问题
【发布时间】:2022-01-20 14:55:36
【问题描述】:

我想用银行账户制作一个机器人,但它说用户未定义。我看了很多教程并四处搜索,但找不到解决方案:O 我是新手,真的不擅长 python,我将不胜感激。

@client.command()
async def balance(ctx):
  user = ctx.author
  await open_account(user)
  users = await get_bank_data(ctx)

  wallet_amt = users[str(user.id)]["wallet"]
  bank_amt = users[str(user.id)]["bank"]
  await ctx.send(bank_amt)


async def open_account(ctx, user):

  users = await get_bank_data(ctx)
  

  with open("mainbank.json", "r") as f:
    users = json.load(f)

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

  with open("mainback.json", "w") as f:
    json.dump(users,f)
    return True
 
async def get_bank_data(ctx):
  with open("mainbank.json", "r") as f:
    users = json.load(f) 
  return users

【问题讨论】:

  • 请提供完整的错误描述。

标签: python json discord discord.py


【解决方案1】:

不确定该代码中的“用户”未定义错误。但是我发现里面还有很多其他的问题。

  1. await open_account(user),此行不正确。 open_account 的定义清楚地表明它需要两个参数,但你只传递一个参数给它。这应该抛出“缺少一个必需的位置参数'用户'”,这也可能是您遇到的错误
  2. with open("mainback.json", "w") as f:我很确定你在这里打错字了,'mainbank' 而不是 'mainback'。
  3. async def get_bank_data(ctx),这个函数显然有一个参数,但没有使用它。我相信您知道该函数不一定需要调用参数。我也不明白为什么这个函数不需要它是协程。
  4. 同样,open_account 也不必是协程。它还需要一个ctx,这是非常无用的,因为它只是使用它来传递给get_ban_data,这是无用的,因为函数不使用它,如第 3 点所述。

【讨论】:

    猜你喜欢
    • 2021-04-23
    • 2021-02-24
    • 2021-04-24
    • 2019-06-16
    • 1970-01-01
    • 2022-01-13
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多