2023-12-18 16:17:29 -05:00
|
|
|
import json
|
2023-12-18 19:00:16 -05:00
|
|
|
from datetime import timedelta
|
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
|
|
|
|
2023-12-18 17:09:15 -05:00
|
|
|
from ..utilities.database import connect, create_guild_table, mysql_log
|
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
|
|
|
|
): # 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()
|
2023-12-18 16:50:00 -05:00
|
|
|
data: [dict] = sorted(json.loads(file), key=lambda x: x["moderation_id"])
|
|
|
|
|
|
|
|
user_mod_types = ["NOTE", "WARN", "MUTE", "UNMUTE", "KICK", "BAN", "UNBAN"]
|
|
|
|
|
|
|
|
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"
|
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"]:
|
|
|
|
case["role_id"] = 0
|
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"]:
|
|
|
|
case["changes"] = []
|
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"])
|
2023-12-28 05:36:17 -05:00
|
|
|
if not metadata.get('imported_from'):
|
2023-12-28 04:23:55 -05:00
|
|
|
metadata.update({
|
|
|
|
'imported_from': 'Aurora'
|
|
|
|
})
|
2023-12-18 16:17:29 -05:00
|
|
|
|
2023-12-18 16:50:00 -05:00
|
|
|
if case["duration"] != "NULL":
|
|
|
|
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:
|
2023-12-18 16:50:00 -05:00
|
|
|
duration = "NULL"
|
2023-12-18 16:43:28 -05:00
|
|
|
|
2023-12-18 16:17:29 -05:00
|
|
|
await mysql_log(
|
|
|
|
self.ctx.guild.id,
|
2023-12-18 16:50:00 -05:00
|
|
|
case["moderator_id"],
|
|
|
|
case["moderation_type"],
|
|
|
|
case["target_type"],
|
|
|
|
case["target_id"],
|
|
|
|
case["role_id"],
|
2023-12-28 04:56:58 -05:00
|
|
|
duration,
|
2023-12-18 16:50:00 -05:00
|
|
|
case["reason"],
|
|
|
|
timestamp=case["timestamp"],
|
|
|
|
resolved=case["resolved"],
|
|
|
|
resolved_by=case["resolved_by"],
|
|
|
|
resolved_reason=case["resolve_reason"],
|
|
|
|
expired=case["expired"],
|
|
|
|
changes=case["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-01-05 04:26:01 -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
|
|
|
|
): # 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)
|