2024-02-28 10:58:57 -05:00
|
|
|
# pylint: disable=duplicate-code
|
2023-12-18 16:17:08 -05:00
|
|
|
import json
|
2023-12-18 17:24:40 -05:00
|
|
|
from datetime import timedelta
|
2024-05-04 15:13:31 -04:00
|
|
|
from time import time
|
2023-12-18 17:24:40 -05:00
|
|
|
|
|
|
|
from discord import ButtonStyle, Interaction, Message, ui
|
2023-12-18 16:17:08 -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:08 -05:00
|
|
|
|
2023-12-18 17:24:40 -05:00
|
|
|
|
2023-12-18 16:17:08 -05:00
|
|
|
class ImportGalacticBotView(ui.View):
|
|
|
|
def __init__(self, timeout, ctx, message):
|
|
|
|
super().__init__()
|
|
|
|
self.ctx: commands.Context = ctx
|
|
|
|
self.message: Message = message
|
|
|
|
|
|
|
|
@ui.button(label="Yes", style=ButtonStyle.success)
|
2024-02-03 13:41:57 -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:08 -05:00
|
|
|
await self.message.delete()
|
2024-02-03 13:41:57 -05:00
|
|
|
await interaction.response.send_message(
|
|
|
|
"Deleting original table...", ephemeral=True
|
|
|
|
)
|
2023-12-18 16:17:08 -05:00
|
|
|
|
2023-12-28 04:23:55 -05:00
|
|
|
database = connect()
|
2023-12-18 16:17:08 -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...")
|
|
|
|
|
|
|
|
accepted_types = [
|
2024-02-03 13:41:57 -05:00
|
|
|
"NOTE",
|
|
|
|
"WARN",
|
|
|
|
"MUTE",
|
|
|
|
"UNMUTE",
|
|
|
|
"KICK",
|
|
|
|
"SOFTBAN",
|
|
|
|
"BAN",
|
|
|
|
"UNBAN",
|
|
|
|
"SLOWMODE",
|
|
|
|
"LOCKDOWN",
|
2023-12-18 16:17:08 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
file = await self.ctx.message.attachments[0].read()
|
2024-02-03 13:41:57 -05:00
|
|
|
data = sorted(json.loads(file), key=lambda x: x["case"])
|
2023-12-18 16:17:08 -05:00
|
|
|
|
|
|
|
failed_cases = []
|
|
|
|
|
|
|
|
for case in data:
|
2024-02-03 13:41:57 -05:00
|
|
|
if case["type"] not in accepted_types:
|
2023-12-18 16:17:08 -05:00
|
|
|
continue
|
|
|
|
|
2024-02-03 13:41:57 -05:00
|
|
|
timestamp = round(case["timestamp"] / 1000)
|
2023-12-18 16:17:08 -05:00
|
|
|
|
|
|
|
try:
|
2024-02-03 13:41:57 -05:00
|
|
|
if case["duration"] is not None and float(case["duration"]) != 0:
|
|
|
|
duration = timedelta(seconds=round(float(case["duration"]) / 1000))
|
2023-12-18 16:17:08 -05:00
|
|
|
else:
|
2024-05-04 15:13:31 -04:00
|
|
|
duration = None
|
2023-12-18 16:17:08 -05:00
|
|
|
except OverflowError:
|
2024-02-03 13:41:57 -05:00
|
|
|
failed_cases.append(case["case"])
|
2023-12-18 16:17:08 -05:00
|
|
|
continue
|
|
|
|
|
2024-05-04 15:13:31 -04:00
|
|
|
metadata = {"imported_from": "GalacticBot", "imported_timestamp": int(time())}
|
2023-12-18 16:17:08 -05:00
|
|
|
|
2024-02-03 13:41:57 -05:00
|
|
|
if case["type"] == "SLOWMODE":
|
|
|
|
metadata["seconds"] = case["data"]["seconds"]
|
2023-12-18 16:17:08 -05:00
|
|
|
|
2024-02-03 13:41:57 -05:00
|
|
|
if case["resolved"]:
|
2023-12-18 16:17:08 -05:00
|
|
|
resolved = 1
|
|
|
|
resolved_by = None
|
|
|
|
resolved_reason = None
|
|
|
|
resolved_timestamp = None
|
2024-02-03 13:41:57 -05:00
|
|
|
if case["changes"]:
|
|
|
|
for change in case["changes"]:
|
|
|
|
if change["type"] == "RESOLVE":
|
|
|
|
resolved_by = change["staff"]
|
|
|
|
resolved_reason = change["reason"]
|
|
|
|
resolved_timestamp = round(change["timestamp"] / 1000)
|
2023-12-18 16:17:08 -05:00
|
|
|
break
|
|
|
|
if resolved_by is None:
|
2024-02-03 13:41:57 -05:00
|
|
|
resolved_by = "?"
|
2023-12-18 16:17:08 -05:00
|
|
|
if resolved_reason is None:
|
2024-02-03 13:41:57 -05:00
|
|
|
resolved_reason = (
|
|
|
|
"Could not get resolve reason during moderation import."
|
|
|
|
)
|
2023-12-18 16:17:08 -05:00
|
|
|
if resolved_timestamp is None:
|
|
|
|
resolved_timestamp = timestamp
|
|
|
|
changes = [
|
|
|
|
{
|
2024-02-03 13:41:57 -05:00
|
|
|
"type": "ORIGINAL",
|
|
|
|
"reason": case["reason"],
|
|
|
|
"user_id": case["executor"],
|
|
|
|
"timestamp": timestamp,
|
2023-12-18 16:17:08 -05:00
|
|
|
},
|
|
|
|
{
|
2024-02-03 13:41:57 -05:00
|
|
|
"type": "RESOLVE",
|
|
|
|
"reason": resolved_reason,
|
|
|
|
"user_id": resolved_by,
|
|
|
|
"timestamp": resolved_timestamp,
|
|
|
|
},
|
2023-12-18 16:17:08 -05:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
resolved = 0
|
2024-05-04 15:13:31 -04:00
|
|
|
resolved_by = None
|
|
|
|
resolved_reason = None
|
2023-12-18 16:17:08 -05:00
|
|
|
changes = []
|
|
|
|
|
2024-02-03 13:41:57 -05:00
|
|
|
if case["reason"] and case["reason"] != "N/A":
|
|
|
|
reason = case["reason"]
|
2023-12-18 16:17:08 -05:00
|
|
|
else:
|
2024-05-04 15:13:31 -04:00
|
|
|
reason = None
|
2023-12-18 16:17:08 -05:00
|
|
|
|
|
|
|
await mysql_log(
|
|
|
|
self.ctx.guild.id,
|
2024-02-03 13:41:57 -05:00
|
|
|
case["executor"],
|
|
|
|
case["type"],
|
|
|
|
case["targetType"],
|
|
|
|
case["target"],
|
2023-12-18 16:17:08 -05:00
|
|
|
0,
|
2023-12-28 04:56:58 -05:00
|
|
|
duration,
|
2023-12-18 16:17:08 -05:00
|
|
|
reason,
|
|
|
|
timestamp=timestamp,
|
|
|
|
resolved=resolved,
|
|
|
|
resolved_by=resolved_by,
|
|
|
|
resolved_reason=resolved_reason,
|
|
|
|
changes=changes,
|
|
|
|
metadata=metadata,
|
2024-02-03 13:41:57 -05:00
|
|
|
database=database,
|
2023-12-18 16:17:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
await interaction.edit_original_response(content="Import complete.")
|
|
|
|
if failed_cases:
|
2024-02-03 13:41:57 -05:00
|
|
|
await interaction.edit_original_response(
|
|
|
|
content="Import complete.\n"
|
|
|
|
+ warning("Failed to import the following cases:\n")
|
|
|
|
+ box(failed_cases)
|
|
|
|
)
|
2023-12-18 16:17:08 -05:00
|
|
|
|
|
|
|
@ui.button(label="No", style=ButtonStyle.danger)
|
2024-02-03 13:41:57 -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:08 -05:00
|
|
|
await self.message.delete(10)
|
|
|
|
await self.ctx.message.delete(10)
|