2024-02-28 10:58:57 -05:00
|
|
|
# pylint: disable=duplicate-code
|
2023-12-18 16:17:29 -05:00
|
|
|
import json
|
2024-05-04 15:13:31 -04:00
|
|
|
from time import time
|
2023-12-18 16:58:24 -05:00
|
|
|
from typing import Dict
|
2023-12-18 17:24:40 -05:00
|
|
|
|
|
|
|
from discord import ButtonStyle, Interaction, Message, ui
|
2023-12-18 16:17:29 -05:00
|
|
|
from redbot.core import commands
|
2024-01-05 04:21:05 -05:00
|
|
|
from redbot.core.utils.chat_formatting import box, warning
|
2023-12-18 17:24:40 -05:00
|
|
|
|
2024-05-06 21:39:43 -04:00
|
|
|
from ..models.moderation import Moderation
|
|
|
|
from ..utilities.database import connect, create_guild_table
|
2024-06-04 23:31:52 -04:00
|
|
|
from ..utilities.utils import timedelta_from_string
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2023-12-18 16:50:00 -05:00
|
|
|
|
2023-12-28 04:23:55 -05:00
|
|
|
class ImportAuroraView(ui.View):
|
2023-12-18 16:17:29 -05:00
|
|
|
def __init__(self, timeout, ctx, message):
|
|
|
|
super().__init__()
|
|
|
|
self.ctx: commands.Context = ctx
|
|
|
|
self.message: Message = message
|
|
|
|
|
|
|
|
@ui.button(label="Yes", style=ButtonStyle.success)
|
2023-12-18 16:50:00 -05:00
|
|
|
async def import_button_y(
|
|
|
|
self, interaction: Interaction, button: ui.Button
|
2024-02-28 10:56:13 -05:00
|
|
|
): # pylint: disable=unused-argument
|
2023-12-18 16:17:29 -05:00
|
|
|
await self.message.delete()
|
2023-12-18 16:50:00 -05:00
|
|
|
await interaction.response.send_message(
|
|
|
|
"Deleting original table...", ephemeral=True
|
|
|
|
)
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2024-06-05 00:39:56 -04:00
|
|
|
database = await connect()
|
|
|
|
query = f"DROP TABLE IF EXISTS moderation_{self.ctx.guild.id};"
|
|
|
|
database.execute(query)
|
|
|
|
database.commit()
|
|
|
|
database.close()
|
2023-12-18 16:17:29 -05:00
|
|
|
|
|
|
|
await interaction.edit_original_response(content="Creating new table...")
|
|
|
|
|
|
|
|
await create_guild_table(self.ctx.guild)
|
|
|
|
|
|
|
|
await interaction.edit_original_response(content="Importing moderations...")
|
|
|
|
|
|
|
|
file = await self.ctx.message.attachments[0].read()
|
2024-02-14 11:04:26 -05:00
|
|
|
data: list[dict] = sorted(json.loads(file), key=lambda x: x["moderation_id"])
|
2023-12-18 16:50:00 -05:00
|
|
|
|
2024-05-04 12:57:27 -04:00
|
|
|
user_mod_types = ["NOTE", "WARN", "ADDROLE", "REMOVEROLE", "MUTE", "UNMUTE", "KICK", "TEMPBAN", "BAN", "UNBAN"]
|
2023-12-18 16:50:00 -05:00
|
|
|
|
|
|
|
channel_mod_types = ["SLOWMODE", "LOCKDOWN"]
|
2023-12-18 16:17:29 -05:00
|
|
|
|
|
|
|
failed_cases = []
|
|
|
|
|
|
|
|
for case in data:
|
2023-12-18 16:50:00 -05:00
|
|
|
if case["moderation_id"] == 0:
|
2023-12-18 16:33:09 -05:00
|
|
|
continue
|
|
|
|
|
2023-12-18 16:50:00 -05:00
|
|
|
if "target_type" not in case or not case["target_type"]:
|
|
|
|
if case["moderation_type"] in user_mod_types:
|
|
|
|
case["target_type"] = "USER"
|
|
|
|
elif case["moderation_type"] in channel_mod_types:
|
|
|
|
case["target_type"] = "CHANNEL"
|
2024-05-04 12:57:27 -04:00
|
|
|
else:
|
|
|
|
case["target_type"] = "USER"
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2023-12-18 16:50:00 -05:00
|
|
|
if "role_id" not in case or not case["role_id"]:
|
2024-05-04 14:49:07 -04:00
|
|
|
case["role_id"] = None
|
|
|
|
else:
|
|
|
|
case["role_id"] = int(case["role_id"])
|
|
|
|
|
|
|
|
case["target_id"] = int(case["target_id"])
|
|
|
|
case["moderator_id"] = int(case["moderator_id"])
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2023-12-18 16:50:00 -05:00
|
|
|
if "changes" not in case or not case["changes"]:
|
2024-06-03 00:15:17 -04:00
|
|
|
changes = []
|
2024-05-06 13:44:48 -04:00
|
|
|
else:
|
2024-06-03 00:15:17 -04:00
|
|
|
changes = json.loads(case["changes"])
|
|
|
|
if isinstance(changes, str):
|
|
|
|
changes: list[dict] = json.loads(changes)
|
|
|
|
|
|
|
|
for change in changes:
|
|
|
|
if change.get("bot"):
|
|
|
|
del change["bot"]
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2023-12-18 16:56:27 -05:00
|
|
|
if "metadata" not in case:
|
|
|
|
metadata = {}
|
2023-12-18 16:53:13 -05:00
|
|
|
else:
|
2023-12-28 04:51:16 -05:00
|
|
|
metadata: Dict[str, any] = json.loads(case["metadata"])
|
2024-02-03 13:41:57 -05:00
|
|
|
if not metadata.get("imported_from"):
|
|
|
|
metadata.update({"imported_from": "Aurora"})
|
2024-05-04 15:13:31 -04:00
|
|
|
metadata.update({"imported_timestamp": int(time())})
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2024-05-04 14:49:07 -04:00
|
|
|
if case["duration"] != "NULL" and case["duration"] is not None:
|
2024-06-04 23:31:52 -04:00
|
|
|
duration = timedelta_from_string(case["duration"])
|
2023-12-18 16:47:10 -05:00
|
|
|
else:
|
2024-05-04 14:49:07 -04:00
|
|
|
duration = None
|
2023-12-18 16:43:28 -05:00
|
|
|
|
2024-06-04 23:31:36 -04:00
|
|
|
try:
|
2024-06-05 00:14:43 -04:00
|
|
|
await Moderation.log(
|
2024-06-04 23:31:36 -04:00
|
|
|
bot=interaction.client,
|
|
|
|
guild_id=self.ctx.guild.id,
|
|
|
|
moderator_id=case["moderator_id"],
|
|
|
|
moderation_type=case["moderation_type"],
|
|
|
|
target_type=case["target_type"],
|
|
|
|
target_id=case["target_id"],
|
|
|
|
role_id=case["role_id"],
|
|
|
|
duration=duration,
|
|
|
|
reason=case["reason"],
|
|
|
|
timestamp=case["timestamp"],
|
|
|
|
resolved=case["resolved"],
|
|
|
|
resolved_by=case["resolved_by"],
|
|
|
|
resolved_reason=case["resolve_reason"],
|
|
|
|
expired=case["expired"],
|
|
|
|
changes=changes,
|
|
|
|
metadata=metadata,
|
|
|
|
database=database,
|
|
|
|
return_obj=False
|
|
|
|
)
|
2024-06-04 23:55:55 -04:00
|
|
|
except Exception as e: # pylint: disable=broad-exception-caught
|
2024-06-04 23:31:36 -04:00
|
|
|
failed_cases.append(str(case["moderation_id"]) + f": {e}")
|
2023-12-18 16:17:29 -05:00
|
|
|
|
|
|
|
await interaction.edit_original_response(content="Import complete.")
|
|
|
|
if failed_cases:
|
2023-12-18 16:50:00 -05:00
|
|
|
await interaction.edit_original_response(
|
2024-02-03 13:41:57 -05:00
|
|
|
content="Import complete.\n"
|
|
|
|
+ warning("Failed to import the following cases:\n")
|
|
|
|
+ box(failed_cases)
|
2023-12-18 16:50:00 -05:00
|
|
|
)
|
2023-12-18 16:17:29 -05:00
|
|
|
|
|
|
|
@ui.button(label="No", style=ButtonStyle.danger)
|
2023-12-18 16:50:00 -05:00
|
|
|
async def import_button_n(
|
|
|
|
self, interaction: Interaction, button: ui.Button
|
2024-02-28 10:56:13 -05:00
|
|
|
): # pylint: disable=unused-argument
|
2023-12-28 04:52:04 -05:00
|
|
|
await self.message.edit(content="Import cancelled.", view=None)
|
2023-12-18 16:17:29 -05:00
|
|
|
await self.message.delete(10)
|
|
|
|
await self.ctx.message.delete(10)
|