From 19bf37de7642ae8cdefd8fc6b4fadac3ac96ea9b Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Wed, 26 Sep 2018 23:49:26 +0200 Subject: ported most code to WebApiProject everything except the user identification and file locations should be somewhat functional --- DiscoBot/Auxiliary/CommandExtension.cs | 119 --------------------------------- DiscoBot/Auxiliary/Permissions.cs | 43 ------------ DiscoBot/DiscoBot.csproj | 4 +- DiscoBot/Program.cs | 5 +- DiscoBot/ToRework/CommandExtension.cs | 119 +++++++++++++++++++++++++++++++++ DiscoBot/ToRework/Permissions.cs | 43 ++++++++++++ 6 files changed, 166 insertions(+), 167 deletions(-) delete mode 100644 DiscoBot/Auxiliary/CommandExtension.cs delete mode 100644 DiscoBot/Auxiliary/Permissions.cs create mode 100644 DiscoBot/ToRework/CommandExtension.cs create mode 100644 DiscoBot/ToRework/Permissions.cs (limited to 'DiscoBot') diff --git a/DiscoBot/Auxiliary/CommandExtension.cs b/DiscoBot/Auxiliary/CommandExtension.cs deleted file mode 100644 index 098e35f..0000000 --- a/DiscoBot/Auxiliary/CommandExtension.cs +++ /dev/null @@ -1,119 +0,0 @@ -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/DiscoBot/Auxiliary/Permissions.cs b/DiscoBot/Auxiliary/Permissions.cs deleted file mode 100644 index 4d73146..0000000 --- a/DiscoBot/Auxiliary/Permissions.cs +++ /dev/null @@ -1,43 +0,0 @@ -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 bool Test(ICommandContext c, string role) - { - if (!Check(c, role)) - { - c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait(); - return false; - } - - return true; - } - - 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/DiscoBot.csproj b/DiscoBot/DiscoBot.csproj index 30f4755..d25a5e9 100644 --- a/DiscoBot/DiscoBot.csproj +++ b/DiscoBot/DiscoBot.csproj @@ -113,13 +113,13 @@ - + - + diff --git a/DiscoBot/Program.cs b/DiscoBot/Program.cs index 5bc0195..9d3c140 100644 --- a/DiscoBot/Program.cs +++ b/DiscoBot/Program.cs @@ -34,9 +34,7 @@ namespace DiscoBot string token = File.ReadAllText("Token"); //Properties.Settings.Default.Token; - - this.services = new ServiceCollection().AddSingleton(new AudioService()) - .BuildServiceProvider(); + AppDomain.CurrentDomain.ProcessExit += OnProcessExit; await this.InstallCommandsAsync(); @@ -73,6 +71,7 @@ namespace DiscoBot return; } + // Create a Command Context var context = new CommandContext(this.client, message); diff --git a/DiscoBot/ToRework/CommandExtension.cs b/DiscoBot/ToRework/CommandExtension.cs new file mode 100644 index 0000000..098e35f --- /dev/null +++ b/DiscoBot/ToRework/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/DiscoBot/ToRework/Permissions.cs b/DiscoBot/ToRework/Permissions.cs new file mode 100644 index 0000000..4d73146 --- /dev/null +++ b/DiscoBot/ToRework/Permissions.cs @@ -0,0 +1,43 @@ +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 bool Test(ICommandContext c, string role) + { + if (!Check(c, role)) + { + c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait(); + return false; + } + + return true; + } + + public static void Test(ICommandContext c, string[] roles) + { + if (!Check(c, roles)) + { + c.Channel.SendMessageAsync("```xl\n Keine ausreichenden Berechtigungen\n```").Wait(); + } + } + } +} -- cgit v1.3.1