Im writing a discord.py bot and this is the warns command, it gets warns logged by the warn cmd!
Why do I get a key error? My JSON:
{"777618366894571530": {"1": "ROMO charged by ROMO#6714 for demo"}}
My script to get the warns:
if str(guildid) in warns:
for value in warns.items():
warnval = warns[str(guildid)][value]
if user in warnval:
embed.add_field(name=f"``Warn {value}``", value=f"{warnval}", inline=True)
else:
await ctx.send("Guild has no warns!")
My full warns cmd to get warns from the json:
@command()
@cooldown(9, 20, BucketType.user)
async def warns(self, ctx, user : discord.Member):
guild = user.guild
guildid = user.guild.id
userid = user.id
print("LOADING")
warns = {}
guildid = ctx.guild.id
placing = (len(warns)+1)
with open("./warns.json","r") as f:
warns = json.load(f)
embed = discord.Embed(title=f"Warns", description= f"Warns for user {user}", color=0x00ff00)
if str(guildid) in warns:
for value in warns.items():
warnval = warns[str(guildid)][value]
if user in warnval:
embed.add_field(name=f"``Warn {value}``", value=f"{warnval}", inline=True)
else:
await ctx.send("Guild has no warns!")
embed.set_footer(text=f"These are {user.display_name}'s warns.")
await ctx.send(embed=embed)
question from:
https://stackoverflow.com/questions/65848254/encountering-a-weird-key-error-is-this-something-to-do-with-json-or-my-statemen 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…