I am currently making a discord.py warn command. I am trying to make it so on execution, it will look for the guild.id, if there is none it will add the guild.id to the file, then the user.id, same as it will add if it's not in there, then it states 3 things, the warn.id/message.id, who warned the user, and the reason of the warn. If they already have a warning, instead of overlapping/overwrite it, it should add to the file instead of re-doing it. So I am asking how I can make this work or how to make it not overwrite? I have tried with open("warnings.json", "a") as f: when dumping, but it just added a whole new thing after the code in the json file.
The code:
@commands.command() @commands.has_permissions(kick_members=True) async def warn(self, ctx, member: discord.Member = None, *, reason=None): if member is None: embed = discord.Embed(title='Error:', description='Please list a member', color=discord.Color.red()) await ctx.send(embed=embed) elif member == ctx.author: embed = discord.Embed(title='Error:', description='You can not warn yourself', color=discord.Color.red()) await ctx.send(embed=embed) elif member == self.bot.user: embed = discord.Embed(title='Error:', description='You can not warn me', color=discord.Color.red()) await ctx.send(embed=embed) elif reason is None: embed = discord.Embed(title='Error:', description='Please list a reason', color=discord.Color.red()) await ctx.send(embed=embed) else: embed = discord.Embed( title=f'{member.name} has been warned:', description=f'Reason: {reason}', color=discord.Color.red() ) embed.set_footer(text=f'Warned by {ctx.author.name}', icon_url=ctx.author.avatar_url) my_msg = await ctx.send(embed=embed) with open("warnings.json", "r") as f: warn_list = json.load(f) warn_list[str(ctx.guild.id)] = {} warn_list[str(ctx.guild.id)][member.id] = [] warn_list[str(ctx.guild.id)][member.id].append({ 'warnId': my_msg.id, 'reason': reason, 'warnedBy': ctx.author.name }) with open("warnings.json", "w") as f: json.dump(warn_list, f)``` https://stackoverflow.com/questions/65929625/how-do-you-append-a-json-file-in-discord-py January 28, 2021 at 09:05AM
没有评论:
发表评论