我的警告命令不工作



(Replit.com)我试图使用警告命令,但每次我尝试,有一个错误:TypeError: '命令'对象是不可订阅的

with open('warns.json', encoding='utf-8') as f:
try:
report = json.load(f)
except ValueError:
report = {}
report['users'] = []
@client.command(pass_context=True)
@commands.has_permissions(manage_roles=True, ban_members=True)
async def warn(ctx, user: discord.User, *reason: str):
if not reason:
await ctx.send("Please provide a reason")
return
author = ctx.author
reason = ' '.join(reason)
embed = discord.Embed(title=f'{author.name} warned {user.name} for')
for current_user in report['users']:
if current_user['name'] == user.name:
current_user['reasons'].append(reason)
break
else:
report['users'].append({
'name': user.name,
'reasons': [
reason,
]
})
with open('warns.json', 'w+') as f:
json.dump(report, f)
await ctx.send(embed=embed)

我得到的错误信息是:

File "main.py", line 411, in warn 
for current_user in report['users']: 
TypeError: 'Command' object is not subscriptable 

问题是,除了report字典之外,您还有一个名为report的命令,因此要修复此错误,只需重命名全局变量或命令。此外,您应该根据用户的id (current_user.id而不是current_user.name)来保存用户,而不是按名称保存用户,因为Nitro用户可以更改其标识符以避免任何警告。

最新更新