Я пытаюсь создать эконом-бота discord.py, поэтому я сначала начал с валюты.Я работаю с кем-то, чтобы создать систему, в которой вы можете добавлять кому-то монеты и видеть, сколько у вас монет, но я получаю эти ошибки.
Это для экономичного бота discord.py.Я новичок в переписывании, поэтому я, вероятно, допустил глупую ошибку, но не могу ее найти.
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)
Это сообщение об ошибке, и я не могу понять, почему.
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'
Извините за код спама.