In my warn command, I'm trying to log the warn in a json file. I am getting a KeyError as follows...
Traceback (most recent call last): File "C:\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke await ctx.command.invoke(ctx) File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: ('834914066040356934', '745371515348058222', 'test')
My strings are working because im getting the correct 3 outputs as you can see above. How can I make this KeyError go away? Thanks!
@client.command()
@commands.has_permissions(manage_messages=True)
async def warn(ctx, member:discord.Member, reason=None):
if member == ctx.author.id:
await ctx.send(f'{ctx.author.mention}, you cannot warn yourself!')
return
if reason == None:
await ctx.send(f'{ctx.author.mention}, you need to provide a reason!')
return
embed=discord.Embed(title=f"{member.name} has been warned!", color=0xFF0000)
embed.add_field(name="Reason:", value=f'{reason}', inline=False)
embed.add_field(name="Moderator:", value=f'{ctx.author.mention}', inline=False)
embed.set_thumbnail(url=member.avatar_url)
embed.timestamp = datetime.datetime.utcnow()
await ctx.send(embed=embed)
with open('warns.json', 'r', encoding='utf-8') as fp:
warn = json.load(fp)
warn[str(ctx.guild.id), str(member.id), (reason)]
try:
warn[str(ctx.guild.id), str(member.id), str(reason)]
except KeyError:
new = {str(ctx.guild.id), str(member.id), str(reason)}
warn.update(new)
with open('warns.json', 'w', encoding='utf-8') as fpp:
json.dump(warn, fpp, indent=2)