· 6 years ago · Jun 27, 2019, 10:16 AM
1using System;
2using System.IO;
3using System.Reflection;
4using System.Threading.Tasks;
5using Discord;
6using Discord.Commands;
7using Discord.WebSocket;
8using Discord.Net;
9
10namespace discbot
11{
12 public class Program
13 {
14
15 public static bool memereact = false, passagain = true, passagainreact = true;
16 static ulong lastid;
17 static ulong messageid;
18 static ulong debugchannel = 593560146370756624;
19 static ulong schedulechannel = 593560036488511507;
20 static EmbedBuilder Embed = new EmbedBuilder();
21
22
23 private DiscordSocketClient Client;
24 private CommandService Commands;
25
26 static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
27
28
29
30 private async Task MainAsync()
31 {
32 Client = new DiscordSocketClient(new DiscordSocketConfig
33 {
34 LogLevel = LogSeverity.Debug
35 });
36
37 Commands = new CommandService(new CommandServiceConfig
38 {
39 CaseSensitiveCommands = false,
40 DefaultRunMode = RunMode.Async,
41 LogLevel = LogSeverity.Debug
42 });
43
44 Client.MessageReceived += Client_CheckTicket;
45 Client.MessageReceived += Client_MessageReceived;
46 Client.ReactionAdded += OnReactionAdded;
47
48 await Commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
49
50 Client.Ready += Client_Ready;
51 Client.Log += Client_Log;
52
53 await Client.LoginAsync(TokenType.Bot, "----");
54 await Client.StartAsync();
55
56
57 await Task.Delay(-1);
58
59 }
60
61 private void NewMethod()
62 {
63 Client.MessageReceived += Client_CheckTicket;
64 }
65
66 private async Task Client_Log(LogMessage msg)
67 {
68 Console.WriteLine($"{DateTime.Now} at {msg.Source} {msg.Message}");
69 }
70
71 private async Task Client_Ready()
72 {
73 await Client.SetGameAsync("Visiting Kim Jong Un");
74 }
75
76 public async Task ReactAsync(SocketUserMessage userMsg)
77 {
78 if (memereact)
79 {
80 string name = userMsg.Author.Username.ToString();
81 Console.WriteLine("Reacting on user: " + name);
82 if (name == "Broccoli")
83 {
84 var emoji = new Emoji("\uD83D\uDEAB");
85 await userMsg.AddReactionAsync(emoji);
86 }
87 else
88 {
89 var emoji = new Emoji("\uD83D\uDCAA");
90 await userMsg.AddReactionAsync(emoji);
91 emoji = new Emoji("\uD83D\uDE02");
92 await userMsg.AddReactionAsync(emoji);
93 emoji = new Emoji("\uD83D\uDC4C");
94 await userMsg.AddReactionAsync(emoji);
95 emoji = new Emoji("\uD83D\uDCAF");
96 await userMsg.AddReactionAsync(emoji);
97 }
98 }
99
100
101
102 }
103
104 public async Task DebugMessage(string a)
105 {
106 Console.WriteLine("Getting server");
107 var server = Client.GetGuild(561221673924427796);
108 Console.WriteLine("Getting chat");
109 var chat = server.GetTextChannel(debugchannel);
110 Console.WriteLine("Writing to Chat");
111 await chat.SendMessageAsync(a);
112 Console.WriteLine("Succeeded");
113 Console.WriteLine(a);
114 }
115
116 public async Task ScheduleMessage(string a, bool b = false)
117 {
118 Console.WriteLine("Getting server");
119 var server = Client.GetGuild(561221673924427796);
120 Console.WriteLine("Getting chat");
121 var chat = server.GetTextChannel(schedulechannel);
122 Console.WriteLine("Writing to Chat");
123 if (b)
124 {
125 await chat.SendMessageAsync(a, false, Embed.Build());
126 }
127 else
128 {
129 await chat.SendMessageAsync(a);
130 }
131 Console.WriteLine("Succeeded");
132 }
133
134 private async Task OnReactionAdded(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction thereaction)
135 {
136 if (passagainreact && thereaction.Channel.Id != debugchannel || thereaction.Channel.Id != schedulechannel)
137 {
138 await DebugMessage("Messageid: " + thereaction.Channel.Id.ToString());
139 passagainreact = false;
140 await DebugMessage("React func started");
141
142 Console.WriteLine("React func started");
143 string path = @"C:\Users\Alex\source\repos\discbot\messageid.txt";
144 string path2 = @"C:\Users\Alex\source\repos\discbot\ids.txt";
145 string s = "";
146
147
148 using (StreamReader sr = File.OpenText(path))
149 {
150 lastid = new ulong();
151 try
152 {
153 while ((s = sr.ReadLine()) != null)
154 {
155 var tests = s.Split(" ");
156 ulong thekeytodark = ulong.Parse(tests[0]);
157 if (thekeytodark != lastid)
158 {
159 await DebugMessage("Checking if " + thereaction.MessageId + " : " + thekeytodark + " matches");
160 lastid = thekeytodark;
161 if (thereaction.MessageId == thekeytodark)
162 {
163 var emotes = thereaction.Emote;
164 string stuff = "";
165 switch (emotes.ToString())
166 {
167 case "?":
168 using (StreamReader read = File.OpenText(path))
169 {
170 await DebugMessage("Trying to match id's");
171 while((stuff = read.ReadLine()) != null)
172 {
173 var tester = stuff.Split(" ");
174 if (tester[0] == thekeytodark.ToString())
175 {
176 using (StreamReader reads = File.OpenText(path2))
177 {
178 string shait = " ";
179 while ((shait = reads.ReadLine()) != null)
180 {
181 var testing = shait.Split(",!");
182 if(tester[1] == testing[0])
183 {
184 await DebugMessage("Variables for embed: " + shait);
185 await DebugMessage("Found!");
186 try
187 {
188 await DebugMessage(stuff);
189 Embed = new EmbedBuilder();
190 Embed.WithAuthor("Request: " + testing[2] + " for " + testing[14] + " department", testing[12]);
191 Embed.WithColor(Int32.Parse(testing[9]), Int32.Parse(testing[10]), Int32.Parse(testing[11]));
192 Embed.WithFooter("Requested by: " + testing[2] + " at " + DateTime.Now, testing[12]);
193 Embed.WithDescription(testing[6]);
194 }
195 catch (Exception e)
196 {
197 read.Close();
198 reads.Close();
199 await DebugMessage("Error while trying to make a embed: " + e);
200 return;
201 }
202 }
203
204 }
205
206
207 }
208
209
210 }
211 }
212
213 }
214
215
216 await ScheduleMessage("A request at " + thereaction.Channel.Name + " was completed!!", true);
217 await thereaction.Channel.DeleteMessageAsync(thereaction.MessageId);
218 sr.Close();
219 passagainreact = true;
220 return;
221
222 case "?":
223 await ScheduleMessage("the work for a request from " + thereaction.Channel.Name + " has been started!!");
224 sr.Close();
225 passagainreact = true;
226 return;
227
228 case "⛔":
229 using (StreamReader read = File.OpenText(path))
230 {
231 await DebugMessage("Trying to match id's");
232 while ((stuff = read.ReadLine()) != null)
233 {
234 var tester = stuff.Split(" ");
235 if (tester[0] == thekeytodark.ToString())
236 {
237 using (StreamReader reads = File.OpenText(path2))
238 {
239 string shait = " ";
240 while ((shait = reads.ReadLine()) != null)
241 {
242 var testing = shait.Split(",!");
243 if (tester[1] == testing[0])
244 {
245 await DebugMessage("Variables for embed: " + shait);
246 await DebugMessage("Found!");
247 try
248 {
249 await DebugMessage(stuff);
250 Embed = new EmbedBuilder();
251 Embed.WithAuthor("Request: " + testing[2] + " for " + testing[14] + " department", testing[12]);
252 Embed.WithColor(Int32.Parse(testing[9]), Int32.Parse(testing[10]), Int32.Parse(testing[11]));
253 Embed.WithFooter("Requested by: " + testing[2] + " at " + DateTime.Now, testing[12]);
254 Embed.WithDescription(testing[6]);
255 }
256 catch (Exception e)
257 {
258 read.Close();
259 reads.Close();
260 await DebugMessage("Error while trying to make a embed: " + e);
261 return;
262 }
263 }
264
265 }
266
267
268 }
269
270
271 }
272 }
273
274 }
275 await ScheduleMessage("the work for a request from " + thereaction.Channel.Name + " has been DENIED!!", true);
276 await thereaction.Channel.DeleteMessageAsync(thereaction.MessageId);
277 sr.Close();
278 passagainreact = true;
279 return;
280
281 default:
282 await DebugMessage("Reaction or emoji at " + thereaction.Channel.Name + " was not a listed one so i wont to do anything");
283 sr.Close();
284 passagainreact = true;
285 return;
286
287
288 }
289
290 }
291 }
292 }
293 }
294 catch (Exception e)
295 {
296 await channel.SendMessageAsync("Something went wrong... Exception was sent to console!");
297 Console.WriteLine(e);
298 passagainreact = true;
299 return;
300 }
301 sr.Close();
302 passagainreact = true;
303 return;
304 }
305 }
306 passagainreact = true;
307 return;
308 }
309
310
311 public async Task Client_CheckTicket(SocketMessage arg)
312 {
313 if (passagain && (!(arg.Channel.Id == debugchannel || arg.Channel.Id == schedulechannel)))
314 {
315 await DebugMessage("Messageid: " + arg.Channel.Id.ToString());
316 passagain = false;
317 try
318 {
319 var Message = arg as SocketUserMessage;
320 var Context = new SocketCommandContext(Client, Message);
321 ulong secretkey = new ulong();
322 if (!(arg.Channel.Id != debugchannel || arg.Channel.Id != schedulechannel))
323 {
324 Console.WriteLine("Detected a debug message i wont be taking any actions against it");
325 passagain = true;
326 return;
327 }
328
329 if (Context.User.IsBot)
330 {
331
332 String s = "";
333 String path = @"C:\Users\Alex\source\repos\discbot\ids.txt";
334
335 using (StreamReader sr = File.OpenText(path))
336 {
337
338 try
339 {
340
341 while ((s = sr.ReadLine()) != null)
342 {
343
344 await DebugMessage("Checking this message: " + Message.Content);
345 var testing = s.Split(",!");
346 if (Context.Message.Content.Contains(testing[0]))
347 {
348 messageid = Message.Id;
349 secretkey = ulong.Parse(testing[0]);
350 await DebugMessage("added");
351 }
352
353 }
354
355 }
356 catch (Exception e)
357 {
358 await DebugMessage("Something went wrong... Exception was sent to console!");
359 Console.WriteLine(e);
360 passagain = true;
361 return;
362 }
363 sr.Close();
364
365 }
366 if (messageid != 0)
367 {
368 path = @"C:\Users\Alex\source\repos\discbot\messageid.txt";
369 using (StreamWriter sr = File.AppendText(path))
370 {
371 sr.WriteLine(messageid + " " + secretkey);
372 sr.Close();
373 }
374 }
375 else
376 {
377 await DebugMessage("MessageID was 0 so i will not add it to my database");
378 passagain = true;
379 return;
380 }
381
382 }
383 else
384 {
385 await DebugMessage("No pass");
386 }
387 }
388 catch (Exception e)
389 {
390 await DebugMessage(e.ToString());
391 }
392 }
393 passagain = true;
394 return;
395 }
396
397 private async Task Client_MessageReceived(SocketMessage arg)
398 {
399
400 var Message = arg as SocketUserMessage;
401 await ReactAsync(Message);
402 var Context = new SocketCommandContext(Client, Message);
403
404 if (Context.Message == null || Context.Message.Content == "") return;
405 if (Context.User.IsBot) return;
406
407 int ArgPos = 0;
408 if (!(Message.HasStringPrefix("nuked!", ref ArgPos) || Message.HasMentionPrefix(Client.CurrentUser, ref ArgPos))) return;
409
410
411 var Result = await Commands.ExecuteAsync(Context, ArgPos, null);
412 if (!Result.IsSuccess)
413 {
414 Console.WriteLine($"{DateTime.Now} at Commands Something went wrong with executing a nuke launch. Text: {Context.Message.Content} | Error: {Result.ErrorReason}");
415 await Context.Channel.SendMessageAsync($"{DateTime.Now} at Commands Something went wrong with executing a nuke launch. Text: {Context.Message.Content} | Error: {Result.ErrorReason}");
416 }
417
418 }
419
420
421
422 }
423
424}