CLOSED 类型错误:“bool”对象在discord.py 中不可订阅
我正在制作一个私人不和谐机器人,而且我对编码很陌生。每次我尝试运行此命令 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在
get_bank_data
中使用了return True
。因此,当您说You're set
users
to the valueTrue
时,当您请求users[str(user.id)] 时,会出现错误
。将该 return 语句替换为
并且您的代码应该可以工作。
The problem is that you're using
return True
inget_bank_data
. So when you sayYou're setting
users
to the valueTrue
, which then gives you an error when you ask forusers[str(user.id)]
.Replace that return statement with
And your code should work.