【问题标题】:Adding data to json file when command is ran discord.py运行命令时将数据添加到 json 文件 discord.py
【发布时间】:2021-12-02 07:32:48
【问题描述】:

需要帮助 我希望它在每次运行命令时再次添加服务器数据 目标是最终将其更改为 on_guild_join 所以每次它加入服务器时,它都会创建一个类似的服务器设置 公会 ID、名称和类似数据存储,供机器人的其他部分访问

错误:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

代码:

 with open("./serverconfigs.json") as f:
        data = json.load(f)
    
    newserver = configData["Servers"]
    
    
@client.event
async def on_ready():
    print(client.user.name)
    print(client.user.id)
    print('Client ready!')


@client.command()
async def test1(ctx):
    with open('serverconfigs.json', 'r+') as f:
        serverdata = {
            "guildid":"",
            "guildname":"",
            "bannedwords":[],
            "blacklistedchannels":[],
            "modrole":[]
        }
        serverdata.append(newserver)
        data = json.load(f)
        json.dump(data, f, indent=2)
    await ctx.send('Data Collected.')

【问题讨论】:

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


    【解决方案1】:
    @client.command()
    async def test1(ctx, channel: discord.TextChannel, role: discord.Role, *, bannedwords: str):
        with open('serverconfigs.json') as json_file:
        data = json.load(json_file)
    
            serverdata = {
                "guildid":guild.id,
                "guildname":guild.name,
                "bannedwords":bannedwords,
                "blacklistedchannels":channel,
                "modrole":role
            }
            data.append(serverdata)
    
            with open('serverconfigs.json','w') as j:
    
            json.dump(data,j,indent=2)
    
        await ctx.send('Data Collected.')
    

    我没有时间对此进行测试,但它应该可以工作,如果有什么问题,请告诉我!

    【讨论】:

    • 不得不编辑一些东西,但转储数据的主要部分工作得很好,谢谢。
    猜你喜欢
    • 2021-01-12
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2020-12-31
    • 1970-01-01
    • 2011-03-10
    • 2021-07-06
    相关资源
    最近更新 更多