python - 我的警告命令不起作用 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
解决方案
问题是,除了report
字典之外,您还有一个名为 的命令report
,因此要修复此错误,只需重命名全局变量或命令即可。此外,不要按名称保存用户,而应按他们的 id(current_user.id
而不是current_user.name
)保存他们,因为 Nitro 用户可以更改他们的鉴别器以避免任何警告。
推荐阅读
- reactjs - react-redux 在react-reducer中刷新网页时刷新商店值?
- javascript - Select 查询是否使用 Firestore 上的所有文档大小?
- autodesk-forge - 使用 Forge API 查看 Revit 绘图视图
- c - 如何在 C 中将 RGB565 转换为 HSL 颜色
- redhat - xdotool 无法在 redhat 上检测到 headless firefox 没有 GUI
- ios - 如何使用 SwiftUI 观察 TextField 值并结合?
- java - 我正在尝试在 java 中合并两个 JsonObjects
- javascript - 刷新页面后的粗体活动菜单
- docker - 配置文件更改为 VerneMQ docker 映像
- spring - 为什么 Querydsl 总是要求连接是事务性的?