Reputation: 11
I am trying to make a discord.py economy bot, so I first started with the currency. I am working with somebody make a system where you can add coins to somebody and see how many coins you have, but I am getting these errors.
This is for a discord.py economy bot. I'm new to rewrite, so I probably made a silly mistake, but I can't find it.
import discord
from discord.ext import commands
import json
import os
bot = commands.Bot(command_prefix="!")
token =
def user_add_coins(user_id: int, points: int):
if os.path.isfile("coins.json"):
try:
with open('coins.json', 'r') as fp:
users = json.load(fp)
users[user_id]['coins'] += points
with open('coins.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
except KeyError:
with open('coins.json', 'r') as fp:
users = json.load(fp)
users[user_id] = {}
users[user_id]['coins'] = points
with open('coins.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
else:
users = {"user_id": {}}
users[user_id]['coins'] = points
with open('coins.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
def get_points(user_id: int):
if os.path.isfile('coins.json'):
with open('coins.json', 'r') as fp:
users = json.load(fp)
return users[user_id]['coins']
else:
return 0
@bot.event
async def on_message(message):
user_add_coins(message.author.id, 1)
@bot.command()
async def coins(ctx):
coins = get_points(ctx.author.id)
await ctx.send(f"Your Coins Is `{coins}` !")
@bot.group()
async def add(ctx):
if ctx.command_invk is None:
return
@add.command()
async def coins(ctx, args: int, member: discord.Member):
if ctx.author.id in owners:
user_add_coins(member.id, int(args))
await ctx.send(f"Sussces Add {args} to {member.mention} !")
bot.run(token)
This is the error message, and I can't seem to figure out why.
Traceback (most recent call last):
File "C:/Users/MYNAME/PycharmProjects/Faction Discord Bot/Faction Discord Test Bot.py", line 14, in user_add_coins
users[user_id]['coins'] += points
KeyError: 474744664449089556
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 255, in _run_event
await coro(*args, **kwargs)
File "C:/Users/MYNAME/PycharmProjects/Faction Discord Bot/Faction Discord Test Bot.py", line 40, in on_message
user_add_coins(message.author.id, 1)
File "C:/Users/MYNAME/PycharmProjects/Faction Discord Bot/Faction Discord Test Bot.py", line 23, in user_add_coins
json.dump(users, fp, sort_keys=True, indent=4)
File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\json\__init__.py", line 179, in dump
for chunk in iterable:
File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\json\encoder.py", line 430, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\json\encoder.py", line 353, in _iterencode_dict
items = sorted(dct.items(), key=lambda kv: kv[0])
TypeError: '<' not supported between instances of 'int' and 'str'
Sorry for the code spam.
Upvotes: 1
Views: 4840
Reputation: 57033
Some of the users in users
have numeric keys and some others have string keys. By passing sort_keys=True
to dump()
you insist that the users are sorted by their keys, which in this case is not possible: you cannot mix apples and oranges. Solution: remove that option.
json.dump(users, fp, indent=4)
Upvotes: 6