【问题标题】:Discord.py Json file instead of adding new values, json replace all the values with oneDiscord.py json文件而不是添加新值,json将所有值替换为一个
【发布时间】:2021-03-18 07:56:24
【问题描述】:

所以,我创建了将用户输入存储到 json 文件中的命令,代码如下:

       for user in client.users:
            with open('suggestions.json', 'r') as f:
                h = json.load(f)

             h[str(f'{user.id} | {user.name} ')] = message.content

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

问题在于,当两个或多个不同的用户使用该命令时,他们的所有输入都会在最后一个输入中发生变化,例如:

{
    "ID| USER NAME ": "ol",
    "ID | USER NAME ": "ol",
    "ID | USER NAME ": "ol"
}

由于某种原因,还存储了用户输入的机器人名称和 ID,以及在那一秒输入的所有成员或机器人,其值与发出命令的人相同,我尝试了很多东西,我真的不知道,也许只是使用数据库,但 atm 我对此一无所知。

为了更好地查看,我的命令此时看起来像这样:

@client.command()
async def suggestion(ctx):
    await ctx.send("You want to submit a suggestion?")
    message = await client.wait_for ('message' ,timeout=25, check = lambda m: m.author == ctx.author and m.channel == ctx.channel)

    if message.content == "yes"  :
        await ctx.send("Good choice, now let's hear your suggestion : ")

        message = await client.wait_for('message', timeout=25, check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
        await ctx.send("Alirght, i will send this suggestion to the developer, thanks ! ")
        for user in client.users:
            with open('suggestions.json', 'r') as f:
                h = json.load(f)

            h[str(f'{user.id} | {user.name} ')] = message.content

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

    elif message.content == "no":
        await ctx.send("Too bad, please use this command just if you have any ideas.")

【问题讨论】:

    标签: python json python-3.x bots discord.py


    【解决方案1】:

    所以,你正在做一个简单的建议命令,这是用于将数据完美地存储在 json 中的方式:)

    def saveJson(data, file):
        json.dump(data, open(file, "w"), indent = 4)
    
    _suggestions = open('./suggestions.json', 'r')
    suggestions = json.load(f)
    _suggestions.close()
    
    @client.command()
    async def suggest(ctx, *, message):
        user = ctx.author.id
        username = str(ctx.author)
        for current_user in suggestions['users']:
            if current_user['id'] == user:
                current_user['suggestions'].append(message)
                if username != current_user['name']:
                    current_user['name'] = username
                break
        else:
            suggestions['users'].append({
                'id':user,
                'name':username,
                'suggestions': [message]
            })
        saveJson(suggestions, "./suggestions.json")
    

    您的 json 文件应如下所示,

    {
        'users': [
            {
                'id': 123456789,
                'name': 'billy#1234',
                'suggestions: [
                    'be good',
                    'drink milk',
                    'eat cookies'
                ]
            },
            {
                'id': 1234512345,
                'name': 'willy#1234',
                'suggestions: [
                    'don't be good',
                    'don't drink milk',
                    'don't eat cookies',
                    'drink orange juice'
                ]
            }
    }
    

    并且,通过提出建议命令,您可以检索用户的所有建议,如果您想知道该怎么做,请告诉我:)

    【讨论】:

    • 您的版本看起来不错,我刚刚编辑了我的问题以查看完整代码,因此您可以大致了解代码应该如何,我希望这会有所帮助,所以现在的问题是,如何我可以为我的代码实现你的解决方案@BillyDev
    • 而且我还有一堆错误,因为`_suggestions = open('suggestions.json', 'r') as f: 建议 = json.load(f) ` 像“预期声明的错误,发现 Py:AS_KEYWORD,预期语句结束,预期表达式,名称 'f' 可以未定义,重新声明上面定义的 '_suggestions' 没有使用“这个,只有两行。看起来不错,但是当我分析代码时..
    • 再检查一遍,应该是修好了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多