diff --git a/moderation/moderation.py b/moderation/moderation.py index e8febe6..233140c 100644 --- a/moderation/moderation.py +++ b/moderation/moderation.py @@ -1,5 +1,7 @@ import logging +import json import time +import os from datetime import datetime, timedelta, timezone from typing import Union import discord @@ -697,7 +699,7 @@ class Moderation(commands.Cog): await self.log(interaction, moderation_id) @app_commands.command(name="history") - async def history(self, interaction: discord.Interaction, target: discord.User = None, moderator: discord.User = None, pagesize: app_commands.Range[int, 1, 25] = 5, page: int = 1, ephemeral: bool = False): + async def history(self, interaction: discord.Interaction, target: discord.User = None, moderator: discord.User = None, pagesize: app_commands.Range[int, 1, 25] = 5, page: int = 1, ephemeral: bool = False, export: bool = False): """List previous infractions. Parameters @@ -711,7 +713,9 @@ class Moderation(commands.Cog): page: int Page to select epheremal: bool - Hide the command response""" + Hide the command response + export: bool + Exports the server's entire moderation history to a JSON file""" await interaction.response.defer(ephemeral=ephemeral) permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction) if permissions: @@ -741,6 +745,17 @@ class Moderation(commands.Cog): for result in results: case_dict = self.generate_dict(result) result_dict_list.append(case_dict) + if export: + try: + filename = f"moderation_{interaction.guild.id}.json" + with open(filename, "w") as f: + json.dump(result_dict_list, f) + await interaction.followup.send(file=discord.File(f"moderation_{interaction.guild.id}.json"), ephemeral=ephemeral) + os.remove(filename) + return + except json.JSONDecodeError as e: + await interaction.followup.send(content=f"An error occured while exporting the moderation history.\nError:\n```{e}```", ephemeral=ephemeral) + return if target or moderator: case_quantity = len(result_dict_list) else: