为什么此齿轮不会修改json文件?

时间:2019-07-22 19:42:48

标签: python discord.py

我已经创建了此练级系统,但是代码未修改json 为什么不保存和修改json? 我没有为替代做任何尝试 因为对我来说,最好的格式是json 而且我在bot的控制台中没有任何错误

我只发送部分代码,因为整个代码太长了

class levelling(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.client = client

        self.client.loop.create_task(self.save_users())
        with open(r"/Users/Polliog/PycharmProjects/AP/cogs/users.json", "r") as f:
            self.users = json.load(f)

    async  def save_users(self):
        await self.client.wait_until_ready()
        while not self.client.is_closed():
            with open(r"/Users/Polliog/PycharmProjects/AP/cogs/users.json", "w") as f:
                json.dump(self.users, f, indent=4)

            await asyncio.sleep(5)


    def lvl_up(self, author_id):
        cur_xp = self.users[author_id]["exp"]
        cur_lvl = self.users[author_id]["level"]

        if cur_xp >= round((4 * (cur_lvl ** 3)) / 5):
            self.users[author_id]["level"] += 1
            return  True
        else:
            return False


    async def on_message(self, message):
        if message.author == self.client.user:
            return
        author_id = str(message.author.id)

        if not author_id in self.users:
            self.users[author_id] = {}
            self.users[author_id]["level"] = 1
            self.users[author_id]["exp"] = 9

        self.users[author_id]["exp"] += 1

        if self.lvl_up(author_id):
            await message.channell.send(f"{message.author.mention} è ora al livello {self.users[author_id]['level']}")


    @commands.command()
    async def level(self, ctx, member: discord.Member = None):
            member = ctx.author if not member else member
            member_id = str(member.id)

            if not member_id in self.users:
                await ctx.send("L'utente no ha un livello")
            else:
                embed = discod.Embed(color=member.color, timestamp=ctx.message.created.at)

                embed.set_author(name=f"Livello - {member}", value=self.users[member_id]["level"])
                embed.add_field(name="XP", value=self.users[member_id]["exp"])

                await ctx.send(embed=Embed)

0 个答案:

没有答案