【问题标题】:Discord.py give commandDiscord.py 给出命令
【发布时间】:2020-12-21 19:01:24
【问题描述】:

我正在尝试使用给定命令创建积分系统,例如,如果用户完成挑战,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

这是给定命令:

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

(抱歉信息不足)

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案1】:

    如果 users 是一个列表,你不能像字典一样索引它。

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

    您应该有某种形式的 ID 和他们的用户 ID。

    这都是假设您的 JSON 文件看起来像这样。

    {“points”: []}
    

    【讨论】:

    • 你好@StarbuckBarista,我试着做你的建议,但我想不通!如果我做错了什么,你能告诉我吗?我将在下一条评论中发布代码
    • @client.command(pass_context = True) async def give(ctx, member, *, amount): async def add_points(users, member, amount): for user in users: if user['id'] == member.id: user['programming_points'] += amount with open('users.json', 'r') as f: users = json.load(f) await update_data(users, member) await add_points(users, member, amount) with open('users.json', 'w') as f: json.dump('users', 'f')
    • async def update_data(users, user): if not user.id in users: users[user.id] = {} users[user.id]['programming_points'] = []
    • 你应该看看我关于制作货币系统的另一篇文章。 stackoverflow.com/questions/63625197/…
    • 非常感谢,我会尽快检查,如果没问题我会更新你看看它是否有效
    猜你喜欢
    • 2021-10-03
    • 2020-12-30
    • 2021-06-29
    • 2021-03-26
    • 2021-08-28
    • 2021-01-22
    • 2021-03-30
    • 2021-05-17
    • 2021-01-15
    相关资源
    最近更新 更多