Discord.py Give命令

时间:2020-09-02 15:09:25

标签: python discord bots discord.py

我正在尝试使用Give命令制作积分系统,例如,如果用户完成挑战,那么mod可以给用户积分,我已经为它编写了一个函数,可以将用户ID和0点写入.json。当他们加入时(第一个方框),但我似乎无法正确地获得Give命令(第二个方框),并且没有出现错误,我们将不胜感激,在此先感谢:)

async def update_data(users, user): #
    if not user.id in users: #i called this in the member join event
      users[user.id] = {}
      users[user.id]['points'] = 0

该功能应该添加点:

async def add_points(ctx, users, user, pts): #I wrote that function at the beginning of the script
      users[user.id]['points'] += pts

这是Give命令:

@client.command(pass_context = True)
    async def give(ctx, member, amount):
      with open('users.json', 'r') as f:
        users =  json.load(f)
        await add_points(users, member, amount) 
      with open('users.json', 'w') as f:                      
        json.dump('users', 'f') 
      await ctx.send(f'given {member} {amount} programming points')

(很抱歉缺少信息)

1 个答案:

答案 0 :(得分:0)

如果用户是列表,则不能像字典一样将其编入索引。

for user in users:
    if user[“id”] == member.id:
        user[“points”] += AMOUNT

您应该具有其用户ID的某种形式的ID。

这全部是假设您的JSON文件如下所示。

{“points”: []}