【发布时间】:2020-11-09 20:43:45
【问题描述】:
Here is a photo of json file. 我想在discord.py中做关卡系统,这是我的代码:
import discord
from discord.ext import commands
from discord.utils import get
import asyncio
import json
TOKEN = 'xxxx'
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_message(message):
with open(r'C:\\Users\\asus\\lvl.json', 'r') as f:
users = json.load(f)
user = discord.User
async def update_data(users, user: discord.User):
if not user in users:
users[user] = {}
users[user]['exp'] = 0
users[user]['lvl'] = 1
async def add_exp(users, user: discord.User, exp: float):
users[user][exp] = exp + exp
async def add_lvl(users, user: discord.User):
exp = users[user]['exp']
lvl = users[user]['lvl']
if exp > lvl:
await message.channel.send(f'{message.author.mention} достиг {str(lvl)} уровня!')
lvl = lvl + 1
exp = 0
await update_data(users, str(message.author.id))
await add_exp(users, str(message.author.id), float(0.1))
await add_lvl(users, str(message.author.id))
users[user]['exp'] = 0
users[user]['lvl'] = lvl + 1
with open('C:\\Users\\asus\\lvl.json', 'w') as f:
json.dump(users, f)
await bot.process_commands(message)
# RUN
bot.run(TOKEN)
但是我有这个错误,我认为它在 json 文件中存在一些问题,因为他将 '0,1' 复制为 str。请不要介意,请检查此错误并heeeeelp我请):
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 205, in on_message
users = json.load(f)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 2 column 1 (char 44)
请帮忙)
【问题讨论】:
标签: python python-3.x bots discord.py discord.py-rewrite