体验(XP)不适用于所有用户JSON Discord.PY [英] Experience (XP) not working for all users JSON Discord.PY
问题描述
我想为在一个大约50至60个人在其中输入内容的房间中输入的邮件提供积分。它将首次将用户添加到JSON文件,但不会为他们输入的消息添加更多点。我再次对其进行了测试,只有一个用户从他们键入的消息中获得积分,其余用户保持不变。以下是代码:
I'm trying to give points for messages typed in a room that has around 50-60 people that type in it. It will add the user to the JSON file the first time, but it won't add any more points for the messages they type. I tested it again and only one user was getting points for the messages they typed and the rest remained the same. Here is the code:
@client.event
async def on_message(message):
if message.content.lower().startswith('!points'):
await client.send_message(message.channel, "You have {} points!".format(get_points(message.author.id)))
user_add_points(message.author.id,1)
def user_add_points(user_id: int, points: int):
if os.path.isfile("users.json"):
try:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id]['points'] += points
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
except KeyError:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id] = {}
users[user_id]['points'] = points
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent = 4)
else:
users = {user_id:{}}
users[user_id]['points'] = points
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
def get_points(user_id: int):
if os.path.isfile('users.json'):
with open('users.json', 'r') as fp:
users = json.load(fp)
return users[user_id]['points']
else:
return 0
推荐答案
我们只需要读取一次文件,然后在需要时保存对文件的修改。我没有注意到会导致上述行为的任何逻辑错误,因此这可能是关于允许您的漫游器查看哪些消息的权限问题。为了方便调试,我简化了代码,并添加了一些打印内容以跟踪发生的情况。我还在 on_message
中添加了一个防护措施,以阻止机器人对其自身做出响应。
We should only need to read the file once, and then just save our modifications to the file when we need to. I didn't notice any logical errors that would lead to the behavior described, so it may be a permissions issue regarding what messages your bot is allowed to see. To facilitate debugging, I've simplified your code and added some prints to track what's going on. I also added a guard in on_message
to stop the bot from responding to itself.
import json
import discord
client = discord.Client()
try:
with open("users.json") as fp:
users = json.load(fp)
except Exception:
users = {}
def save_users():
with open("users.json", "w+") as fp:
json.dump(users, fp, sort_keys=True, indent=4)
def add_points(user: discord.User, points: int):
id = user.id
if id not in users:
users[id] = {}
users[id]["points"] = users[id].get("points", 0) + points
print("{} now has {} points".format(user.name, users[id]["points"]))
save_users()
def get_points(user: discord.User):
id = user.id
if id in users:
return users[id].get("points", 0)
return 0
@client.event
async def on_message(message):
if message.author == client.user:
return
print("{} sent a message".format(message.author.name))
if message.content.lower().startswith("!points"):
msg = "You have {} points!".format(get_points(message.author))
await client.send_message(message.channel, msg)
add_points(message.author, 1)
client.run("token")
这篇关于体验(XP)不适用于所有用户JSON Discord.PY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!