/
Censor.cs
288 lines (251 loc) · 11.2 KB
/
Censor.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using Discord;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SwissbotCore.Handlers.AutoMod
{
[DiscordHandler]
public class Censor
{
private DiscordSocketClient client;
public static List<string> WhiteList = new List<string>();
public Censor(DiscordSocketClient c)
{
client = c;
client.MessageReceived += Client_MessageReceived;
client.MessageUpdated += Client_MessageUpdated;
WhiteList = LoadWhiteList().Result;
Task.Run(async () =>
{
try
{
var cmd = await client.Rest.CreateGuildCommand(new SlashCommandCreationProperties()
{
Name = "whitelist",
Description = "Adds a word to the whitelist that blocks a word from the censor",
Options = new List<ApplicationCommandOptionProperties>()
{
new ApplicationCommandOptionProperties()
{
Type = ApplicationCommandOptionType.SubCommand,
Description = "Adds a word to the whitelist",
Name = "Add",
Options = new List<ApplicationCommandOptionProperties>()
{
new ApplicationCommandOptionProperties()
{
Name = "Word",
Type = ApplicationCommandOptionType.String,
Required = true,
Description = "The word to add to the whitelist"
}
}
},
new ApplicationCommandOptionProperties()
{
Type = ApplicationCommandOptionType.SubCommand,
Description = "Removes a word to the whitelist",
Name = "Remove",
Options = new List<ApplicationCommandOptionProperties>()
{
new ApplicationCommandOptionProperties()
{
Name = "Word",
Type = ApplicationCommandOptionType.String,
Required = true,
Description = "The word to add to the whitelist"
}
}
},
new ApplicationCommandOptionProperties()
{
Type = ApplicationCommandOptionType.SubCommand,
Description = "Lists the current whitelist",
Name = "List",
},
}
}, Global.SwissGuildId);
}
catch (Exception x)
{
}
});
client.InteractionCreated += Client_InteractionCreated;
}
private async Task Client_MessageUpdated(Cacheable<IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3)
=> await Client_MessageReceived(arg2);
private async Task Client_InteractionCreated(SocketInteraction arg)
{
if (arg.Data.Name == "whitelist")
{
if (!Program.UserHasPerm(arg.Member))
{
await arg.FollowupAsync("Invalid permission fucker, go sit on a pinecone dickhead");
return;
}
if (arg.Data.Options.Count == 1)
{
var opt = arg.Data.Options.First();
switch (opt.Name)
{
case "add":
{
var word = (string)opt.Options.First().Value;
if (WhiteList.Contains(word))
{
await arg.FollowupAsync("", false, new EmbedBuilder()
{
Title = "No.. sex",
Color = Color.Red,
Description = $"The word `{word}` is already in the whitelist!"
}.WithCurrentTimestamp().Build());
return;
}
AddWhitelist(word);
await arg.FollowupAsync("", false, new EmbedBuilder()
{
Title = "Success!",
Color = Color.Green,
Description = $"Added `{word}` to the whitelist!"
}.WithCurrentTimestamp().Build());
return;
}
case "remove":
{
var word = (string)opt.Options.First().Value;
if (!WhiteList.Contains(word))
{
await arg.FollowupAsync("", false, new EmbedBuilder()
{
Title = "No.. sex",
Color = Color.Red,
Description = $"The word `{word}` is not in the whitelist!"
}.WithCurrentTimestamp().Build());
return;
}
RemoveWhitelist(word);
await arg.FollowupAsync("", false, new EmbedBuilder()
{
Title = "Success!",
Color = Color.Green,
Description = $"Removed `{word}` from the whitelist!"
}.WithCurrentTimestamp().Build());
return;
}
case "list":
await arg.FollowupAsync("", false, new EmbedBuilder()
{
Title = "Whitelist",
Color = Color.Green,
Description = $"Heres the current whitelist:\n```\n{string.Join("\n", WhiteList)}```"
}.WithCurrentTimestamp().Build());
return;
}
}
}
}
public void AddWhitelist(string item)
{
if (!WhiteList.Contains(item))
WhiteList.Add(item);
SaveWhitelist();
}
public void RemoveWhitelist(string item)
{
if (WhiteList.Contains(item))
WhiteList.Remove(item);
SaveWhitelist();
}
public void SaveWhitelist()
=> SwissbotStateHandler.SaveObject("Whitelist.json", WhiteList);
public async Task<List<string>> LoadWhiteList()
{
try
{
return await SwissbotStateHandler.LoadObject<List<string>>("Whitelist.json");
}
catch (Exception x)
{
Global.ConsoleLog("Failed to load whitelist, returning empty");
return new List<string>();
}
}
private async Task Client_MessageReceived(SocketMessage arg)
{
if (CheckCensor(arg))
{
var smsg = arg.Content;
if (smsg.Length > 1023)
{
var s = smsg.Take(1020);
smsg = new string(s.ToArray()) + "...";
}
EmbedBuilder b = new EmbedBuilder()
{
Title = "Censor Alert",
Color = Color.Orange,
Fields = new List<EmbedFieldBuilder>()
{
{new EmbedFieldBuilder()
{
Name = "Message",
Value = smsg
} },
{new EmbedFieldBuilder()
{
Name = "Author",
Value = arg.Author.ToString() + $" ({arg.Author.Id})"
} },
{new EmbedFieldBuilder()
{
Name = "Action",
Value = "Message Deleted",
} },
{new EmbedFieldBuilder()
{
Name = "Channel",
Value = $"<#{arg.Channel.Id}>"
} },
{ new EmbedFieldBuilder()
{
Name = "Jump to timeline",
Value = arg.Channel.GetMessagesAsync(2).FlattenAsync().Result.Last().GetJumpUrl()
} }
}
};
await arg.DeleteAsync();
foreach (var item in Global.CensoredWords)
if (arg.Content.ToLower().Contains(item.ToLower()))
b.Fields.Add(new EmbedFieldBuilder() { Name = "Field", Value = item });
await client.GetGuild(Global.SwissGuildId).GetTextChannel(665647956816429096).SendMessageAsync("", false, b.Build());
}
}
public bool CheckCensor(SocketMessage arg)
{
if (arg.Author.IsBot) { return false; }
var g = client.GetGuild(Global.SwissGuildId).GetUser(arg.Author.Id);
if (g == null)
return false;
var r = g.Roles;
if(Program.UserHasPerm(arg.Author.Id))
{ return false; }
string cont = arg.Content.ToLower();
foreach (var item in Global.CensoredWords)
if (cont.Contains(item.ToLower()))
{
string word = "";
if (cont.Contains(' '))
word = cont.Split(' ').FirstOrDefault(x => x.Contains(item.ToLower()));
else
word = cont;
if (WhiteList.Any(x => x.ToLower() == word.ToLower() || x.ToLower().Contains(word.ToLower())))
return false;
return true;
}
return false;
}
}
}