Python从字符串检查json文件Python中的用户id

2024-06-28 14:56:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的不知道怎么解释,所以我会解释我要做什么

  1. .profile <GrowID>,插入json文件中的GrowID
  2. 如果文件中有GrowID,请打开用户数据

这是我现在拥有的

命令模板

@client.command(aliases=["p"])
async def profile(ctx, GrowID=None):
  with open('account.json', 'r') as f:
    account = json.load(f)

  if GrowID == None:
    user = ctx.author

  elif not GrowID == None:
    user = client.get_user(GrowID.user_id)

  if str(user.id) in account:
    # <Codes>

我的数据在account.json

{
    "811510359289495572": {
        "Premium": false,
        "GrowID": "Dec7h7alker",
        "Character": "Default",
        "Level": 1,
        "XP": 0,
        "XPLevel": 150,
        "Gems": 0,
        "World Locks": 0,
        "Diamond Locks": 0,
        "Growtokens": 0
    }
}

命令添加数据

@client.command()
async def start(ctx):
        with open('account.json', 'r') as f:
          account = json.load(f)
        user = ctx.author

        if str(user.id) in account:
                await ctx.send("Your account has already existed!")

        elif str(user.id) not in account:
                account[str(user.id)] = {}
                account[str(user.id)]["Premium"] = False
                account[str(user.id)]["GrowID"] = user.name
                account[str(user.id)]["Character"] = "Default"
                account[str(user.id)]["Level"] = 1
                account[str(user.id)]["XP"] = 0
                account[str(user.id)]["XPLevel"] = 150
                account[str(user.id)]["Gems"] = 0
                account[str(user.id)]["World Locks"] = 0
                account[str(user.id)]["Diamond Locks"] = 0
                account[str(user.id)]["Growtokens"] = 0

                with open('account.json', 'w') as f:
                        json.dump(account, f, indent=4)

    # <codes (not needed cuz its just sending message)>

好了


Tags: 数据clientnoneidjsonifaswith
1条回答
网友
1楼 · 发布于 2024-06-28 14:56:44

我真的不明白问题出在哪里,因为正如评论中已经提到的那样,您可以使用已经存在的代码从用户检索数据

但是对于这一点,我们首先假设GrowID依赖于discord.Member,否则它将不起作用

然后我们在上面构建我们的命令:

@client.command(aliases=["p"])
async def profile(ctx, GrowID: discord.Member = None):
    with open('channel.json', 'r') as f:
        account = json.load(f) # Load account

        if GrowID == None:
            user = ctx.author
            await ctx.send(account[str(user.id)]) # Retrieve and send data

        else:
            user1 = client.get_user(GrowID.id) # Get the ID of the named user.
            await ctx.send(account[str(user1.id)]) # Send data of the account

然后将向您发送有关该用户的以下信息:

{'Premium': False, 'GrowID': 'Dec7h7alker', 'Character': 'Default', 'Level': 1, 'XP': 0, 'XPLevel': 150, 'Gems': 0, 'World Locks': 0, 'Diamond Locks': 0, 'Growtokens': 0}

为了让这个问题更加用户友好,需要你的知识,因为这与问题无关

命令的工作原理:

enter image description here

相关问题 更多 >