2024-02-28 10:58:57 -05:00
|
|
|
# pylint: disable=duplicate-code
|
2023-12-18 16:17:29 -05:00
|
|
|
import json
|
2023-12-18 19:00:16 -05:00
|
|
|
from datetime import timedelta
|
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
|
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
|
|
|
|
2023-12-28 04:23:55 -05:00
|
|
|
database = connect()
|
2023-12-18 16:17:29 -05:00
|
|
|
cursor = database.cursor()
|
|
|
|
|
|
|
|
query = f"DROP TABLE IF EXISTS moderation_{self.ctx.guild.id};"
|
|
|
|
cursor.execute(query)
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
database.commit()
|
|
|
|
|
|
|
|
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:
|
2023-12-18 16:50:00 -05:00
|
|
|
hours, minutes, seconds = map(int, case["duration"].split(":"))
|
|
|
|
duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
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-05-06 14:15:05 -04:00
|
|
|
Moderation.log(
|
2024-06-03 00:17:33 -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"],
|
2023-12-18 16:50:00 -05:00
|
|
|
timestamp=case["timestamp"],
|
|
|
|
resolved=case["resolved"],
|
|
|
|
resolved_by=case["resolved_by"],
|
|
|
|
resolved_reason=case["resolve_reason"],
|
|
|
|
expired=case["expired"],
|
2024-06-03 00:15:17 -04:00
|
|
|
changes=changes,
|
2023-12-18 16:56:27 -05:00
|
|
|
metadata=metadata,
|
2023-12-18 16:50:00 -05:00
|
|
|
database=database,
|
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)
|