-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalModule.cs
More file actions
446 lines (391 loc) · 16.8 KB
/
ExternalModule.cs
File metadata and controls
446 lines (391 loc) · 16.8 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using System;
using System.Diagnostics;
using Discord.WebSocket;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Discord;
using System.Globalization;
using System.Linq;
using Discord.Rest;
using Discord.Commands;
using Slothu;
using Newtonsoft.Json;
namespace SlothuExtras
{
class ExternalModule
{
private static readonly string local_dir = @"D:\DiscordBotFiles\";
private static readonly string host_dir = "/home/ec2-user/BotFiles/";
private static readonly string _rebootScript = local_dir + @"reboot.bat";
private static readonly string host_reboot = host_dir + @"reboot.bat";
DiscordSocketClient _client;
public ExternalModule() { }
public ExternalModule(DiscordSocketClient Client) => _client = Client;
public void KillAllProcesses(string ProcessName)
{
foreach (Process p in Process.GetProcessesByName(ProcessName))
{
p.Kill();
}
}
public void KillAllProcesses(string ProcessName, int PID_Exception)
{
foreach (Process p in Process.GetProcessesByName(ProcessName))
{
if (PID_Exception != p.Id) {
p.Kill();
}
}
}
public async Task Logout(DiscordSocketClient client)
{
Console.WriteLine("Logging out...");
await client.LogoutAsync();
}
public async Task Shutdown(DiscordSocketClient client)
{
KillAllProcesses("bash");
KillAllProcesses("dotnet");
await Logout(client);
}
public async Task Reboot(DiscordSocketClient client)
{
ProcessStartInfo info = new ProcessStartInfo("cmd.exe", "/K " + _rebootScript);
Process newApp = new Process();
newApp = Process.Start(info);
await Task.Delay(2000);
KillAllProcesses("dotnet", newApp.Id);
await Logout(client);
}
public string CreateFolder(string SourceDirectory, string FolderName) // "D:\DiscordBotGuilds\", "foldername"
{
string pathFile = Path.Combine(SourceDirectory, FolderName);
if (Directory.Exists(pathFile)) return pathFile;
else Directory.CreateDirectory(pathFile);
return (Directory.Exists(pathFile)) ? pathFile : null;
}
public string CreateFile(string directory, string fileName) // "D:\DiscordBotGuilds\guildId" ; "guildId-info.txt"
{
string filePath = directory + @"\" + fileName; // "D:\DiscordGuildBots\foldername\guildId-info.txt
if (File.Exists(filePath))
{
return filePath;
}
else
{
FileStream fs = File.Create(filePath);
fs.Close();
}
return filePath;
}
public async Task<string> ReadFile(string filePath)
{
string fileContent;
FileStream source = File.OpenRead(filePath);
byte[] result = new byte[source.Length];
await source.ReadAsync(result, 0, (int)source.Length);
fileContent = Encoding.ASCII.GetString(result);
source.Close();
return fileContent;
}
public void WriteFile(string filePath, string text)
{
File.WriteAllText(filePath, text);
}
public void AppendFile(string filePath, string text)
{
File.AppendAllText(filePath, text);
}
public SocketTextChannel GetDefaultChannel(SocketGuild Guild)
{
SocketTextChannel DefaultOrOldest = Guild.DefaultChannel;
foreach (SocketTextChannel Channel in Guild.TextChannels)
{
if (DefaultOrOldest == null)
{
DefaultOrOldest = Channel;
}
else
{
if (Channel.CreatedAt.Ticks < DefaultOrOldest.CreatedAt.Ticks)
{
DefaultOrOldest = Channel;
}
}
}
return DefaultOrOldest;
}
public int OnlineMemberCount(IReadOnlyCollection<SocketUser> Users)
{
int OnlineCount = 0;
foreach (SocketUser User in Users)
{
if (User.Status != UserStatus.Offline && User.Status != UserStatus.Invisible)
{
OnlineCount++;
}
}
return OnlineCount;
}
public int GetTotalHumans(IReadOnlyCollection<SocketUser> Users)
{
int TotalHumans = 0;
foreach (SocketUser User in Users)
{
if (User.IsBot == false)
{
TotalHumans++;
}
}
return TotalHumans;
}
public string CreateGuildFolder(ulong guildId)
{
ExternalModule em = new ExternalModule();
string Directory = @"D:\DiscordBotGuilds\";
//string Directory = @"/home/ec2-user/GuildFolders/";
string FolderName = guildId.ToString();
string pathFile = em.CreateFolder(Directory, FolderName);
return pathFile;
}
public void CreateInitialGuildFolders(string GuildDirectory)
{
ExternalModule EM = new ExternalModule();
string[] InitialFolders =
{
"Bossing",
"CustomResponses",
"Reminders"
};
foreach (string folderName in InitialFolders)
{
EM.CreateFolder(GuildDirectory, folderName);
}
}
public void CreateInitialFiles(string directory)
{
ExternalModule em = new ExternalModule();
string[] InitialFiles = { "MorningMessage.txt", "EveningMessage.txt", "MorningTime.txt", "EveningTime.txt", "DefaultChannel.txt" };
foreach (string fileName in InitialFiles)
{
em.CreateFile(directory, fileName);
}
}
public async void TimedMessageAsync() // Morning and Night Messages
{
IReadOnlyCollection<SocketGuild> Guilds = _client.Guilds;
ExternalModule EM = new ExternalModule();
string MorningMessageFile;
string EveningMessageFile;
string MorningTimeFile;
string EveningTimeFile;
string MorningMessage = "";
string EveningMessage = "";
string MorningTime = "";
string EveningTime = "";
foreach (SocketGuild Guild in Guilds)
{
ulong GuildId = Guild.Id;
string GuildName = Guild.Name;
string FolderPath = @"D:\DiscordBotGuilds\" + Guild.Id.ToString();
MorningMessageFile = FolderPath + @"\MorningMessage.txt";
EveningMessageFile = FolderPath + @"\EveningMessage.txt";
MorningTimeFile = FolderPath + @"\MorningTime.txt";
EveningTimeFile = FolderPath + @"\EveningTime.txt";
MorningMessage = await EM.ReadFile(MorningMessageFile);
EveningMessage = await EM.ReadFile(EveningMessageFile);
MorningTime = await EM.ReadFile(MorningTimeFile);
EveningTime = await EM.ReadFile(EveningTimeFile);
AwaitingCorrectTime(GuildId);
}
}
public async void AwaitingCorrectTime(ulong GuildId)
{
ExternalModule EM = new ExternalModule();
SocketGuild Guild = _client.GetGuild(GuildId);
string GuildName = Guild.Name;
string GuildDirectory = @"D:\DiscordBotGuilds\" + GuildId.ToString();
while (_client.LoginState == LoginState.LoggedIn)
{
string MorningTime = await EM.ReadFile(GuildDirectory + @"\MorningTime.txt");
string EveningTime = await EM.ReadFile(GuildDirectory + @"\EveningTime.txt");
string MorningMessage = await EM.ReadFile(GuildDirectory + @"\MorningMessage.txt");
string EveningMessage = await EM.ReadFile(GuildDirectory + @"\EveningMessage.txt");
string ChannelIdString = await EM.ReadFile(GuildDirectory + @"\DefaultChannel.txt");
ulong ChannelId = UInt64.TryParse(ChannelIdString, NumberStyles.Number, null, out ulong result) ? Convert.ToUInt64(ChannelIdString) : 0;
SocketTextChannel Channel = Guild.GetTextChannel(ChannelId);
string UtcNow = DateTime.UtcNow.ToShortTimeString();
if (UtcNow.Equals(MorningTime))
{
await CheckMessageSent(Channel, MorningMessage);
}
if (UtcNow.Equals(EveningTime))
{
await CheckMessageSent(Channel, EveningMessage);
}
await Task.Delay(60000);
}
}
public async Task CheckMessageSent(SocketTextChannel Channel, string Content)
{
IEnumerable<IMessage> ChannelMessages = Channel.GetMessagesAsync(10).Flatten().Result;
DateTime Now = DateTime.UtcNow;
bool MessageSent = false;
foreach (IMessage Message in ChannelMessages)
{
DateTime SentAt = Message.Timestamp.DateTime;
if (Message.Content.Equals(Content)
&& Now.AddMinutes(-5) < SentAt)
{
MessageSent = true;
break;
}
}
if (!MessageSent)
{
await Channel.SendMessageAsync(Content);
}
}
public async Task ModerationAction(IUser Target, IUser Sender, SocketTextChannel Channel, string Content, string Footer)
{
EmbedBuilder Embed = new EmbedBuilder();
Embed.WithAuthor(Sender.Username);
Embed.WithColor(new Color(0, 170, 255));
Embed.WithCurrentTimestamp();
Embed.AddField("Moderation Action!", Content, true);
Embed.WithFooter(Footer);
await Channel.SendMessageAsync("", false, Embed);
}
public async Task Unmute(int Seconds, SocketGuildUser User, SocketGuildUser Sender, IRole Role)
{
await Task.Delay(Seconds * 60 * 1000);
if (!(User.Roles.Contains(Role)))
{
Console.WriteLine("User is already unmuted!");
return;
}
else
{
await User.RemoveRoleAsync(Role);
await ModerationAction(
User,
Sender,
GetDefaultChannel(User.Guild),
$"{User.Mention} has been unmuted",
"This action was performed automatically"
);
Console.WriteLine($"Unmuted {User.Username}#{User.DiscriminatorValue}");
}
}
public async Task ModifyGuildChannels(SocketGuild Guild, IRole Role)
{
IReadOnlyCollection<SocketTextChannel> Channels = Guild.TextChannels;
OverwritePermissions Perms = new OverwritePermissions(sendMessages: PermValue.Deny);
foreach (SocketTextChannel Channel in Channels)
{
await Channel.AddPermissionOverwriteAsync(Role, Perms);
}
}
public SocketRole RoleExists(SocketGuild Guild, string RoleName)
{
foreach (SocketRole Role in Guild.Roles)
{
if (Role.Name.Equals(RoleName))
{
Console.WriteLine("Role found!");
return Role;
}
}
Console.WriteLine("Role does not exist");
return null;
}
public async Task<RestRole> CreateRole(SocketGuild Guild, string Name, ulong Value, Color Colour)
{
Console.WriteLine($"Creating role with name {Name}");
RestRole Role = await Guild.CreateRoleAsync(Name, new GuildPermissions(Value), Colour);
return Role;
}
public void SetPrefix(string Prefix)
{
Environment.SetEnvironmentVariable("prefix", Prefix);
}
public string GetPrefix()
{
return Environment.GetEnvironmentVariable("prefix");
}
public async Task Bossing(SocketCommandContext context, string boss, int time, List<SocketUser> team)
{
string GuildID = context.Guild.Id.ToString();
string BossingFolder = $@"D:\DiscordBotGuilds\{GuildID}\Bossing";
//string BossingFolder = $@"/home/ec2-user/GuildFolders/{GuildID}/Bossing";
string SessionsFolder = CreateFolder(BossingFolder, "Sessions");
int sessionCount = Directory.GetFiles(BossingFolder + @"\Sessions").Length + 1;
Session Session = new Session(context, boss, team, sessionCount, time);
string JSON = JsonConvert.SerializeObject(Session);
CreateFile(SessionsFolder, "session" + sessionCount.ToString() + ".json");
WriteFile(SessionsFolder + $@"\session{sessionCount}.json", JSON);
await Session.ViewSession(context);
}
public async Task CreateSession(SocketCommandContext context)
{
string GuildID = context.Guild.Id.ToString();
string BossingFolder = $@"D:\DiscordBotGuilds\{GuildID}\Bossing";
//string BossingFolder = $@"/home/ec2-user/GuildFolders/{GuildID}/Bossing";
string SessionsFolder = CreateFolder(BossingFolder, "Sessions");
int sessionCount = Directory.GetFiles(BossingFolder + @"\Sessions").Length + 1;
}
public string GrabSessionJSON(SocketCommandContext Context, int SessionID)
{
string Folder = $@"D:\DiscordBotGuilds\{Context.Guild.Id}\Bossing\Sessions";
//string Folder = $@"/home/ec2-user/GuildFolders/{Context.Guild.Id}/Bossing/Sessions";
string FileName = $"session{SessionID}";
string FilePath = Folder + $@"\{FileName}.json";
bool doesExist = File.Exists(FilePath);
if (!doesExist) return null;
string JSON = ReadFile(FilePath).GetAwaiter().GetResult();
return JSON;
}
public Session GetSession(SocketCommandContext Context, int SessionID)
{
string Folder = $@"D:\DiscordBotGuilds\{Context.Guild.Id}\Bossing\Sessions";
//string Folder = $@"/home/ec2-user/GuildFolders/{Context.Guild.Id}/Bossing/Sessions";
string FileName = $"session{SessionID}";
string FilePath = Folder + $@"\{FileName}.json";
bool doesExist = File.Exists(FilePath);
if (!doesExist) return null;
string JSON = ReadFile(FilePath).GetAwaiter().GetResult();
Session session = new Session();
session = JsonConvert.DeserializeObject<Session>(JSON);
session.Deserialize(Context);
return session;
}
public Session[] GetAllSessions(SocketCommandContext Context)
{
string Folder = $@"D:\DiscordBotGuilds\{Context.Guild.Id}\Bossing\Sessions";
//string Folder = $@"/home/ec2-user/GuildFolders/{Context.Guild.Id}/Bossing/Sessions";
int numberOfSessions = Directory.GetFiles(Folder).Count();
if (numberOfSessions == 0)
return new Session[0];
Console.WriteLine("There are {0} sessions", numberOfSessions);
List<Session> Sessions = new List<Session>();
for (int i = 0; i < numberOfSessions; i++)
{
string FileName = $"session{i+1}";
string FilePath = Folder + $@"\{FileName}.json";
string JSON = ReadFile(FilePath).GetAwaiter().GetResult();
Session session = new Session();
session = JsonConvert.DeserializeObject<Session>(JSON);
session.Deserialize(Context);
Sessions.Add(session);
}
return Sessions.ToArray();
}
public bool IsInActiveSession(SocketUser caller, Session session)
{
List<SocketUser> Team = session.GetTeam();
return (Team.Contains(caller) && session.Active==true);
}
}
}