-
Notifications
You must be signed in to change notification settings - Fork 45
/
Moderation.cs
173 lines (157 loc) · 6.84 KB
/
Moderation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Linq;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.EventHandlers;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace CompatBot.Commands;
internal sealed partial class Moderation: BaseCommandModuleCustom
{
[Command("report"), RequiresWhitelistedRole]
[Description("Adds specified message to the moderation queue")]
public async Task Report(CommandContext ctx, [Description("Message ID from current channel to report")] ulong messageId, [RemainingText, Description("Optional report comment")] string? comment = null)
{
try
{
var msg = await ctx.Channel.GetMessageAsync(messageId).ConfigureAwait(false);
await ReportMessage(ctx, comment, msg);
}
catch (Exception)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to report the message").ConfigureAwait(false);
}
}
[Command("report"), RequiresWhitelistedRole]
[Description("Adds specified message to the moderation queue")]
public async Task Report(CommandContext ctx, [Description("Message link to report")] string messageLink, [RemainingText, Description("Optional report comment")] string? comment = null)
{
try
{
var msg = await ctx.GetMessageAsync(messageLink).ConfigureAwait(false);
if (msg is null)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Can't find linked message").ConfigureAwait(false);
return;
}
await ReportMessage(ctx, comment, msg);
}
catch (Exception)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to report the message").ConfigureAwait(false);
}
}
[Command("analyze"), Aliases("reanalyze", "parse", "a")]
[Description("Make bot to look at the attached log again")]
public async Task Reanalyze(CommandContext ctx, [Description("Message ID from the same channel")]ulong messageId)
{
try
{
var msg = await ctx.Channel.GetMessageAsync(messageId).ConfigureAwait(false);
if (msg == null)
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
else
LogParsingHandler.EnqueueLogProcessing(ctx.Client, ctx.Channel, msg, ctx.Member, true, true);
}
catch
{
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
}
}
[Command("analyze")]
public async Task Reanalyze(CommandContext ctx, [Description("Full message link")] string messageLink)
{
try
{
var msg = await ctx.GetMessageAsync(messageLink).ConfigureAwait(false);
if (msg == null)
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
else
LogParsingHandler.EnqueueLogProcessing(ctx.Client, ctx.Channel, msg, ctx.Member, true, true);
}
catch (Exception e)
{
Config.Log.Warn(e);
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
}
}
[Command("analyze")]
public async Task Reanalyze(CommandContext ctx)
{
try
{
if (ctx.Message.Attachments.Any())
LogParsingHandler.EnqueueLogProcessing(ctx.Client, ctx.Channel, ctx.Message, ctx.Member, true, true);
else if (ctx.Message.ReferencedMessage is {} refMsg && refMsg.Attachments.Any())
LogParsingHandler.EnqueueLogProcessing(ctx.Client, ctx.Channel, refMsg, ctx.Member, true, true);
}
catch (Exception e)
{
Config.Log.Warn(e);
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
}
}
[Command("badupdate"), Aliases("bad", "recall"), RequiresBotModRole]
[Description("Toggles new update announcement as being bad")]
public async Task BadUpdate(CommandContext ctx, [Description("Link to the update announcement")] string updateMessageLink)
{
var msg = await ctx.GetMessageAsync(updateMessageLink).ConfigureAwait(false);
var embed = msg?.Embeds?.FirstOrDefault();
if (embed == null)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Invalid update announcement link").ConfigureAwait(false);
return;
}
await ToggleBadUpdateAnnouncementAsync(msg).ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
}
public static async Task ToggleBadUpdateAnnouncementAsync(DiscordMessage? message)
{
var embed = message?.Embeds?.FirstOrDefault();
if (message is null || embed is null)
return;
var result = new DiscordEmbedBuilder(embed);
const string warningTitle = "Warning!";
if (embed.Color.Value.Value == Config.Colors.UpdateStatusGood.Value)
{
result = result.WithColor(Config.Colors.UpdateStatusBad);
result.ClearFields();
var warned = false;
foreach (var f in embed.Fields)
{
if (!warned && f.Name.EndsWith("download"))
{
result.AddField(warningTitle, "This build is known to have severe problems, please avoid downloading.");
warned = true;
}
result.AddField(f.Name, f.Value, f.Inline);
}
}
else if (embed.Color.Value.Value == Config.Colors.UpdateStatusBad.Value)
{
result = result.WithColor(Config.Colors.UpdateStatusGood);
result.ClearFields();
foreach (var f in embed.Fields)
{
if (f.Name == warningTitle)
continue;
result.AddField(f.Name, f.Value, f.Inline);
}
}
await message.UpdateOrCreateMessageAsync(message.Channel, embed: result).ConfigureAwait(false);
}
private static async Task ReportMessage(CommandContext ctx, string? comment, DiscordMessage msg)
{
if (msg.Reactions.Any(r => r.IsMe && r.Emoji == Config.Reactions.Moderated))
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Already reported").ConfigureAwait(false);
return;
}
var member = await ctx.Client.GetMemberAsync(ctx.Message.Author).ConfigureAwait(false);
await ctx.Client.ReportAsync("👀 Message report", msg, new[] { member }, comment, ReportSeverity.Medium).ConfigureAwait(false);
await msg.ReactWithAsync(Config.Reactions.Moderated).ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success, "Message reported").ConfigureAwait(false);
}
}