summaryrefslogtreecommitdiff
path: root/DiscordBot/Auxiliary
diff options
context:
space:
mode:
Diffstat (limited to 'DiscordBot/Auxiliary')
-rw-r--r--DiscordBot/Auxiliary/CommandExtension.cs98
-rw-r--r--DiscordBot/Auxiliary/Dice.cs31
-rw-r--r--DiscordBot/Auxiliary/Permissions.cs32
-rw-r--r--DiscordBot/Auxiliary/RandomMisc.cs36
-rw-r--r--DiscordBot/Auxiliary/SpellCorrect.cs105
5 files changed, 302 insertions, 0 deletions
diff --git a/DiscordBot/Auxiliary/CommandExtension.cs b/DiscordBot/Auxiliary/CommandExtension.cs
new file mode 100644
index 0000000..690f352
--- /dev/null
+++ b/DiscordBot/Auxiliary/CommandExtension.cs
@@ -0,0 +1,98 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Discord;
+using Discord.Commands;
+
+namespace DiscordBot.Auxiliary
+{
+ 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}```");
+
+ 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());
+
+ var test = messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot).Select(c => c);
+ Task.WaitAll(test.Select(message => (message as IUserMessage)?.DeleteAsync()).ToArray());
+ }
+
+ public static async Task ReplyAsync(this ModuleBase m, IEnumerable<string> message, bool directMessage = false)
+ {
+ var sb = new StringBuilder();
+ foreach (var 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<string> message, TimeSpan time)
+ {
+ var sb = new StringBuilder();
+ foreach (var 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();
+
+ var stream = _client.OpenRead(url);
+ await channel.SendFileAsync(stream, url.Split('/').Last());
+ }
+ }
+} \ No newline at end of file
diff --git a/DiscordBot/Auxiliary/Dice.cs b/DiscordBot/Auxiliary/Dice.cs
new file mode 100644
index 0000000..c44e87e
--- /dev/null
+++ b/DiscordBot/Auxiliary/Dice.cs
@@ -0,0 +1,31 @@
+using System;
+
+namespace DiscordBot.Auxiliary
+{
+ public static class Dice // roll it!
+ {
+ private static readonly Random Rnd = new Random();
+
+ public static int Roll(int d = 20)
+ {
+ return Rnd.Next(d) + 1;
+ }
+
+
+ public static int Roll(int count, int d)
+ {
+ if (d <= 0) return 0;
+
+ var sum = 0;
+ for (var i = 0; i < Math.Abs(count); i++)
+ {
+ var roll = Roll(d);
+ sum += roll;
+ }
+
+ sum *= Math.Abs(count) / count;
+
+ return sum;
+ }
+ }
+} \ No newline at end of file
diff --git a/DiscordBot/Auxiliary/Permissions.cs b/DiscordBot/Auxiliary/Permissions.cs
new file mode 100644
index 0000000..c2cb058
--- /dev/null
+++ b/DiscordBot/Auxiliary/Permissions.cs
@@ -0,0 +1,32 @@
+using System.Collections.Generic;
+using System.Linq;
+using Discord.Commands;
+using Discord.WebSocket;
+
+namespace DiscordBot.Auxiliary
+{
+ public static class Permissions
+ {
+ public static bool Check(ICommandContext c, string role)
+ {
+ return ((SocketGuildUser) c.User).Roles.ToList().Exists(v => v.Name.Equals(role));
+ }
+
+ public static bool Check(ICommandContext c, IEnumerable<string> roles)
+ {
+ return roles.Any(role => ((SocketGuildUser) c.User).Roles.ToList().Exists(v => v.Name.Equals(role)));
+ }
+
+ public static bool Test(ICommandContext c, string role)
+ {
+ if (Check(c, role)) return true;
+ c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait();
+ return false;
+ }
+
+ public static void Test(ICommandContext c, string[] roles)
+ {
+ if (!Check(c, roles)) c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait();
+ }
+ }
+} \ No newline at end of file
diff --git a/DiscordBot/Auxiliary/RandomMisc.cs b/DiscordBot/Auxiliary/RandomMisc.cs
new file mode 100644
index 0000000..f7c5186
--- /dev/null
+++ b/DiscordBot/Auxiliary/RandomMisc.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Linq;
+using System.Text;
+
+namespace DiscordBot.Auxiliary
+{
+ public static class RandomMisc
+ {
+ public static string Roll(string input)
+ {
+ var output = new StringBuilder();
+ var strings = input.Split('w', 'd').ToList();
+ var count = Convert.ToInt32(strings[0]);
+ strings = strings[1].Split(' ').ToList();
+ var d = Convert.ToInt32(strings[0]);
+
+ if (strings.Count > 0)
+ {
+ }
+
+ var sum = 0;
+ for (var i = 0; i < count; i++)
+ {
+ var roll = Dice.Roll(d);
+ sum += roll;
+ output.Append("[" + roll + "] ");
+ }
+
+ if (strings.Count <= 1) return output.ToString();
+ sum += Convert.ToInt32(strings[1]);
+ output.Append("sum: " + sum);
+
+ return output.ToString();
+ }
+ }
+} \ No newline at end of file
diff --git a/DiscordBot/Auxiliary/SpellCorrect.cs b/DiscordBot/Auxiliary/SpellCorrect.cs
new file mode 100644
index 0000000..a2ba91a
--- /dev/null
+++ b/DiscordBot/Auxiliary/SpellCorrect.cs
@@ -0,0 +1,105 @@
+using System;
+using System.Diagnostics;
+
+namespace DiscordBot.Auxiliary
+{
+ public class SpellCorrect : StringComparer
+ {
+ public const int ErrorThreshold = 94100;
+
+ public override int Compare(string x, string y)
+ {
+ return CompareEasy(x, y);
+ }
+
+ public static int CompareEasy(string x, string y)
+ {
+ if (string.IsNullOrEmpty(x)) throw new ArgumentException("message", nameof(x));
+
+ if (string.IsNullOrEmpty(y)) throw new ArgumentException("message", nameof(y));
+
+ if (x.Equals(y)) return 0;
+
+ x = x.ToLower();
+ y = y.ToLower();
+ if (x.Equals(y)) return 1;
+
+ var subs = y.Split(' ', '/');
+ var score = subs.Length;
+ foreach (var s in subs)
+ if (s.Equals(x))
+ score--;
+
+ if (score < subs.Length) return score + 1;
+
+ return 100000 - (int) (CompareExact(x, y) * 1000.0);
+ /*if (y.Contains(x))
+ return 6;*/
+ }
+
+ public override bool Equals(string x, string y)
+ {
+ Debug.Assert(x != null, nameof(x) + " != null");
+ return x.Equals(y);
+ }
+
+ public override int GetHashCode(string obj)
+ {
+ throw new NotImplementedException();
+ }
+
+ public static double CompareExact(string s, string q)
+ {
+ s = s.ToLower();
+ q = q.ToLower();
+
+ int i, j;
+ const double match = 3.0;
+ const double gap = -2.0;
+ const double mismatch = -2.0;
+
+ double decay;
+
+ var matrix = new double[s.Length + 1, q.Length + 1];
+ var max = 0.0;
+ matrix[0, 0] = 0.0;
+
+ for (i = 1; i < s.Length; i++)
+ // matrix[i, 0] = 0.0;
+ matrix[i, 0] = i * gap;
+
+ for (i = 1; i < q.Length; i++) matrix[0, i] = 0.0;
+
+
+ for (i = 1; i <= s.Length; i++)
+ for (j = 1; j <= q.Length; j++)
+ {
+ decay = j / (double) (s.Length * 1000);
+ var add = s[i - 1] == q[j - 1] ? match - decay : mismatch;
+ var score = matrix[i - 1, j - 1] + add;
+
+ if (score < matrix[i - 1, j] + gap) score = matrix[i - 1, j] + gap;
+
+ if (score < matrix[i, j - 1] + gap) score = matrix[i, j - 1] + gap;
+
+ if (i > 1 && j > 1)
+ if (s[i - 1] == q[j - 2] && s[i - 2] == q[j - 1])
+ {
+ add = 3 / 2.0 * match - decay;
+ if (score < matrix[i - 2, j - 2] + add) score = matrix[i - 2, j - 2] + add;
+ }
+
+ // if (score < 0)
+ // {
+ // score = 0;
+ // }
+
+ if (max < score && i == s.Length) max = score;
+
+ matrix[i, j] = score;
+ }
+
+ return max;
+ }
+ }
+} \ No newline at end of file