Skip to content

Commit 17dfd3a

Browse files
committed
add automod managed autowarns/automutes
Fixes #315
1 parent b1312c7 commit 17dfd3a

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

Events/AutoModEvents.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Threading.Channels;
2+
13
namespace 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);

Types/Config.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ public ulong InsidersChannel
295295
[JsonProperty("reactionEmoji")]
296296
public ReactionEmojiConfig ReactionEmoji { get; set; }
297297

298+
[JsonProperty("autoModRules")]
299+
public List<AutoModRuleConfig> AutoModRules { get; set; } = new();
300+
}
301+
302+
public class AutoModRuleConfig
303+
{
304+
[JsonProperty("ruleId")]
305+
public ulong RuleId { get; private set; }
306+
307+
[JsonProperty("action")]
308+
public string Action { get; private set; }
309+
310+
[JsonProperty("reason")]
311+
public string Reason { get; private set; } = "Automod rule violation";
298312
}
299313

300314
public class WorkflowConfig

0 commit comments

Comments
 (0)