diff options
author | TrueDoctor <d-kobert@web.de> | 2018-04-08 20:23:57 +0200 |
---|---|---|
committer | TrueDoctor <d-kobert@web.de> | 2018-04-08 20:23:57 +0200 |
commit | 23d2ede6124b0a7b10a74058a396477d52941337 (patch) | |
tree | d711ba83f6c17dc0065cb6c7b065ad27ef5b237f /DiscoBot/CommandExtension.cs | |
parent | 27a7038458f7ae0bcb9f2b4065940308b89fc172 (diff) |
Did a lot of awsome stuff
Diffstat (limited to 'DiscoBot/CommandExtension.cs')
-rw-r--r-- | DiscoBot/CommandExtension.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/DiscoBot/CommandExtension.cs b/DiscoBot/CommandExtension.cs new file mode 100644 index 0000000..06eec92 --- /dev/null +++ b/DiscoBot/CommandExtension.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiscoBot +{ + using System.ComponentModel; + using System.Threading; + using Discord; + using Discord.Commands; + + public static class CommandExtension + { + public static async Task ReplyTimed(this ModuleBase m, string message, TimeSpan time) + { + var token = message.GetHashCode(); + var send = m.Context.Channel.SendMessageAsync($"#{token}\n```xl\n{message}```", true); + + BackgroundWorker barInvoker = new BackgroundWorker(); + barInvoker.DoWork += delegate + { + Thread.Sleep(time); + delete(token, m); + }; + + await send; + barInvoker.RunWorkerAsync(); + } + + private static async void delete(int token, ModuleBase m) + { + var messagesAsync = m.Context.Channel.GetMessagesAsync(100); + Task.WaitAll(messagesAsync.ToArray()); + var list = messagesAsync.ToEnumerable().ToList(); + var messages = new List<IMessage>(); + foreach (var task in list) + { + messages.AddRange(task.ToList()); + } + + await m.Context.Channel.DeleteMessagesAsync(messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot)); + } + } + +} |