CLOSED TypeError: 'bool' object is not subscriptable in discord.py

CLOSED TypeError: 'bool' object is not subscriptable in discord.py

我正在制作一个私人的 discord 机器人,我是编码新手。每次我尝试 运行 这个命令 .balance 它都会给我错误:TypeError: 'bool' object is not subscriptable。我不知道这意味着什么,我需要帮助。这是我的代码:

#balance
@client.command(aliases=['bal', 'b'])
async def balance(ctx):
  await open_account(ctx.author)
  users = await get_bank_data()
  user = ctx.author
  wallet_amt = users[str(user.id)]['wallet']
  bank_amt = users[str(user.id)]['bank']
  
  embedbalance = discord.Embed(title = f'''{ctx.author.name}'s balance''',color = 0x00ff00)
  embedbalance.add_field(name = 'Wallet balance:',value = wallet_amt)
  embedbalance.add_field(name = 'Bank balance:',value = bank_amt)

  await ctx.send(embed = embedbalance)

更新:当我尝试执行 .beg 时,我遇到了同样的错误。而且它也不会上传 .json 文件。这是代码:

#beg
@client.command()
async def beg(ctx):
  await open_account(ctx.author)

  user = ctx.author
  users = await get_bank_data()

  earnings = random.randrange(100)
  embedbeg = discord.Embed(title = f'''Someone gave you {earnings} coins!''',color = 0x00ff00)
  await ctx.send(embed = embedbeg)
  
  users[str(users)]['wallet'] += earnings

  with open('mainbank.json', 'w') as f:
    json.dump(users,f)

这是gat银行数据代码:

async def open_account(user):
  with open('mainbank.json', "r") as f:
    users = json.load(f)
    
  if str(user.id) in users:
    return False
  else:
    users[str(user.id)] = {}
    users[str(user.id)]['wallet'] = 100
    users[str(user.id)]['bank'] = 0

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

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

问题是您在 get_bank_data 中使用了 return True。所以当你说

users = await get_bank_data()

您正在将 users 设置为值 True,当您要求 users[str(user.id)].

时会出现错误

将 return 语句替换为

return users

你的代码应该可以工作。