From f6edadbfcadbc9e38e22500a496a74dd49d96d8a Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Fri, 1 Jun 2018 11:25:03 +0100 Subject: added Permissions Class for easy Permission required commands --- DiscoBot/App.config | 4 + DiscoBot/Audio/Soundeffects.cs | 29 ++-- DiscoBot/Audio/Voice.cs | 16 ++- DiscoBot/Auxiliary/CommandExtension.cs | 2 + DiscoBot/Auxiliary/Permissions.cs | 40 ++++++ DiscoBot/Characters/Character.cs | 6 +- DiscoBot/Commands/Gm.cs | 6 +- DiscoBot/Commands/List.cs | 6 + DiscoBot/Commands/MiscCommands.cs | 236 ++++++++++++++++++++++++++++++++- DiscoBot/Commands/ProbenTest.cs | 19 ++- DiscoBot/DSA.cs | 2 +- DiscoBot/DiscoBot.csproj | 34 +++-- DiscoBot/packages.config | 13 +- 13 files changed, 377 insertions(+), 36 deletions(-) create mode 100644 DiscoBot/Auxiliary/Permissions.cs (limited to 'DiscoBot') diff --git a/DiscoBot/App.config b/DiscoBot/App.config index 1ec9ae3..2a3afc9 100644 --- a/DiscoBot/App.config +++ b/DiscoBot/App.config @@ -26,6 +26,10 @@ + + + + diff --git a/DiscoBot/Audio/Soundeffects.cs b/DiscoBot/Audio/Soundeffects.cs index 485be28..9982947 100644 --- a/DiscoBot/Audio/Soundeffects.cs +++ b/DiscoBot/Audio/Soundeffects.cs @@ -10,19 +10,23 @@ Bell, Ding, Nooo, - Monterkill, + Monsterkill, Finish, Wrong, Magic, - Stupid + Stupid, + Police, + Roblox } public static class SoundEffects { + public static int Volume { get; set; } = 50; + public static async Task Play(Sound s) { string url = string.Empty; - int vol = 256; + int volume = 256; switch (s) { case Sound.Bell: @@ -35,26 +39,35 @@ case Sound.Magic: url = "https://www.myinstants.com/media/sounds/dream-harp-sound-effect.mp3"; break; - case Sound.Monterkill: + case Sound.Monsterkill: url = "https://www.myinstants.com/media/sounds/announcer_kill_monster_01.mp3"; break; case Sound.Nooo: url = "https://www.myinstants.com/media/sounds/nooo.swf.mp3"; break; + case Sound.Roblox: + url = "https://www.myinstants.com/media/sounds/roblox-death-sound_ytkBL7X.mp3"; + break; case Sound.Stupid: url = "https://www.myinstants.com/media/sounds/stupid_dum_03.mp3"; - vol = 10; + volume = 10; + break; + case Sound.Police: + url = "https://www.myinstants.com/media/sounds/sound-of-the-police.mp3"; break; case Sound.Wrong: url = "https://www.myinstants.com/media/sounds/wrong-answer-sound-effect.mp3"; - vol = 50; - break; + volume = 50; + break; } + volume = (int)(volume * (Volume / 100.0)); + + if (url != string.Empty) { // await Dsa.Service.SendAudioAsync(url, vol); - await Voice.SendAsync(url, vol); + await Voice.SendAsync(url, volume); return; } diff --git a/DiscoBot/Audio/Voice.cs b/DiscoBot/Audio/Voice.cs index 7a7da5c..f9fc7a3 100644 --- a/DiscoBot/Audio/Voice.cs +++ b/DiscoBot/Audio/Voice.cs @@ -62,11 +62,8 @@ [Command("leave", RunMode = RunMode.Async)] public async Task LeaveChannelAsync(IVoiceChannel channel = null) { - if (!((SocketGuildUser)this.Context.User).Roles.ToList().Exists(v => v.Name.Equals("Meister"))) - { - await this.ReplyAsync("```xl\n Keine ausreichenden Berechtigunen\n```"); - return; - } +// Permissions.Test(this.Context, "Meister"); + if (Client != null) { var wait = SoundEffects.Play(Sound.Nooo); @@ -76,6 +73,15 @@ } } + [Command("volume")] + public async Task SetVolume(int volume) + { + if (volume <= 100 && volume >= 0) + { + SoundEffects.Volume = volume; + } + } + [Command("play", RunMode = RunMode.Async)] public async Task PlayAudioAsync(string path) { diff --git a/DiscoBot/Auxiliary/CommandExtension.cs b/DiscoBot/Auxiliary/CommandExtension.cs index 059ea91..5d07f6c 100644 --- a/DiscoBot/Auxiliary/CommandExtension.cs +++ b/DiscoBot/Auxiliary/CommandExtension.cs @@ -42,5 +42,7 @@ m.Context.Channel.DeleteMessagesAsync( messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot)); } + + } } diff --git a/DiscoBot/Auxiliary/Permissions.cs b/DiscoBot/Auxiliary/Permissions.cs new file mode 100644 index 0000000..48f8040 --- /dev/null +++ b/DiscoBot/Auxiliary/Permissions.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DiscoBot.Auxiliary +{ + using Discord.Commands; + using Discord.WebSocket; + + 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, string[] roles) + { + return roles.Any(role => ((SocketGuildUser)c.User).Roles.ToList().Exists(v => v.Name.Equals(role))); + } + + public static void Test(ICommandContext c, string role) + { + if (!Check(c, role)) + { + c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait(); + } + } + + public static void Test(ICommandContext c, string[] roles) + { + if (!Check(c, roles)) + { + c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait(); + } + } + } +} diff --git a/DiscoBot/Characters/Character.cs b/DiscoBot/Characters/Character.cs index c092154..86e8b3a 100644 --- a/DiscoBot/Characters/Character.cs +++ b/DiscoBot/Characters/Character.cs @@ -22,12 +22,12 @@ this.PropTable.Add("KO", "Konstitution"); this.PropTable.Add("KK", "Körperkraft"); - this.Post_process(); // calculate derived values } public Character(string path) : this() { this.Load(path); // load + this.Post_process(); // calculate derived values } public Character(Character c, string name, int stDv = 2) : this() @@ -52,6 +52,8 @@ { this.Kampftalente.Add(new KampfTalent(i.Name, i.At + (int)Math.Round(RandomMisc.Random(stDv)), i.Pa + (int)Math.Round(RandomMisc.Random(stDv)))); } + + this.Post_process(); // calculate derived values } public string Name { get; set; } // char name @@ -139,7 +141,7 @@ if (tap < 0) { - SoundEffects.Play(Sound.Wrong).Wait(); + //SoundEffects.Play(Sound.Wrong).Wait(); } output.AppendFormat(" tap: {0,2}", tap); diff --git a/DiscoBot/Commands/Gm.cs b/DiscoBot/Commands/Gm.cs index c1fa4b1..b426655 100644 --- a/DiscoBot/Commands/Gm.cs +++ b/DiscoBot/Commands/Gm.cs @@ -36,11 +36,7 @@ [Alias("GM", "as", "As", "als")] public async Task ProbeAsync([Summary("Fernkampfwaffe")] string name, string command, string waffe, int erschwernis = 0) { - if (!((SocketGuildUser)this.Context.User).Roles.ToList().Exists(v => v.Name.Equals("Meister"))) - { - await this.ReplyAsync("```xl\n Keine ausreichenden Berechtigunen\n```"); - return; - } + Permissions.Test(this.Context, "Meister"); command = command.ToLower(); string res = this.Test(name, command, waffe, erschwernis); diff --git a/DiscoBot/Commands/List.cs b/DiscoBot/Commands/List.cs index ca92088..41fa3d9 100644 --- a/DiscoBot/Commands/List.cs +++ b/DiscoBot/Commands/List.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; + using DiscoBot.Audio; using DiscoBot.Auxiliary; using DiscoBot.Characters; @@ -57,6 +58,11 @@ res.AddRange( character.Talente.Select(s => s.Name)); break; + case "sound": + case "sounds": + res.AddRange( + Enum.GetNames(typeof(Sound))); + break; case "v": case "vt": case "vor": diff --git a/DiscoBot/Commands/MiscCommands.cs b/DiscoBot/Commands/MiscCommands.cs index d60e25d..551022a 100644 --- a/DiscoBot/Commands/MiscCommands.cs +++ b/DiscoBot/Commands/MiscCommands.cs @@ -1,9 +1,31 @@ -namespace DiscoBot.Commands +using System; +using System.Collections.Generic; +using System.Runtime.Remoting.Contexts; +using System.Text; + +using DiscoBot.Auxiliary; + +using Discord; +using Discord.Commands; + +namespace DiscoBot.Commands { + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.IO; + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Net.Mime; + using System.Text; + using System.Threading; using System.Threading.Tasks; + using System.Windows.Forms; using DiscoBot.Auxiliary; + using Discord; using Discord.Commands; public class MiscCommands : ModuleBase @@ -36,5 +58,217 @@ var test = new Spotify.WebClient(); return this.ReplyAsync(string.Join("\n", test.GetPlaylist(""))); } + + [Command("liebe"), Summary("Echos a message.")] + [Alias("Liebe", "<3", "love")] + public Task LoveAsync() + { + return this.ReplyAsync(":heart: :heart: :heart: Verteilt die Liebe!"); + //return this.ReplyAsync("!say !liebe"); + } + + [Command("maul"), Summary("Echos a message.")] + public Task MaulAsync() + { + return this.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("report"), Summary("Report a Tweet")] + public async Task ReportAsync([Remainder, Summary("Link")] string link) + { + var content = new System.Net.Http.StringContent(link); + + using (HttpClient client = new HttpClient()) + { + var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content); + } + + await this.ReplyAsync($"Dein report wurde hinzugefügt"); + } + + [Command("match"), Summary("Tinder.")] + [Alias("mach","pass", "passt")] + public Task TinderAsync(string s1, string s2) + { + + var sc = new SpellCorrect(); + var rand = new System.Random((s1+s2).GetHashCode()); + + var wert = Math.Log10(Math.Floor(1000.0 * (sc.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 this.ReplyAsync($"Ihr passt zu {Math.Floor(100.0 * wert )/ 100.0}% zusammen"); + + } + + [Command("reddit"), Summary("Reddit.")] + public Task RedditAsync() + { + return this.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(CacheMode.AllowDownload); + 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(); + + string ls = string.Empty; + using (var client = new WebClient()) + { + ls =client.DownloadString(@"https://discordapp.com/assets/8529401dde4ab112e81d.js"); + } + + /*using (ScriptEngine engine = new ScriptEngine("jscript")) + { + ParsedScript parsed = engine.Parse(ls); + Debug.WriteLine(parsed.CallMethod("t", 3)); + }*/ + /*var task = MessageLoopWorker.Run(WebCrawler.DoWorkAsync, + "https://discordapp.com/widget?id=361270203952136203&theme=dark"); + task.Wait(); + var kl =task.Result; + Console.WriteLine("DoWorkAsync completed."); + + + ls = WebCrawler.Crawl("https://discordapp.com/widget?id=361270203952136203&theme=dark"); +*/ + /* + ScrapingBrowser Browser = new ScrapingBrowser(); + Browser.AllowAutoRedirect = true; // Browser has settings you can access in setup + Browser.AllowMetaRedirect = true; + Browser.UserAgent = new FakeUserAgent("The Doctor", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"); + Browser.Timeout = TimeSpan.FromMinutes(1); + WebPage PageResult = Browser.NavigateToPage(new Uri("https://discordapp.com/widget?id=361270203952136203&theme=dark")); + //await Task.Delay(TimeSpan.FromSeconds(10)); + + var dom = new ScrapySharp.Html.Parsing.HtmlDomBuilder(new HtmlDeclarationReader(new CodeReader(PageResult.Html.OuterHtml))); + + HtmlNode TitleNode = PageResult.Html.CssSelect(".widget-header").First(); + string PageTitle = TitleNode.InnerText;*/ + + var sc = new SpellCorrect(); + + var res = new List(); + + foreach (string line in lines) + { + var best = us.OrderBy(user => sc.Compare(user, line)).First(); + + double fit = sc.Compare(best, line); + + if (fit < SpellCorrect.ErrorThreshold - 20000) + { + if (fit.Equals(0)) + { + res.Add($"@\t{best} !!! => {line}"); + } + else + { + res.Add($"-\t{best} hat Ähnlichkeit mit: {line}"); + } + } + } + + var sb = new StringBuilder(); + foreach (string 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(this.Context, new []{"Admin", "Mod"})) + this.ReplyTimedAsync(sb.ToString(), TimeSpan.FromSeconds(90)); + + //await this.ReplyAsync($"{count} Duplikate gefunden"); + + } + + + [Command("clear"), Summary("Echos a message.")] + public async Task DeletekAsync(int count) + { + var messagesAsync = Context.Channel.GetMessagesAsync(count); + 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" })) + { + await Context.Channel.DeleteMessagesAsync(messages); + } + + } + + [Command("check"), Summary("Echos a message.")] + [Alias("Check")] + public async Task CheckAsync(string quarry) + { + var perm = new List(); + perm.Add("Admin"); + perm.Add("Mod"); + perm.Add("Privatpolizei"); + + Permissions.Test(this.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; + + if (fit < SpellCorrect.ErrorThreshold - 20000) + { + Antwort= $"```xl\nAuf anderem Server Match gefunden: {count}"; + } + else + { + Antwort = $"```xl\nAuf anderem Server Kein Match gefunden: {quarry}"; + } + + + var users = Context.Guild.GetUsersAsync(CacheMode.AllowDownload); + 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); + + if (fit < SpellCorrect.ErrorThreshold - 20000) + { + Antwort = Antwort + $"\nAuf unserem Server Match gefunden: {count}\n```"; + } + else + { + Antwort = Antwort + $"\nAuf unserem Server Kein Match gefunden: {quarry} \n```"; + } + + ReplyAsync(Antwort); + + } } } diff --git a/DiscoBot/Commands/ProbenTest.cs b/DiscoBot/Commands/ProbenTest.cs index a73f4fd..bdfaf23 100644 --- a/DiscoBot/Commands/ProbenTest.cs +++ b/DiscoBot/Commands/ProbenTest.cs @@ -10,7 +10,24 @@ [Alias("T", "Talent", "talent", "zauber", "z", "versuche")] public Task TalentAsync([Summary("Talent oder Zaubername")] string talent, int erschwernis = 0) { - string res = Gm.CheckCommand(Dsa.Relation[this.Context.User.Username], CommandTypes.Talent, talent, erschwernis); + string res; + try + { + res = Gm.CheckCommand( + Dsa.Relation[this.Context.User.Username], + CommandTypes.Talent, + talent, + erschwernis); + } + catch + { + res = Gm.CheckCommand( + Dsa.Relation["Tardis"], + CommandTypes.Talent, + talent, + erschwernis); + } + return this.ReplyAsync("```xl\n" + res + "\n```"); } diff --git a/DiscoBot/DSA.cs b/DiscoBot/DSA.cs index 121be08..0779988 100644 --- a/DiscoBot/DSA.cs +++ b/DiscoBot/DSA.cs @@ -25,7 +25,7 @@ public static void Startup() { Relation.Add("The Doctor", "Numeri Illuminus"); // Relation - Relation.Add("Tardis", "Morla"); // "Numeri Illuminus"); + Relation.Add("Tardis", "Helga von Drachenei, Tausendsasserin"); // "Numeri Illuminus"); Relation.Add("DSA Bot", "Morla"); // "Felis Exodus Schattenwald"); Relation.Add("Morla", "Morla"); Relation.Add("Rhoktar", "Rhoktar4"); diff --git a/DiscoBot/DiscoBot.csproj b/DiscoBot/DiscoBot.csproj index 37ddc5c..18a304e 100644 --- a/DiscoBot/DiscoBot.csproj +++ b/DiscoBot/DiscoBot.csproj @@ -53,6 +53,12 @@ ..\packages\Discord.Net.WebSocket.1.0.2\lib\net45\Discord.Net.WebSocket.dll + + ..\packages\FSharp.Core.4.0.0.1\lib\net40\FSharp.Core.dll + + + ..\packages\HtmlAgilityPack.1.4.9.4\lib\Net45\HtmlAgilityPack.dll + ..\packages\Microsoft.Extensions.DependencyInjection.2.0.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll @@ -62,8 +68,14 @@ ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + + ..\packages\ScrapySharp.2.6.2\lib\net45\ScrapySharp.Core.dll + + + ..\packages\ScrapySharp.2.6.2\lib\net45\ScrapySharpAsync.dll ..\packages\libsodium-net.0.10.0\lib\Net40\Sodium.dll @@ -76,8 +88,9 @@ ..\packages\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll - - ..\packages\System.Console.4.3.0\lib\net46\System.Console.dll + + ..\packages\System.Console.4.3.1\lib\net46\System.Console.dll + True @@ -110,6 +123,7 @@ ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll @@ -128,6 +142,8 @@ ..\packages\System.Security.Cryptography.X509Certificates.4.3.2\lib\net461\System.Security.Cryptography.X509Certificates.dll True + + @@ -145,6 +161,8 @@ + + @@ -185,13 +203,13 @@ - + Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}". - - + + - + \ No newline at end of file diff --git a/DiscoBot/packages.config b/DiscoBot/packages.config index e3bca0f..5c47ddc 100644 --- a/DiscoBot/packages.config +++ b/DiscoBot/packages.config @@ -1,6 +1,6 @@  - + @@ -8,19 +8,22 @@ + + - + - - + + + - + -- cgit v1.2.3-70-g09d2