1+ using System . Threading . Channels ;
2+
13namespace Cliptok . Events
24{
35 public class AutoModEvents
@@ -6,27 +8,55 @@ public static async Task AutoModerationRuleExecuted(DiscordClient client, AutoMo
68 {
79 Program . discord . Logger . LogDebug ( "Got an AutoMod Rule Executed event with action type {actionType} in channel {channelId} by user {userId}" , e . Rule . Action . Type , e . Rule . ChannelId , e . Rule . UserId ) ;
810
9- if ( e . Rule . Action . Type == DiscordRuleActionType . BlockMessage )
11+ var author = await client . GetUserAsync ( e . Rule . UserId ) ;
12+ var channel = await client . GetChannelAsync ( e . Rule . ChannelId ! . Value ) ;
13+
14+ // Create a "mock" message object to pass to the message handler, since we don't have the actual message object
15+ var mentionedUsers = new List < ulong > ( ) ;
16+ if ( e . Rule . Content is not null )
1017 {
11- // AutoMod blocked a message. Pass it to the message handler to run it through some filters anyway.
18+ foreach ( var match in Constants . RegexConstants . user_rx . Matches ( e . Rule . Content ) )
19+ {
20+ var id = Convert . ToUInt64 ( ( ( Match ) match ) . Groups [ 1 ] . ToString ( ) ) ;
21+ if ( ! mentionedUsers . Contains ( id ) )
22+ mentionedUsers . Add ( id ) ;
23+ }
24+ }
25+ var message = new MockDiscordMessage ( author : author , channel : channel , channelId : channel . Id , content : e . Rule . Content , mentionedUsersCount : mentionedUsers . Count ) ;
1226
13- Program . discord . Logger . LogDebug ( "Got an AutoMod Message Block event in channel {channelId} by user {userId}" , e . Rule . ChannelId , e . Rule . UserId ) ;
27+ if ( e . Rule . Action . Type is DiscordRuleActionType . BlockMessage && Program . cfgjson . AutoModRules . Any ( r => r . RuleId == e . Rule . RuleId ) ) {
28+ var ruleConfig = Program . cfgjson . AutoModRules . First ( r => r . RuleId == e . Rule . RuleId ) ;
29+ string reason = ruleConfig . Reason ;
30+ if ( reason is null || reason == "" )
31+ reason = "Automod rule violation" ;
32+
33+ var user = await client . GetUserAsync ( e . Rule . UserId ) ;
1434
15- var author = await client . GetUserAsync ( e . Rule . UserId ) ;
16- var channel = await client . GetChannelAsync ( e . Rule . ChannelId ! . Value ) ;
35+ if ( user is null )
36+ {
37+ Program . discord . Logger . LogError ( "AutoMod rule executed for user {userId} but user could not be found." , e . Rule . UserId ) ;
38+ return ;
39+ }
1740
18- // Create a "mock" message object to pass to the message handler, since we don't have the actual message object
19- var mentionedUsers = new List < ulong > ( ) ;
20- if ( e . Rule . Content is not null )
41+ switch ( ruleConfig . Action )
2142 {
22- foreach ( var match in Constants . RegexConstants . user_rx . Matches ( e . Rule . Content ) )
23- {
24- var id = Convert . ToUInt64 ( ( ( Match ) match ) . Groups [ 1 ] . ToString ( ) ) ;
25- if ( ! mentionedUsers . Contains ( id ) )
26- mentionedUsers . Add ( id ) ;
27- }
43+ case "mute" :
44+ await MuteHelpers . MuteUserAsync ( user , reason , Program . discord . CurrentUser . Id , Program . homeGuild ) ;
45+ return ;
46+ case "warn" :
47+ DiscordMessage msg = await WarningHelpers . SendPublicWarningMessageAndDeleteInfringingMessageAsync ( message , $ "{ Program . cfgjson . Emoji . Denied } { message . Author . Mention } was automatically warned: **{ reason . Replace ( "`" , "\\ `" ) . Replace ( "*" , "\\ *" ) } **", true ) ;
48+ var warning = await WarningHelpers . GiveWarningAsync ( message . Author , client . CurrentUser , reason , contextMessage : msg , channel , " automatically " ) ;
49+ return ;
50+ default :
51+ throw new NotImplementedException ( $ "Unhandled AutoMod action type: { ruleConfig . Action } ") ;
2852 }
29- var message = new MockDiscordMessage ( author : author , channel : channel , channelId : channel . Id , content : e . Rule . Content , mentionedUsersCount : mentionedUsers . Count ) ;
53+ }
54+
55+ if ( e . Rule . Action . Type == DiscordRuleActionType . BlockMessage )
56+ {
57+ // AutoMod blocked a message. Pass it to the message handler to run it through some filters anyway.
58+
59+ Program . discord . Logger . LogDebug ( "Got an AutoMod Message Block event in channel {channelId} by user {userId}" , e . Rule . ChannelId , e . Rule . UserId ) ;
3060
3161 // Pass to the message handler
3262 await MessageEvent . MessageHandlerAsync ( client , message , channel , false , true , true ) ;
0 commit comments