diff options
author | natrixaeria <janng@gmx.de> | 2019-05-19 17:40:59 +0200 |
---|---|---|
committer | natrixaeria <janng@gmx.de> | 2019-05-19 17:40:59 +0200 |
commit | 1509b5ef3d7e9e71d9294e234ec0e39f2d831998 (patch) | |
tree | 78300ffff230958101b422a4e6026925b287822f /DiscoBot/Program.cs | |
parent | c3bb858bb54dc8c64bbd48054c2c58dc0073f09c (diff) | |
parent | c4d046858e0822b7c2c540ac2368b2c0e88e7a26 (diff) |
Merge branch 'scribble' of https://github.com/TrueDoctor/DiscoBot into scribble
Diffstat (limited to 'DiscoBot/Program.cs')
-rw-r--r-- | DiscoBot/Program.cs | 100 |
1 files changed, 41 insertions, 59 deletions
diff --git a/DiscoBot/Program.cs b/DiscoBot/Program.cs index 5dc69c1..6ddac5d 100644 --- a/DiscoBot/Program.cs +++ b/DiscoBot/Program.cs @@ -1,123 +1,105 @@ using System; +using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Threading.Tasks; - +using DiscoBot.Audio; using Discord; using Discord.Commands; using Discord.WebSocket; -using Microsoft.Extensions.DependencyInjection; - namespace DiscoBot { - using System.IO; - - using DiscoBot.Audio; - using DiscoBot.DSA_Game; - public class Program { - private CommandService commands; private DiscordSocketClient client; + private CommandService commands; private IServiceProvider services; - public static void Main(string[] args) => new Program().StartAsync().GetAwaiter().GetResult(); + public static void Main(string[] args) + { + new Program().StartAsync().GetAwaiter().GetResult(); + } public async Task StartAsync() { - Dsa.Startup(); - - this.client = new DiscordSocketClient(); - this.commands = new CommandService(); - + client = new DiscordSocketClient(); + commands = new CommandService(); - string token = File.ReadAllText("Token"); + var token = File.ReadAllText("Token"); //Properties.Settings.Default.Token; - + AppDomain.CurrentDomain.ProcessExit += OnProcessExit; - await this.InstallCommandsAsync(); + await InstallCommandsAsync(); + + await client.LoginAsync(TokenType.Bot, token); + await client.StartAsync(); - await this.client.LoginAsync(TokenType.Bot, token); - await this.client.StartAsync(); - await Task.Delay(-1); } public Task InstallCommandsAsync() { // Hook the MessageReceived Event into our Command Handler - this.client.MessageReceived += this.HandleCommandAsync; - + client.MessageReceived += HandleCommandAsync; + // Discover all of the commands in this assembly and load them. - return this.commands.AddModulesAsync(Assembly.GetEntryAssembly()); + return commands.AddModulesAsync(Assembly.GetEntryAssembly()); } public async Task HandleCommandAsync(SocketMessage messageParam) { // Don't process the command if it was a System Message - if (!(messageParam is SocketUserMessage message)) - { - return; - } + if (!(messageParam is SocketUserMessage message)) return; // Create a number to track where the prefix ends and the command begins - int argPos = 0; + var argPos = 0; // Determine if the message is a command, based on if it starts with '!' or a mention prefix - if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(this.client.CurrentUser, ref argPos))) - { - return; - } + if (!(message.HasCharPrefix('!', ref argPos) || + message.HasMentionPrefix(client.CurrentUser, ref argPos))) return; + - // Create a Command Context - var context = new CommandContext(this.client, message); - + var context = new CommandContext(client, message); + // Execute the command. (result does not indicate a return value, // rather an object stating if the command executed successfully) - var result = await this.commands.ExecuteAsync(context, argPos, this.services); + var result = await commands.ExecuteAsync(context, argPos, services); if (result.Error == CommandError.UnknownCommand) - { - await context.Channel.SendMessageAsync(SendCommand(message.Author.Username, message.Content, "https://localhost:44365/api/Commands")); - } - else if (!result.IsSuccess) - { - await context.Channel.SendMessageAsync(result.ErrorReason); - } + await context.Channel.SendMessageAsync(SendCommand(message.Author.Username, message.Content, + "https://kobert.dev/api/dsa/commands")); + else if (!result.IsSuccess) await context.Channel.SendMessageAsync(result.ErrorReason); } - private string SendCommand(string name, string command, string url) + private static string SendCommand(string name, string command, string url) { - var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); + var httpWebRequest = (HttpWebRequest) WebRequest.Create(url); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { - command = command.Remove(0,1); - var args = command.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries); + command = command.Remove(0, 1); + var args = command.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries); + + var content = string.Empty; + if (args.Length > 1) content = "\"" + args.Skip(1).Aggregate((s, n) => s + "\", \"" + n) + "\""; - string content = string.Empty; - if (args.Length > 1) - { - content = "\"" + args.Skip(1).Aggregate((s, n) => ( s + "\", \"" + n)) + "\""; - } + var json = "{\"Name\":\"" + name + "\"," + + "\"CmdIdentifier\":\"" + args.First() + "\"," + + "\"CmdTexts\": [" + content + "] }"; - string json = "{\"Name\":\"" + name + "\"," + - "\"CmdIdentifier\":\"" + args.First() + "\"," + - "\"CmdTexts\": ["+ content+"] }"; - streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } - var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); + var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { return streamReader.ReadToEnd(); @@ -130,4 +112,4 @@ namespace DiscoBot Voice.Client.StopAsync(); } } -} +}
\ No newline at end of file |