@ -12,13 +12,14 @@ from models.usage_service_model import UsageService
usage_service = UsageService ( Path ( os . environ . get ( " DATA_DIR " , os . getcwd ( ) ) ) )
model = Model ( usage_service )
class ModerationResult :
WARN = " warn "
DELETE = " delete "
NONE = " none "
class ThresholdSet :
class ThresholdSet :
def __init__ ( self , h_t , hv_t , sh_t , s_t , sm_t , v_t , vg_t ) :
self . keys = [
" hate " ,
@ -27,7 +28,7 @@ class ThresholdSet:
" sexual " ,
" sexual/minors " ,
" violence " ,
" violence/graphic "
" violence/graphic " ,
]
self . thresholds = [
h_t ,
@ -70,7 +71,9 @@ class Moderation:
return embed
@staticmethod
def build_admin_warning_message ( moderated_message , deleted_message = None , timed_out = None ) :
def build_admin_warning_message (
moderated_message , deleted_message = None , timed_out = None
) :
embed = discord . Embed (
title = " Potentially unwanted message in the "
+ moderated_message . guild . name
@ -81,12 +84,13 @@ class Moderation:
link = f " https://discord.com/channels/ { moderated_message . guild . id } / { moderated_message . channel . id } / { moderated_message . id } "
embed . add_field ( name = " Message link " , value = link , inline = False )
if deleted_message :
embed . add_field ( name = " Message deleted by: " , value = deleted_message , inline = False )
embed . add_field (
name = " Message deleted by: " , value = deleted_message , inline = False
)
if timed_out :
embed . add_field ( name = " User timed out by: " , value = timed_out , inline = False )
return embed
@staticmethod
def build_admin_moderated_message ( moderated_message , response_message ) :
@ -106,8 +110,8 @@ class Moderation:
@staticmethod
def determine_moderation_result ( text , response ) :
warn_set = ThresholdSet ( 0.005 , 0.05 , 0.05 , 0.91 , 0.1 , 0.08 , 0.1 )
delete_set = ThresholdSet ( 0.1 , 0.1 , 0.1 , 0.95 , 0.2 , 0.6 , 0.4 )
warn_set = ThresholdSet ( 0.005 , 0.05 , 0.05 , 0.91 , 0.1 , 0.08 , 0.1 )
delete_set = ThresholdSet ( 0.1 , 0.1 , 0.1 , 0.95 , 0.2 , 0.6 , 0.4 )
warn_result , flagged_warn = warn_set . moderate ( text , response )
delete_result , flagged_delete = delete_set . moderate ( text , response )
@ -119,8 +123,6 @@ class Moderation:
else :
return ModerationResult . NONE
# This function will be called by the bot to process the message queue
@staticmethod
async def process_moderation_queue (
@ -162,9 +164,15 @@ class Moderation:
)
elif moderation_result == ModerationResult . WARN :
response_message = await moderations_alert_channel . send (
embed = Moderation . build_admin_warning_message ( to_moderate . message ) ,
embed = Moderation . build_admin_warning_message (
to_moderate . message
) ,
)
await response_message . edit (
view = ModerationAdminView (
to_moderate . message , response_message
)
)
await response_message . edit ( view = ModerationAdminView ( to_moderate . message , response_message ) )
else :
await moderation_queue . put ( to_moderate )
@ -180,13 +188,21 @@ class ModerationAdminView(discord.ui.View):
def __init__ ( self , message , moderation_message , nodelete = False ) :
super ( ) . __init__ ( timeout = None ) # 1 hour interval to redo.
self . message = message
self . moderation_message = moderation_message ,
self . moderation_message = ( moderation_message , )
if not nodelete :
self . add_item ( DeleteMessageButton ( self . message , self . moderation_message ) )
self . add_item ( TimeoutUserButton ( self . message , self . moderation_message , 1 , nodelete ) )
self . add_item ( TimeoutUserButton ( self . message , self . moderation_message , 6 , nodelete ) )
self . add_item ( TimeoutUserButton ( self . message , self . moderation_message , 12 , nodelete ) )
self . add_item ( TimeoutUserButton ( self . message , self . moderation_message , 24 , nodelete ) )
self . add_item (
TimeoutUserButton ( self . message , self . moderation_message , 1 , nodelete )
)
self . add_item (
TimeoutUserButton ( self . message , self . moderation_message , 6 , nodelete )
)
self . add_item (
TimeoutUserButton ( self . message , self . moderation_message , 12 , nodelete )
)
self . add_item (
TimeoutUserButton ( self . message , self . moderation_message , 24 , nodelete )
)
class DeleteMessageButton ( discord . ui . Button [ " ModerationAdminView " ] ) :
@ -202,8 +218,14 @@ class DeleteMessageButton(discord.ui.Button["ModerationAdminView"]):
await interaction . response . send_message (
" This message was deleted " , ephemeral = True , delete_after = 10
)
await self . moderation_message [ 0 ] . edit ( embed = Moderation . build_admin_warning_message ( self . message , deleted_message = interaction . user . mention ) ,
view = ModerationAdminView ( self . message , self . moderation_message , nodelete = True ) )
await self . moderation_message [ 0 ] . edit (
embed = Moderation . build_admin_warning_message (
self . message , deleted_message = interaction . user . mention
) ,
view = ModerationAdminView (
self . message , self . moderation_message , nodelete = True
) ,
)
class TimeoutUserButton ( discord . ui . Button [ " ModerationAdminView " ] ) :
@ -222,15 +244,31 @@ class TimeoutUserButton(discord.ui.Button["ModerationAdminView"]):
pass
try :
await self . message . author . timeout ( until = discord . utils . utcnow ( ) + timedelta ( hours = self . hours ) , reason = " Breaking the server chat rules " )
await self . message . author . timeout (
until = discord . utils . utcnow ( ) + timedelta ( hours = self . hours ) ,
reason = " Breaking the server chat rules " ,
)
except Exception as e :
traceback . print_exc ( )
pass
await interaction . response . send_message (
f " This user was timed out for { self . hours } hour(s) " , ephemeral = True , delete_after = 10
f " This user was timed out for { self . hours } hour(s) " ,
ephemeral = True ,
delete_after = 10 ,
)
moderation_message = (
self . moderation_message [ 0 ] [ 0 ]
if self . nodelete
else self . moderation_message [ 0 ]
)
await moderation_message . edit (
embed = Moderation . build_admin_warning_message (
self . message ,
deleted_message = interaction . user . mention ,
timed_out = interaction . user . mention ,
) ,
view = ModerationAdminView (
self . message , self . moderation_message , nodelete = True
) ,
)
moderation_message = self . moderation_message [ 0 ] [ 0 ] if self . nodelete else self . moderation_message [ 0 ]
await moderation_message . edit ( embed = Moderation . build_admin_warning_message ( self . message , deleted_message = interaction . user . mention , timed_out = interaction . user . mention ) ,
view = ModerationAdminView ( self . message , self . moderation_message , nodelete = True ) )