summaryrefslogtreecommitdiff
path: root/DiscoBot/Auxiliary/CommandExtension.cs
diff options
context:
space:
mode:
authorTrueDoctor <d-kobert@web.de>2018-04-09 00:54:19 +0200
committerTrueDoctor <d-kobert@web.de>2018-04-09 00:54:19 +0200
commit82f07c959dc7a87251b4617e462003471e3cc071 (patch)
tree37c397e9d7ae74c74b2afad3dae940237327b755 /DiscoBot/Auxiliary/CommandExtension.cs
parentcac1ade8763605c3cf09859a48358cab0e00027a (diff)
Refactoring and Cleanup
Diffstat (limited to 'DiscoBot/Auxiliary/CommandExtension.cs')
-rw-r--r--DiscoBot/Auxiliary/CommandExtension.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/DiscoBot/Auxiliary/CommandExtension.cs b/DiscoBot/Auxiliary/CommandExtension.cs
new file mode 100644
index 0000000..6690d03
--- /dev/null
+++ b/DiscoBot/Auxiliary/CommandExtension.cs
@@ -0,0 +1,46 @@
+namespace DiscoBot.Auxiliary
+{
+ using System;
+ using System.Collections.Generic;
+ using System.ComponentModel;
+ using System.Linq;
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ using Discord;
+ using Discord.Commands;
+
+ public static class CommandExtension
+ {
+ public static async Task ReplyTimedAsync(this ModuleBase m, string message, TimeSpan time)
+ {
+ var token = message.GetHashCode();
+ var send = m.Context.Channel.SendMessageAsync($"#{token}\n```xl\n{message}```", true);
+
+ var barInvoker = new BackgroundWorker();
+ barInvoker.DoWork += delegate
+ {
+ Thread.Sleep(time);
+ Delete(token, m);
+ };
+
+ await send;
+ barInvoker.RunWorkerAsync();
+ }
+
+ private static void Delete(int token, ModuleBase m)
+ {
+ var messagesAsync = m.Context.Channel.GetMessagesAsync();
+ Task.WaitAll(messagesAsync.ToArray());
+ var list = messagesAsync.ToEnumerable().ToList();
+ var messages = new List<IMessage>();
+ foreach (var task in list)
+ {
+ messages.AddRange(task.ToList());
+ }
+
+ m.Context.Channel.DeleteMessagesAsync(
+ messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot));
+ }
+ }
+}