'KeyError in Discord.PY when the key exists
So, I'm currently creating a currency for my Discord bot, and I'm trying to make a give command so that users can give the currency to other users. I keep getting a KeyError, yet the user ID is already in the json file. Could someone take a look and see if anything can be done? I've seen so many keyerror problems with solutions yet none of them have been of use.
def addCoins(users, member, amount):
try:
users[member.mention] += amount
except KeyError:
users[member.mention] = amount
with open('user_coins.json', 'w') as f:
json.dump(users, f)
`@bot.command()
async def mycoins(ctx):
user = ctx.author
with open('user_coins.json', 'r') as f:
userCoinDic = json.load(f)
users = userCoinDic.keys()
if user.mention in users:
embcoins = discord.Embed(title=f"Your Coins, {user}", description=f"You have {userCoinDic[user.mention]} coins.", color=0xFF8300, inline=False)
await ctx.send(embed=embcoins)
else:
await ctx.send("You don't seem to have any coins...")
def giveCoins(users, member, amount):
try:
users[member] += int(amount)
except KeyError:
users[member] = int(amount)
@bot.command()
async def give(ctx, amount, userToGive):
user = ctx.author
with open('user_coins.json', 'r') as f:
userCoinDic = json.load(f)
giveCoins(userCoinDic, userToGive, amount)
userCoinDic[user.mention] -= int(amount)
await ctx.send(f"Successfully given {userToGive} {amount} lasagna coins")
with open('user_coins.json', 'w') as f:
json.dump(userCoinDic, f)
The json file:
{"<@800485638583091210>": 130, "<@496433501751607316>": 35, "<@805067781769789482>": 30, "<@266012778358243339>": 5, "<@747549629335339149>": 35, "<@761003497239609354>": 100, "<@!878371788852166697>": 55}
And the error:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 189, in mycoins
embcoins = discord.Embed(title=f"Your Coins, {user}", description=f"You have {userCoinDic[user.mention]} coins.", color=0xFF8300, inline=False)
KeyError: '<@878371788852166697>'
Solution 1:[1]
As I have Noticed in the json file the id that's (the last one) giving an error has a typo in it instead of:
"<@!878371788852166697>": 55
It has to be:
"<@878371788852166697>": 55
Solution 2:[2]
The member.mention is returning <@878371788852166697> when in the JSON it is stored as <@!878371788852166697>, hence the KeyError...
To avoid such errors, do not use mentions as keys as mentions returned can change; it will generally be <@id> but can sometimes be <@!id> (which means that user has a display name different to their actual username) so there is inconsistency there. Instead of using mentions as the key, I highly suggest using ids because Discord IDs can never change, hence becoming more reliable and easier to work with.
You can access the ID of a Member/user Object by using the .id attribute (e.g. member.id).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ouflak |
| Solution 2 | Syntax? |
