Skip to content

Commit 324b69c

Browse files
committed
restructure automod event to avoid nulls
1 parent b873fd2 commit 324b69c

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

Events/AutoModEvents.cs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,60 @@ public static async Task AutoModerationRuleExecuted(DiscordClient client, AutoMo
66
{
77
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);
88

9-
var author = await client.GetUserAsync(e.Rule.UserId);
10-
var channel = await client.GetChannelAsync(e.Rule.ChannelId!.Value);
119

12-
// Create a "mock" message object to pass to the message handler, since we don't have the actual message object
13-
var mentionedUsers = new List<ulong>();
14-
if (e.Rule.Content is not null)
10+
if (e.Rule.Action.Type is DiscordRuleActionType.BlockMessage)
1511
{
16-
foreach (var match in Constants.RegexConstants.user_rx.Matches(e.Rule.Content))
17-
{
18-
var id = Convert.ToUInt64(((Match)match).Groups[1].ToString());
19-
if (!mentionedUsers.Contains(id))
20-
mentionedUsers.Add(id);
21-
}
22-
}
23-
var message = new MockDiscordMessage(author: author, channel: channel, channelId: channel.Id, content: e.Rule.Content, mentionedUsersCount: mentionedUsers.Count);
24-
25-
if (e.Rule.Action.Type is DiscordRuleActionType.BlockMessage && Program.cfgjson.AutoModRules.Any(r => r.RuleId == e.Rule.RuleId))
26-
{
27-
var ruleConfig = Program.cfgjson.AutoModRules.First(r => r.RuleId == e.Rule.RuleId);
28-
string reason = ruleConfig.Reason;
29-
if (reason is null || reason == "")
30-
reason = "Automod rule violation";
12+
var author = await client.GetUserAsync(e.Rule.UserId);
13+
var channel = await client.GetChannelAsync(e.Rule.ChannelId!.Value);
3114

32-
var user = await client.GetUserAsync(e.Rule.UserId);
15+
Program.discord.Logger.LogDebug("Got an AutoMod Message Block event in channel {channelId} by user {userId}", e.Rule.ChannelId, e.Rule.UserId);
3316

34-
if (user is null)
17+
// Create a "mock" message object to pass to the message handler, since we don't have the actual message object
18+
var mentionedUsers = new List<ulong>();
19+
if (e.Rule.Content is not null)
3520
{
36-
Program.discord.Logger.LogError("AutoMod rule executed for user {userId} but user could not be found.", e.Rule.UserId);
37-
return;
21+
foreach (var match in Constants.RegexConstants.user_rx.Matches(e.Rule.Content))
22+
{
23+
var id = Convert.ToUInt64(((Match)match).Groups[1].ToString());
24+
if (!mentionedUsers.Contains(id))
25+
mentionedUsers.Add(id);
26+
}
3827
}
28+
var message = new MockDiscordMessage(author: author, channel: channel, channelId: channel.Id, content: e.Rule.Content, mentionedUsersCount: mentionedUsers.Count);
3929

40-
switch (ruleConfig.Action)
30+
if (Program.cfgjson.AutoModRules.Any(r => r.RuleId == e.Rule.RuleId))
4131
{
42-
case "mute":
43-
await MuteHelpers.MuteUserAsync(user, reason, Program.discord.CurrentUser.Id, Program.homeGuild);
44-
return;
45-
case "warn":
46-
DiscordMessage msg = await WarningHelpers.SendPublicWarningMessageAndDeleteInfringingMessageAsync(message, $"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**", true);
47-
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, channel, " automatically ");
48-
return;
49-
default:
50-
throw new NotImplementedException($"Unhandled AutoMod action type: {ruleConfig.Action}");
51-
}
52-
}
32+
var ruleConfig = Program.cfgjson.AutoModRules.First(r => r.RuleId == e.Rule.RuleId);
33+
string reason = ruleConfig.Reason;
34+
if (reason is null || reason == "")
35+
reason = "Automod rule violation";
5336

54-
if (e.Rule.Action.Type == DiscordRuleActionType.BlockMessage)
55-
{
56-
// AutoMod blocked a message. Pass it to the message handler to run it through some filters anyway.
37+
var user = await client.GetUserAsync(e.Rule.UserId);
5738

58-
Program.discord.Logger.LogDebug("Got an AutoMod Message Block event in channel {channelId} by user {userId}", e.Rule.ChannelId, e.Rule.UserId);
39+
if (user is null)
40+
{
41+
Program.discord.Logger.LogError("AutoMod rule executed for user {userId} but user could not be found.", e.Rule.UserId);
42+
return;
43+
}
44+
45+
switch (ruleConfig.Action)
46+
{
47+
case "mute":
48+
await MuteHelpers.MuteUserAsync(user, reason, Program.discord.CurrentUser.Id, Program.homeGuild);
49+
return;
50+
case "warn":
51+
DiscordMessage msg = await WarningHelpers.SendPublicWarningMessageAndDeleteInfringingMessageAsync(message, $"{Program.cfgjson.Emoji.Denied} {message.Author.Mention} was automatically warned: **{reason.Replace("`", "\\`").Replace("*", "\\*")}**", true);
52+
var warning = await WarningHelpers.GiveWarningAsync(message.Author, client.CurrentUser, reason, contextMessage: msg, channel, " automatically ");
53+
return;
54+
default:
55+
throw new NotImplementedException($"Unhandled AutoMod action type: {ruleConfig.Action}");
56+
}
57+
}
5958

6059
// Pass to the message handler
6160
await MessageEvent.MessageHandlerAsync(client, message, channel, false, true, true);
6261
}
62+
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)