discord.py KeyError with warnings command and mongodb
Question:
I’ve been constantly getting this error whenever I try to use this command and I’ve been testing around with it and I still can’t figure out what the issue is despite looking through other questions.
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
Here’s my code
@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)
If anyone knows please help me I’ve been dealing with this issue for a while
Answers:
(not enough reputation points to comment)
but the error is saying that whatever you are trying to index, looks like it’s warn in here, you are probably expecting a dictionary that you would index with "reason" but instead warn is a string, i suggest you try printing warn and see what you are actually trying to index
I’ve been constantly getting this error whenever I try to use this command and I’ve been testing around with it and I still can’t figure out what the issue is despite looking through other questions.
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
Here’s my code
@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)
If anyone knows please help me I’ve been dealing with this issue for a while
(not enough reputation points to comment)
but the error is saying that whatever you are trying to index, looks like it’s warn in here, you are probably expecting a dictionary that you would index with "reason" but instead warn is a string, i suggest you try printing warn and see what you are actually trying to index