(Replit.com) I tried to use the warn command but every time i try, there is a error: TypeError: ‘Command’ object is not subscriptable
JavaScript
x
30
30
1
with open('warns.json', encoding='utf-8') as f:
2
try:
3
report = json.load(f)
4
except ValueError:
5
report = {}
6
report['users'] = []
7
@client.command(pass_context=True)
8
@commands.has_permissions(manage_roles=True, ban_members=True)
9
async def warn(ctx, user: discord.User, *reason: str):
10
if not reason:
11
await ctx.send("Please provide a reason")
12
return
13
author = ctx.author
14
reason = ' '.join(reason)
15
embed = discord.Embed(title=f'{author.name} warned {user.name} for')
16
for current_user in report['users']:
17
if current_user['name'] == user.name:
18
current_user['reasons'].append(reason)
19
break
20
else:
21
report['users'].append({
22
'name': user.name,
23
'reasons': [
24
reason,
25
]
26
})
27
with open('warns.json', 'w+') as f:
28
json.dump(report, f)
29
await ctx.send(embed=embed)
30
The error message I get is:
JavaScript
1
4
1
File "main.py", line 411, in warn
2
for current_user in report['users']:
3
TypeError: 'Command' object is not subscriptable
4
Advertisement
Answer
The problem is, that aside the report
dictionary, you also have a command called report
, so to fix this error, simply rename either the global variable or the command.
Also instead of saving the users by name, you should save them by their id (current_user.id
instead of current_user.name
), because Nitro users can change their discriminator to avoid any warns.