繁体   English   中英

在python中删除JSON文件中的条目

[英]Deleting an entry in an JSON file in python

所以我在discord.py中负责反应角色。

我想做一个“ on_message_delete”事件,当机器人的反应角色被删除时,它会删除该条目。

async def get_reactionsID(self):
    with open("reaction_roles.json", "r+") as f:
        reaction = json.load(f)
    return reaction
    @commands.has_permissions(manage_channels=True)
    @commands.command()
    async def reactions(self, ctx):
        guild_id = ctx.guild.id
        data = reaction_roles_data.get(str(guild_id), None)
        embed = discord.Embed(title="Reaction Roles")
        if data is None:
            embed.description = "There are no reaction roles set up right now."
        else:
            for index, rr in enumerate(data):
                emote = rr.get("emote")
                role_id = rr.get("roleID")
                role = ctx.guild.get_role(role_id)
                channel_id = rr.get("channelID")
                message_id = rr.get("messageID")
                embed.add_field(
                    name=index,
                    value=f"{emote} - @{role} - [message](https://www.discordapp.com/channels/{guild_id}/{channel_id}/{message_id})",
                    inline=False,
                )
        await ctx.send(embed=embed)


@commands.Cog.listener()
async def on_message_delete(self, message):
    with open("reaction_roles.json", "r+") as f:
      reaction = json.load(f)
      if message.id in f:
        reactionToDelete = await self.get_reactionsID()
        reaction[str(reactionToDelete)]["id"]["emote"]["roleID"]["channelID"]["messageID"].pop()

这就是JSON文件中的内容: {"732268549623644230": [{"id": "83b3a144-9c24-4024-8054-0cfdd91a0802", "emote": "\?\?", "roleID": 747425571939680257, "channelID": 794653597967056896, "messageID": 805172187286863922}

尽管就我而言,它只是保留了它,但它并没有删除任何内容,也没有错误。

我将非常感谢您的每一次帮助,谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM