首页 > 解决方案 > 我的警告命令不起作用 discord.py

问题描述

(Replit.com)我尝试使用警告命令,但每次尝试时都会出现错误:TypeError:'Command' object is not subscriptable

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 

标签: pythondiscord.py

解决方案


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


推荐阅读