discord.py KeyError with warnings command and mongodb



每当我尝试使用这个命令时,我一直在不断地得到这个错误,我一直在测试它,尽管通过其他问题查看,我仍然无法找出问题是什么。

Traceback (most recent call last):
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 190, in wrapped
ret = await coro(*args, **kwargs)
File "/home/container/main.py", line 398, in warnings
embed.add_field(name=f"{warn['reason']}", value=f"<@{warn['moderator_id']}>", inline=False)
TypeError: string indices must be integers
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 1347, in invoke
await ctx.command.invoke(ctx)
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 986, in invoke
await injected(*ctx.args, **ctx.kwargs)  # type: ignore
File "/home/container/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 199, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: string indices must be integers

这是我的代码

@client.hybrid_command(name = "warnings", with_app_command=True, description="View the warnings of a member", aliases=["punishments"])
@commands.is_owner()
async def warnings(ctx, member: discord.Member = None):
if member == None:
await ctx.reply("A Member is required")
else:
check = warndb.warn_logs.find_one({"user_id": member.id})
if check is None:
await ctx.reply("This user has no warns")
else:
embed = discord.Embed(color=embedcolor, title=f"{member.name}'s warnings")
print(check)
for warn in check:
embed.add_field(name=f"{warn['reason']}", value=f"<@{warn['moderator_id']}>", inline=False)
await ctx.send(embed=embed)

如果有人知道请帮助我,我一直在处理这个问题一段时间

(没有足够的声望点来评论)

但错误是说无论你想索引什么,这里看起来是警告,你可能期待一个字典你会索引"reason"但是warn是一个字符串,我建议你试着打印warn看看你实际要索引的是什么

最新更新