简体   繁体   English

Discord.py 给出命令

[英]Discord.py give command

i am trying to make a point system with a give command, like if a user completes a challenge a mod can give the user points, i already made a function for it to write the users ID and 0 points into a .json when they join (first box) but i can't seem to get the give command correctly(second box), and no errors show up, any help would be appreciated, thanks in advance :)我正在尝试使用给定命令制作积分系统,例如如果用户完成挑战,mod 可以给用户积分,我已经为它制作了一个函数,以便在他们加入时将用户 ID 和 0 积分写入 .json (第一个框)但我似乎无法正确获取命令(第二个框),并且没有出现错误,任何帮助将不胜感激,提前致谢:)

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

this function is supposed to add points:这个函数应该添加点:

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

and this is the give command:这是给命令:

@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')

(sorry for the lack of info) (抱歉缺少信息)

If users is a list, you can't just index it like a dictionary.如果 users 是一个列表,则不能像字典一样对其进行索引。

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

You should have some form of ID with their user id.你应该有某种形式的 ID 和他们的用户 ID。

This is all assuming your JSON file looks like this.这一切都假设您的 JSON 文件如下所示。

{“points”: []}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM