2024-02-28 10:58:57 -05:00
# pylint: disable=duplicate-code
2023-12-18 16:17:29 -05:00
import json
2025-01-25 20:04:02 +00:00
import os
from time import time
from typing import Dict , List
2023-12-18 17:24:40 -05:00
2025-01-25 20:04:02 +00:00
from discord import ButtonStyle , File , Interaction , Message , ui
from redbot . core import commands , data_manager
from redbot . core . utils . chat_formatting import warning
2023-12-18 17:24:40 -05:00
2025-01-25 20:04:02 +00:00
from . . models . moderation import Moderation
from . . models . type import Type , type_registry
from . . utilities . json import dump
from . . utilities . utils import create_guild_table , 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 ) :
2025-01-25 20:04:02 +00:00
def __init__ ( self , timeout , ctx , message , data : List [ Dict [ str , any ] ] ) :
2023-12-18 16:17:29 -05:00
super ( ) . __init__ ( )
self . ctx : commands . Context = ctx
self . message : Message = message
2025-01-25 20:04:02 +00:00
self . data : List [ Dict [ str , any ] ] = data
2023-12-18 16:17:29 -05:00
@ui.button ( label = " Yes " , style = ButtonStyle . success )
2025-01-25 20:04:02 +00: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 ( )
2025-01-25 20:04:02 +00:00
await interaction . response . send_message ( " Deleting original table... " , ephemeral = True )
2023-12-18 16:17:29 -05:00
query = f " DROP TABLE IF EXISTS moderation_ { self . ctx . guild . id } ; "
2025-01-25 20:04:02 +00:00
await Moderation . execute ( query = query , return_obj = False )
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... " )
failed_cases = [ ]
2025-01-25 20:04:02 +00:00
for case in self . data :
2023-12-18 16:50:00 -05:00
if case [ " moderation_id " ] == 0 :
2023-12-18 16:33:09 -05:00
continue
2025-01-25 20:04:02 +00:00
moderation_type : Type = type_registry [ case [ " moderation_type " ] . lower ( ) ]
2023-12-18 16:50:00 -05:00
if " target_type " not in case or not case [ " target_type " ] :
2025-01-25 20:04:02 +00:00
if moderation_type . channel :
case [ " target_type " ] = " channel "
2024-05-04 12:57:27 -04:00
else :
2025-01-25 20:04:02 +00:00
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 " ] :
2025-01-25 20:04:02 +00:00
case [ " role_id " ] = None
else :
case [ " role_id " ] = int ( case [ " role_id " ] )
2023-12-18 16:17:29 -05:00
2025-01-25 20:04:02 +00:00
case [ " target_id " ] = int ( case [ " target_id " ] )
case [ " moderator_id " ] = int ( case [ " moderator_id " ] )
changes = case . get ( " changes " , None )
if not changes :
changes = [ ]
else :
if not isinstance ( changes , list ) :
changes = json . loads ( changes )
if isinstance ( changes , str ) :
changes : list [ dict ] = json . loads ( changes )
for change in changes :
if " bot " in change :
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 :
2025-01-25 20:04:02 +00:00
if isinstance ( case [ " metadata " ] , str ) :
metadata : Dict [ str , any ] = json . loads ( case [ " metadata " ] )
else :
metadata = case [ " metadata " ]
2024-02-03 13:41:57 -05:00
if not metadata . get ( " imported_from " ) :
metadata . update ( { " imported_from " : " Aurora " } )
2025-01-25 20:04:02 +00:00
metadata . update ( { " imported_timestamp " : int ( time ( ) ) } )
2023-12-18 16:17:29 -05:00
2025-01-25 20:04:02 +00:00
if case [ " duration " ] != " NULL " and case [ " duration " ] is not None :
duration = timedelta_from_string ( case [ " duration " ] )
if moderation_type . key == " ban " :
moderation_type = type_registry [ " tempban " ]
2023-12-18 16:47:10 -05:00
else :
2025-01-25 20:04:02 +00:00
duration = None
try :
await Moderation . log (
bot = interaction . client ,
guild_id = self . ctx . guild . id ,
moderator_id = case [ " moderator_id " ] ,
moderation_type = moderation_type ,
target_type = case [ " target_type " ] . lower ( ) ,
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 ,
return_obj = False ,
)
except Exception as e : # pylint: disable=broad-exception-caught
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 :
2025-01-25 20:04:02 +00:00
filename = str ( data_manager . cog_data_path ( cog_instance = self ) ) + str ( os . sep ) + f " failed_cases_ { interaction . guild . id } .json "
with open ( filename , " w " , encoding = " utf-8 " ) as f :
dump ( obj = failed_cases , fp = f , indent = 2 )
await interaction . channel . send ( content = " Import complete. \n " + warning ( " Failed to import the following cases: \n " ) , file = File ( filename , f " failed_cases_ { interaction . guild . id } .json " ) )
os . remove ( filename )
2023-12-18 16:17:29 -05:00
@ui.button ( label = " No " , style = ButtonStyle . danger )
2025-01-25 20:04:02 +00: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 )