From e6181c24124d97f2fbc932b8a68311e625463156 Mon Sep 17 00:00:00 2001 From: uzvkl Date: Tue, 11 Jun 2019 23:05:52 +0200 Subject: Move dsa related stuff to subfolder --- dsa/DiscoBot/Commands/CommandExtension.cs | 119 +++++++++++++++++++ dsa/DiscoBot/Commands/FileHandler.cs | 25 ++++ dsa/DiscoBot/Commands/MiscCommands.cs | 189 ++++++++++++++++++++++++++++++ 3 files changed, 333 insertions(+) create mode 100644 dsa/DiscoBot/Commands/CommandExtension.cs create mode 100644 dsa/DiscoBot/Commands/FileHandler.cs create mode 100644 dsa/DiscoBot/Commands/MiscCommands.cs (limited to 'dsa/DiscoBot/Commands') diff --git a/dsa/DiscoBot/Commands/CommandExtension.cs b/dsa/DiscoBot/Commands/CommandExtension.cs new file mode 100644 index 0000000..098e35f --- /dev/null +++ b/dsa/DiscoBot/Commands/CommandExtension.cs @@ -0,0 +1,119 @@ +namespace DiscoBot.Auxiliary +{ + using System; + using System.Collections.Generic; + using System.ComponentModel; + using System.IO; + using System.Linq; + using System.Net; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + + using Discord; + using Discord.Commands; + + public static class CommandExtension + { + private static WebClient client; + + 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}```", false); + + 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(); + foreach (var task in list) + { + messages.AddRange(task.ToList()); + } + + var test = messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot).Select(c=>c ); + var waiters = new List(); + foreach (var message in test) + { + waiters.Add((message as IUserMessage).DeleteAsync()); + } + Task.WaitAll(waiters.ToArray()); + } + + public static async Task ReplyAsync(this ModuleBase m, IEnumerable message, bool directMessage = false) + { + var sb = new StringBuilder(); + foreach (string re in message) + { + if (sb.Length + re.Length > 1798) + { + if (directMessage) + { + await m.Context.User.SendMessageAsync("```xl\n" + sb + "\n```"); + } + else + { + await m.Context.Channel.SendMessageAsync("```xl\n" + sb + "\n```"); + } + + sb.Clear(); + } + + sb.AppendLine(re); + } + + if (directMessage) + { + await m.Context.User.SendMessageAsync("```xl\n" + sb + "\n```"); + } + else + { + await m.Context.Channel.SendMessageAsync("```xl\n" + sb + "\n```"); + } + } + + public static async Task ReplyAsync(this ModuleBase m, IEnumerable message, TimeSpan time) + { + var sb = new StringBuilder(); + foreach (string re in message) + { + if (sb.Length + re.Length > 1798) + { + + await m.ReplyTimedAsync(sb.ToString(), time); + + + sb.Clear(); + } + + sb.AppendLine(re); + } + + await m.ReplyTimedAsync(sb.ToString(), TimeSpan.FromSeconds(90)); + } + + public static async Task SendWebFile(this IMessageChannel channel, string url = "https://i.imgur.com/0iHEycJ.png") + { + if (client == null) + { + client = new WebClient(); + } + + Stream stream = client.OpenRead(url); + await channel.SendFileAsync(stream, url.Split('/').Last()); + } + } +} diff --git a/dsa/DiscoBot/Commands/FileHandler.cs b/dsa/DiscoBot/Commands/FileHandler.cs new file mode 100644 index 0000000..4f8a785 --- /dev/null +++ b/dsa/DiscoBot/Commands/FileHandler.cs @@ -0,0 +1,25 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Discord.Commands; + +namespace DiscoBot.Commands +{ + public class FileHandler : ModuleBase + { + //[Command("send"), Summary("fügt Helden hinzu")] + public void AddChar() + { + var msg = Context.Message; + if (msg.Attachments == null) throw new ArgumentException("Es wurde keine Datei angehängt"); + + var attachments = msg.Attachments.ToList(); + + if (!attachments.Any(x => x.Filename.EndsWith(".xml"))) + throw new ArgumentException("Es wurde kein xml Held mitgeschickt"); + + foreach (var attachment in attachments.Where(x => x.Filename.EndsWith(".xml"))) + throw new NotImplementedException("send File to Server"); + } + } +} \ No newline at end of file diff --git a/dsa/DiscoBot/Commands/MiscCommands.cs b/dsa/DiscoBot/Commands/MiscCommands.cs new file mode 100644 index 0000000..738796c --- /dev/null +++ b/dsa/DiscoBot/Commands/MiscCommands.cs @@ -0,0 +1,189 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DiscoBot.Auxiliary; +using Discord; +using Discord.Commands; + +namespace DiscoBot.Commands +{ + public class MiscCommands : ModuleBase + { + [Command("r")] + [Summary("Würfelt ")] + [Alias("R", "Roll", "roll", "Würfle")] + public Task RollAsync([Remainder] [Summary("Weapon")] string roll) + { + //return this.ReplyAsync("```xl\n" + new Auxiliary.Calculator.StringSolver(roll).Solve() + "\n```"); + return ReplyAsync("```xl\n" + RandomMisc.Roll(roll) + "\n```"); + } + + + [Command("say")] + [Summary("Echos a message.")] + [Alias("s")] + public Task SayAsync([Remainder] [Summary("The text to echo")] + string echo) + { + return ReplyAsync(echo); + } + + [Command("liebe")] + [Summary("Echos a message.")] + [Alias("Liebe", "<3", "love")] + public async Task LoveAsync() + { + var rand = new Random(); + var user = Context.Channel.GetUsersAsync().ToList().Result.ToList().First() + .Where(x => x.Status != UserStatus.Offline).OrderBy(x => rand.Next()).First(); + await ReplyAsync( + ":heart: :heart: :heart: Verteilt die Liebe! :heart: :heart: :heart: \n Besondere Liebe geht an " + + user.Username); + //await this.ReplyAsync("!liebe"); + } + + [Command("maul")] + [Summary("Echos a message.")] + public Task MaulAsync() + { + return ReplyAsync( + "Maul...? Du meintest doch sicher Maulwürfe oder? \n:heart: :heart: :heart: \nGanz viel Liebe für Maulwürfe !\n:heart: :heart: :heart:"); + } + + + [Command("match")] + [Summary("Tinder.")] + [Alias("mach", "pass", "passt")] + public Task TinderAsync(string s1, string s2) + { + var rand = new Random((s1 + s2).GetHashCode()); + + var wert = Math.Log10(Math.Floor(1000.0 * (SpellCorrect.CompareExact(s1, s2) + rand.NextDouble() * 10.0)) / + 1000.0); + wert = wert * 100.0 < 100.0 ? wert * 100.0 : 100.0 - wert; + wert = wert < 0 ? -wert : wert; + return ReplyAsync($"Ihr passt zu {Math.Floor(100.0 * wert) / 100.0}% zusammen"); + } + + [Command("reddit")] + [Summary("Reddit.")] + public Task RedditAsync() + { + return ReplyAsync( + "Ein Archiv der Vergangenen Aktionen findet man hier: https://www.reddit.com/r/ReconquistaInternet/"); + } + + [Command("compare")] + [Summary("Echos a message.")] + public async Task KickAsync() + { + //await this.Context.Guild.DownloadUsersAsync(); + var users = Context.Guild.GetUsersAsync(); + var test = File.ReadAllLines("RG.txt"); + await users; + var us = users.Result.Select(x => x.Username); + + var lines = test.Where(x => !x.Equals(string.Empty)).ToList(); + + + var sc = new SpellCorrect(); + + var res = new List(); + + foreach (var line in lines) + { + var best = us.OrderBy(user => sc.Compare(user, line)).First(); + + double fit = sc.Compare(best, line); + + if (!(fit < SpellCorrect.ErrorThreshold - 20000)) continue; + res.Add(fit.Equals(0) ? $"@\t{best} !!! => {line}" : $"-\t{best} hat Ähnlichkeit mit: {line}"); + } + + var sb = new StringBuilder(); + foreach (var re in res) + { + if (sb.Length + re.Length > 1798) + { + await this.ReplyTimedAsync(sb.ToString(), TimeSpan.FromSeconds(90)); + sb.Clear(); + } + + sb.AppendLine(re); + } + + if (Permissions.Check(Context, new[] {"Admin", "Mod"})) + await this.ReplyTimedAsync(sb.ToString(), TimeSpan.FromSeconds(90)); + + //await this.ReplyAsync($"{count} Duplikate gefunden"); + } + + + [Command("clear")] + [Summary("Cleans up messages.")] + public void DeleteAsync(int count) + { + var messagesAsync = Context.Channel.GetMessagesAsync(count); + if (messagesAsync != null) + { + Task.WaitAll(messagesAsync.ToArray()); + var list = messagesAsync.ToEnumerable().ToList(); + var messages = new List(); + foreach (var task in list) messages.AddRange(task.ToList()); + + if (Permissions.Check(Context, new[] {"Admin", "Mod", "Meister"})) + { + var waiters = new List(); + foreach (var message in messages) waiters.Add(((IUserMessage) message).DeleteAsync()); + + Task.WaitAll(waiters.ToArray()); + } + } + } + + [Command("check")] + [Summary("Echos a message.")] + [Alias("Check")] + public async Task CheckAsync(string quarry) + { + var perm = new List {"Admin", "Mod", "Privatpolizei"}; + + Permissions.Test(Context, perm.ToArray()); + + var test = File.ReadAllLines("RG.txt"); + + var lines = test.Where(x => !x.Equals(string.Empty)).ToList(); + + + var sc = new SpellCorrect(); + var count = lines.OrderBy(line => sc.Compare(quarry, line)).First(); + + var fit = sc.Compare(count, quarry); + + string antwort; + + antwort = fit < SpellCorrect.ErrorThreshold - 20000 + ? $"```xl\nAuf anderem Server Match gefunden: {count}" + : $"```xl\nAuf anderem Server Kein Match gefunden: {quarry}"; + + + var users = Context.Guild.GetUsersAsync(); + await users; + var us = users.Result.Select(x => x.Username); + + sc = new SpellCorrect(); + count = us.OrderBy(line => sc.Compare(quarry, line)).First(); + + fit = sc.Compare(count, quarry); + + antwort = fit < SpellCorrect.ErrorThreshold - 20000 + ? $"{antwort}\nAuf unserem Server Match gefunden: {count}\n```" + : $"{antwort}\nAuf unserem Server Kein Match gefunden: {quarry} \n```"; + + await ReplyAsync(antwort); + } + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2