2023-12-17 03:11:51 -05:00
# pylint: disable=cyclic-import
2023-12-17 02:16:44 -05:00
from datetime import datetime , timedelta
2023-12-17 02:36:18 -05:00
import humanize
2023-12-17 02:16:44 -05:00
from discord import Color , Embed , Guild , Interaction , InteractionMessage
2023-12-17 12:54:41 -05:00
from . utils import get_next_case_number , fetch_user_dict , fetch_channel_dict
2023-12-17 02:16:44 -05:00
async def embed_factory ( embed_type : str , color : Color , / , interaction : Interaction = None , case_dict : dict = None , guild : Guild = None , reason : str = None , moderation_type : str = None , response : InteractionMessage = None , duration : timedelta = None , resolved : bool = False ) :
""" This method creates an embed from set parameters, meant for either moderation logging or contacting the moderated user.
Valid arguments for ' embed_type ' :
- ' message '
2023-12-17 03:06:09 -05:00
- ' log '
2023-12-17 02:16:44 -05:00
- ' case '
- ' changes '
Required arguments for ' message ' :
- guild
- reason
- moderation_type
- response
- duration ( optional )
Required arguments for ' log ' :
- interaction
- case_dict
- resolved ( optional )
Required arguments for ' case ' & ' changes ' :
- interaction
- case_dict """
if embed_type == ' message ' :
if moderation_type in [ " kicked " , " banned " , " tempbanned " , " unbanned " ] :
guild_name = guild . name
else :
guild_name = f " [ { guild . name } ]( { response . jump_url } ) "
if moderation_type in [ " tempbanned " , " muted " ] and duration :
embed_duration = f " for { humanize . precisedelta ( duration ) } "
else :
embed_duration = " "
if moderation_type == " note " :
embed_desc = " received a "
else :
embed_desc = " been "
embed = Embed ( title = str . title ( moderation_type ) , description = f " You have { embed_desc } { moderation_type } { embed_duration } in { guild_name } . " , color = color , timestamp = datetime . now ( ) )
embed . add_field ( name = ' Reason ' , value = f " ` { reason } ` " )
embed . set_author ( name = guild . name , icon_url = guild . icon . url )
embed . set_footer ( text = f " Case # { await get_next_case_number ( guild . id ) : , } " , icon_url = " https://cdn.discordapp.com/attachments/1070822161389994054/1159469476773904414/arrow-right-circle-icon-512x512-2p1e2aaw.png?ex=65312319&is=651eae19&hm=3cebdd28e805c13a79ec48ef87c32ca532ffa6b9ede2e48d0cf8e5e81f3a6818& " )
return embed
if embed_type == ' case ' :
2023-12-17 12:54:41 -05:00
if case_dict [ ' target_type ' ] == ' USER ' :
target_user = await fetch_user_dict ( interaction , case_dict [ ' target_id ' ] )
target_name = f " ` { target_user [ ' name ' ] } ` " if target_user [ ' discriminator ' ] == " 0 " else f " ` { target_user [ ' name ' ] } # { target_user [ ' discriminator ' ] } ` "
elif case_dict [ ' target_type ' ] == ' CHANNEL ' :
target_user = await fetch_channel_dict ( interaction , case_dict [ ' target_id ' ] )
if target_user [ ' mention ' ] :
target_name = f " { target_user [ ' mention ' ] } ( { target_user [ ' id ' ] } ) "
else :
target_name = f " ` { target_user [ ' name ' ] } ` ( { target_user [ ' id ' ] } ) "
2023-12-17 02:16:44 -05:00
2023-12-17 12:54:41 -05:00
moderator_user = await fetch_user_dict ( interaction , case_dict [ ' moderator_id ' ] )
2023-12-17 02:16:44 -05:00
moderator_name = f " ` { moderator_user [ ' name ' ] } ` " if moderator_user [ ' discriminator ' ] == " 0 " else f " ` { moderator_user [ ' name ' ] } # { moderator_user [ ' discriminator ' ] } ` "
embed = Embed ( title = f " 📕 Case # { case_dict [ ' moderation_id ' ] : , } " , color = color )
embed . description = f " **Type:** { str . title ( case_dict [ ' moderation_type ' ] ) } \n **Target:** { target_name } ( { target_user [ ' id ' ] } ) \n **Moderator:** { moderator_name } ( { moderator_user [ ' id ' ] } ) \n **Resolved:** { bool ( case_dict [ ' resolved ' ] ) } \n **Timestamp:** <t: { case_dict [ ' timestamp ' ] } > | <t: { case_dict [ ' timestamp ' ] } :R> "
if case_dict [ ' duration ' ] != ' NULL ' :
td = timedelta ( * * { unit : int ( val ) for unit , val in zip ( [ " hours " , " minutes " , " seconds " ] , case_dict [ " duration " ] . split ( " : " ) ) } )
duration_embed = f " { humanize . precisedelta ( td ) } | <t: { case_dict [ ' end_timestamp ' ] } :R> " if bool ( case_dict [ ' expired ' ] ) is False else str ( humanize . precisedelta ( td ) )
embed . description + = f " \n **Duration:** { duration_embed } \n **Expired:** { bool ( case_dict [ ' expired ' ] ) } "
embed . description + = f " \n **Changes:** { len ( case_dict [ ' changes ' ] ) } "
if case_dict [ ' metadata ' ] :
if case_dict [ ' metadata ' ] [ ' imported_from ' ] :
embed . description + = f " \n **Imported From:** { case_dict [ ' metadata ' ] [ ' imported_from ' ] } "
embed . add_field ( name = ' Reason ' , value = f " ``` { case_dict [ ' reason ' ] } ``` " , inline = False )
if case_dict [ ' resolved ' ] == 1 :
resolved_user = await fetch_user_dict ( interaction , case_dict [ ' resolved_by ' ] )
resolved_name = f " ` { resolved_user [ ' name ' ] } ` " if resolved_user [ ' discriminator ' ] == " 0 " else f " ` { resolved_user [ ' name ' ] } # { resolved_user [ ' discriminator ' ] } ` "
embed . add_field ( name = ' Resolve Reason ' , value = f " Resolved by { resolved_name } ( { resolved_user [ ' id ' ] } ) for: \n ``` { case_dict [ ' resolve_reason ' ] } ``` " , inline = False )
return embed
if embed_type == ' changes ' :
embed = Embed ( title = f " 📕 Case # { case_dict [ ' moderation_id ' ] : , } Changes " , color = color )
memory_dict = { }
if case_dict [ ' changes ' ] :
for change in case_dict [ ' changes ' ] :
if change [ ' user_id ' ] not in memory_dict :
memory_dict [ str ( change [ ' user_id ' ] ) ] = await fetch_user_dict ( interaction , change [ ' user_id ' ] )
user = memory_dict [ str ( change [ ' user_id ' ] ) ]
name = user [ ' name ' ] if user [ ' discriminator ' ] == " 0 " else f " { user [ ' name ' ] } # { user [ ' discriminator ' ] } "
timestamp = f " <t: { change [ ' timestamp ' ] } > | <t: { change [ ' timestamp ' ] } :R> "
if change [ ' type ' ] == ' ORIGINAL ' :
embed . add_field ( name = ' Original ' , value = f " **User:** ` { name } ` ( { user [ ' id ' ] } ) \n **Reason:** { change [ ' reason ' ] } \n **Timestamp:** { timestamp } " , inline = False )
elif change [ ' type ' ] == ' EDIT ' :
embed . add_field ( name = ' Edit ' , value = f " **User:** ` { name } ` ( { user [ ' id ' ] } ) \n **Reason:** { change [ ' reason ' ] } \n **Timestamp:** { timestamp } " , inline = False )
elif change [ ' type ' ] == ' RESOLVE ' :
embed . add_field ( name = ' Resolve ' , value = f " **User:** ` { name } ` ( { user [ ' id ' ] } ) \n **Reason:** { change [ ' reason ' ] } \n **Timestamp:** { timestamp } " , inline = False )
else :
embed . description = " *No changes have been made to this case.* 🙁 "
return embed
if embed_type == ' log ' :
if resolved :
2023-12-17 12:54:41 -05:00
if case_dict [ ' target_type ' ] == ' USER ' :
target_user = await fetch_user_dict ( interaction , case_dict [ ' target_id ' ] )
target_name = f " ` { target_user [ ' name ' ] } ` " if target_user [ ' discriminator ' ] == " 0 " else f " ` { target_user [ ' name ' ] } # { target_user [ ' discriminator ' ] } ` "
elif case_dict [ ' target_type ' ] == ' CHANNEL ' :
target_user = await fetch_channel_dict ( interaction , case_dict [ ' target_id ' ] )
if target_user [ ' mention ' ] :
target_name = f " { target_user [ ' mention ' ] } ( { target_user [ ' id ' ] } ) "
else :
target_name = f " ` { target_user [ ' name ' ] } ` ( { target_user [ ' id ' ] } ) "
2023-12-17 02:16:44 -05:00
2023-12-17 12:54:41 -05:00
moderator_user = await fetch_user_dict ( interaction , case_dict [ ' moderator_id ' ] )
2023-12-17 02:16:44 -05:00
moderator_name = moderator_user [ ' name ' ] if moderator_user [ ' discriminator ' ] == " 0 " else f " { moderator_user [ ' name ' ] } # { moderator_user [ ' discriminator ' ] } "
embed = Embed ( title = f " 📕 Case # { case_dict [ ' moderation_id ' ] : , } Resolved " , color = color )
embed . description = f " **Type:** { str . title ( case_dict [ ' moderation_type ' ] ) } \n **Target:** { target_name } ( { target_user [ ' id ' ] } ) \n **Moderator:** { moderator_name } ( { moderator_user [ ' id ' ] } ) \n **Timestamp:** <t: { case_dict [ ' timestamp ' ] } > | <t: { case_dict [ ' timestamp ' ] } :R> "
if case_dict [ ' duration ' ] != ' NULL ' :
td = timedelta ( * * { unit : int ( val ) for unit , val in zip ( [ " hours " , " minutes " , " seconds " ] , case_dict [ " duration " ] . split ( " : " ) ) } )
duration_embed = f " { humanize . precisedelta ( td ) } | <t: { case_dict [ ' end_timestamp ' ] } :R> " if case_dict [ " expired " ] == ' 0 ' else str ( humanize . precisedelta ( td ) )
embed . description = embed . description + f " \n **Duration:** { duration_embed } \n **Expired:** { bool ( case_dict [ ' expired ' ] ) } "
embed . add_field ( name = ' Reason ' , value = f " ``` { case_dict [ ' reason ' ] } ``` " , inline = False )
resolved_user = await fetch_user_dict ( interaction , case_dict [ ' resolved_by ' ] )
resolved_name = resolved_user [ ' name ' ] if resolved_user [ ' discriminator ' ] == " 0 " else f " { resolved_user [ ' name ' ] } # { resolved_user [ ' discriminator ' ] } "
embed . add_field ( name = ' Resolve Reason ' , value = f " Resolved by { resolved_name } ( { resolved_user [ ' id ' ] } ) for: \n ``` { case_dict [ ' resolve_reason ' ] } ``` " , inline = False )
else :
2023-12-17 12:54:41 -05:00
if case_dict [ ' target_type ' ] == ' USER ' :
target_user = await fetch_user_dict ( interaction , case_dict [ ' target_id ' ] )
target_name = f " ` { target_user [ ' name ' ] } ` " if target_user [ ' discriminator ' ] == " 0 " else f " ` { target_user [ ' name ' ] } # { target_user [ ' discriminator ' ] } ` "
elif case_dict [ ' target_type ' ] == ' CHANNEL ' :
target_user = await fetch_channel_dict ( interaction , case_dict [ ' target_id ' ] )
if target_user [ ' mention ' ] :
target_name = target_user [ ' mention ' ]
else :
target_name = target_user [ ' name ' ]
2023-12-17 02:16:44 -05:00
2023-12-17 12:54:41 -05:00
moderator_user = await fetch_user_dict ( interaction , case_dict [ ' moderator_id ' ] )
2023-12-17 02:16:44 -05:00
moderator_name = moderator_user [ ' name ' ] if moderator_user [ ' discriminator ' ] == " 0 " else f " { moderator_user [ ' name ' ] } # { moderator_user [ ' discriminator ' ] } "
embed = Embed ( title = f " 📕 Case # { case_dict [ ' moderation_id ' ] : , } " , color = color )
embed . description = f " **Type:** { str . title ( case_dict [ ' moderation_type ' ] ) } \n **Target:** { target_name } ( { target_user [ ' id ' ] } ) \n **Moderator:** { moderator_name } ( { moderator_user [ ' id ' ] } ) \n **Timestamp:** <t: { case_dict [ ' timestamp ' ] } > | <t: { case_dict [ ' timestamp ' ] } :R> "
if case_dict [ ' duration ' ] != ' NULL ' :
td = timedelta ( * * { unit : int ( val ) for unit , val in zip ( [ " hours " , " minutes " , " seconds " ] , case_dict [ " duration " ] . split ( " : " ) ) } )
embed . description = embed . description + f " \n **Duration:** { humanize . precisedelta ( td ) } | <t: { case_dict [ ' end_timestamp ' ] } :R> "
embed . add_field ( name = ' Reason ' , value = f " ``` { case_dict [ ' reason ' ] } ``` " , inline = False )
return embed
raise ( TypeError ( " ' type ' argument is invalid! " ) )