CLOSED 类型错误:“bool”对象在discord.py 中不可订阅

发布于 2025-01-14 19:55:21 字数 1690 浏览 5 评论 0原文

我正在制作一个私人不和谐机器人,而且我对编码很陌生。每次我尝试运行此命令 .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

Im making a private discord bot and im new to coding. Every time i try to run this command .balance it gives me the error: TypeError: 'bool' object is not subscriptable. I dont know what this means and i need help with it. Here is my code:

#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)

Update: When i try to do .beg i get the same error to. And it also doesnt upload the .json file. Here is that code:

#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)

Here is the gat bank data code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洛阳烟雨空心柳 2025-01-21 19:55:21

问题是您在 get_bank_data 中使用了 return True。因此,当您说

users = await get_bank_data()

You're set users to the value True 时,当您请求 users[str(user.id)] 时,会出现错误

将该 return 语句替换为

return users

并且您的代码应该可以工作。

The problem is that you're using return True in get_bank_data. So when you say

users = await get_bank_data()

You're setting users to the value True, which then gives you an error when you ask for users[str(user.id)].

Replace that return statement with

return users

And your code should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文