Discord bot写入JSON

时间:2018-11-17 19:41:16

标签: python discord.py

我正在尝试从我的Discord机器人中编写一些JSON内容

from discord.ext import commands
import json

with open("Configs/Premium.json") as f:
    Premium = json.load(f)['serverID']

with open("Configs/Keys.json") as k:
    Keys = json.load(k)


class Redeem:
    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def redeem(self, ctx, key):
        if ctx.message.server.id == Premium:
            try:
                await self.client.say('This server already has premium')
            except Exception as error:
                await self.client.say('{}'.format(error))
        else:
            if key in Keys:
                try:
                    with open('Configs/Premium.json', 'a') as lm:
                        json.dump(key, lm)
                        await self.client.say('Premium has been activated for this server')
                except Exception as error:
                    await self.client.say('{}'.format(error))
            else:
                await self.client.say('That key is invalid')


def setup(client):
    client.add_cog(Redeem(client))

写入JSON,但使它变成这样

{
  "serverID": "512338284479774730"
}"KEY USED"

但是,我希望它如下所示

{
  "serverID": "512338284479774730",
  "serverID": "The Key"
}

它从包含ok密钥的文件中读取,但不会将密钥写入Premium.json

0 个答案:

没有答案