【问题标题】:Updating json via bot command通过 bot 命令更新 json
【发布时间】:2021-04-28 16:51:02
【问题描述】:

我正在尝试创建一个命令 (

@client.command()
@commands.has_permissions(administrator = True)
async def MF_add(ctx, user: discord.Member):
    with open ("MF Points.json", "r") as f:
        users = json.load(f)   
        
        await client.get_user(user_id)
        user = client.get_user(user_id)
        if user in users:
            users["{user.mention}"]["points"] += 1
            await ctx.message.channel.send(f"You have given {member.mention} 1 MF point.")
                
    with open("MF Points.json", "w") as f:
        json.dump(users, f, indent = 4)   

它没有给出任何错误,但它也完全没有响应。

感谢您的帮助!

【问题讨论】:

  • 等待消息发送真的在发送吗?
  • 不,当我在 Discord 中尝试机器人时没有任何反应
  • 我想你忘了加bot.command()
  • 糟糕!不,它就在那里。刚刚在我的帖子中输入错误。
  • 尝试使用if user in users.items():

标签: python json discord discord.py


【解决方案1】:

首先,我想建议您使用 SQLLite 而不是 JSON。 JSON 不是数据库。您真的应该避免使用它来存储经常更改的用户数据。

就您目前所拥有的而言,JSON 的使用类似于字典。它使用键和值。我非常怀疑 if 语句 if user in users: 是否返回 true。您应该检查(使用 print 语句)以查看 if 语句是否正常工作。

【讨论】:

  • 这应该是真的,因为他似乎在其中加载了一个字典,所以它会寻找密钥(虽然它不会因为多种其他原因而工作)
【解决方案2】:

您实际上并没有在这里发出命令,@commands.has_permissions(administrator = True) 不是您用来发出新命令的东西。你需要一个@bot.command() 在它前面,比如:

@bot.command()
@commands.has_guild_permissions(administrator=True)
async def MF_add(ctx, user: discord.Member):
    with open ("MF Points.json", "r") as f:
        users = json.load(f)   
        
        await client.get_user(user_id)
        user = client.get_user(user_id)
        if user in users:
            users["{user.mention}"]["points"] += 1
            await ctx.message.channel.send(f"You have given {member.mention} 1 MF point.")
                
    with open("MF Points.json", "w") as f:
        json.dump(users, f, indent = 4)

我相信这会解决你的问题,现在谈谈你的程序,因为它根本不会像你期望的那样运行(或者它只会出错):

  • client.get_user(userid) 不是异步的,不需要等待
  • 您似乎从未在任何地方定义 user_id(因为它是一个变量)
  • 首先为什么要再次获取用户?你从你的参数中获取用户对吗?
  • ["{user.mention}"] 从当你看到用户的键只是一个字符串时,你需要在它前面添加一个 f 才能真正在用户中寻找一个提及值
  • ctx.message.channel.send 可以缩短为 ctx.send
  • f“你给了 {member.mention} 1 MF 点。”您从未定义过成员值

我已经将你的大部分代码改写成我认为应该可以工作的东西:

@bot.command()
@commands.has_guild_permissions(administrator=True)
async def MF_add(ctx, user: discord.Member):
    with open ("MF Points.json", "r") as f:
        users = json.load(f)   
        
    if str(user.id) in users:
        users[str(user.id)] += 1

    else:
        users[str(user.id)] = 1
    
    await ctx.send(f"You have given {user.mention} 1 MF point.")
                
    with open("MF Points.json", "w") as f:
        json.dump(users, f, indent = 4)

请注意,不知道您实际上想用此代码实现什么/您想引入哪些其他功能,我不太确定这是最好的方法。但是你应该能够以此为基础弄清楚事情

注意:我将键转换为字符串,因为 json 不喜欢 int 键,但如果您使用自定义 object_hook,您可以使用 int 键(我会让您研究一下,它可能无法针对用例进行研究反正你有)

【讨论】:

  • 非常感谢您的深入回答!我实际上在顶部写了@client.command,但是帖子格式变得很奇怪。我尝试了您的新代码,但仍然没有。 if 语句中一定有一些东西。
  • @beepbeepboop 尝试将您的 MF Points 文件临时保存在其他地方,并只使用一个新的 MP Points 文件,其中只有一个空字典。然后再次尝试该命令,看看它是否有任何作用。 (我发送的最后一个版本)
  • 刚刚尝试清除 json 仍然没有。由于压痕,我得到了打印,所以这一切都很好。只是在那之前。
猜你喜欢
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
相关资源
最近更新 更多