0

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)
ColeTMK
  • 96
  • 1
  • 10
  • Tuples aren't a valid method of indexing. – 12944qwerty Jun 04 '21 at 01:31
  • @12944qwerty What are Tuples? – ColeTMK Jun 04 '21 at 01:37
  • Tuples are immutable data structures. Please take a look at the [docs](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) to learn what they are before you start with discordpy. It is highly recommended to learn basic programming skills, including OOP when using discordpy. – 12944qwerty Jun 04 '21 at 02:25
  • Is this answer to the question helping you? [Warn command not executing whatsoever](https://stackoverflow.com/questions/64127326/warn-command-not-executing-whatsoever/64149480#64149480) – Dominik Jun 04 '21 at 09:17

0 Answers0