feat(moderation): added ability to export moderation history
Some checks failed
Pylint / Pylint (3.10) (push) Failing after 1m2s

This commit is contained in:
Seaswimmer 2023-12-13 15:35:17 -05:00
parent d4e0425e0a
commit 6681a7d938
Signed by: cswimr
GPG key ID: 1EBC234EEDA901AE

View file

@ -1,5 +1,7 @@
import logging import logging
import json
import time import time
import os
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from typing import Union from typing import Union
import discord import discord
@ -697,7 +699,7 @@ class Moderation(commands.Cog):
await self.log(interaction, moderation_id) await self.log(interaction, moderation_id)
@app_commands.command(name="history") @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. """List previous infractions.
Parameters Parameters
@ -711,7 +713,9 @@ class Moderation(commands.Cog):
page: int page: int
Page to select Page to select
epheremal: bool 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) await interaction.response.defer(ephemeral=ephemeral)
permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction) permissions = self.check_permissions(interaction.client.user, ['embed_links'], interaction)
if permissions: if permissions:
@ -741,6 +745,17 @@ class Moderation(commands.Cog):
for result in results: for result in results:
case_dict = self.generate_dict(result) case_dict = self.generate_dict(result)
result_dict_list.append(case_dict) 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: if target or moderator:
case_quantity = len(result_dict_list) case_quantity = len(result_dict_list)
else: else: