diff options
Diffstat (limited to 'DiscoBot/Program.cs')
-rw-r--r-- | DiscoBot/Program.cs | 73 |
1 files changed, 41 insertions, 32 deletions
diff --git a/DiscoBot/Program.cs b/DiscoBot/Program.cs index 7ae8244..6bf870e 100644 --- a/DiscoBot/Program.cs +++ b/DiscoBot/Program.cs @@ -1,78 +1,87 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Reflection; +using System.Threading.Tasks; + using Discord; -using Discord.WebSocket; using Discord.Commands; -using Microsoft.Extensions.DependencyInjection; +using Discord.WebSocket; +using Microsoft.Extensions.DependencyInjection; namespace DiscoBot { + using DiscoBot.Commands; + public class Program { private CommandService commands; private DiscordSocketClient client; private IServiceProvider services; - + public static void Main(string[] args) => new Program().StartAsync().GetAwaiter().GetResult(); - static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult(); - - public async Task Start() + public async Task StartAsync() { - var loading = DSA.Startup(); - client = new DiscordSocketClient(); - commands = new CommandService(); + Dsa.Startup(); + this.client = new DiscordSocketClient(); + this.commands = new CommandService(); string token = Properties.Settings.Default.Token; - services = new ServiceCollection() + this.services = new ServiceCollection() .BuildServiceProvider(); - AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit); - - await InstallCommands(); + AppDomain.CurrentDomain.ProcessExit += OnProcessExit; - await client.LoginAsync(TokenType.Bot, token); - await client.StartAsync(); + await this.InstallCommandsAsync(); - await loading; + await this.client.LoginAsync(TokenType.Bot, token); + await this.client.StartAsync(); + await Task.Delay(-1); } - public async Task InstallCommands() + public Task InstallCommandsAsync() { // Hook the MessageReceived Event into our Command Handler - client.MessageReceived += HandleCommand; + this.client.MessageReceived += this.HandleCommandAsync; + // Discover all of the commands in this assembly and load them. - await commands.AddModulesAsync(Assembly.GetEntryAssembly()); + return this.commands.AddModulesAsync(Assembly.GetEntryAssembly()); } - public async Task HandleCommand(SocketMessage messageParam) + public async Task HandleCommandAsync(SocketMessage messageParam) { // Don't process the command if it was a System Message - var message = messageParam as SocketUserMessage; - if (message == null) return; + if (!(messageParam is SocketUserMessage message)) + { + return; + } + // Create a number to track where the prefix ends and the command begins int 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(client.CurrentUser, ref argPos))) return; + if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(this.client.CurrentUser, ref argPos))) + { + return; + } + // Create a Command Context - var context = new CommandContext(client, message); + var context = new CommandContext(this.client, message); + // Execute the command. (result does not indicate a return value, // rather an object stating if the command executed successfully) - var result = await commands.ExecuteAsync(context, argPos, services); + var result = await this.commands.ExecuteAsync(context, argPos, this.services); if (!result.IsSuccess) + { await context.Channel.SendMessageAsync(result.ErrorReason); + } } - static void OnProcessExit(object sender, EventArgs e) + + private static void OnProcessExit(object sender, EventArgs e) { Console.WriteLine("I'm out of here"); - Voice.client.StopAsync(); + Voice.Client.StopAsync(); } } - } |