summaryrefslogtreecommitdiff
path: root/DiscoBot/Program.cs
blob: 6bf870eac660dd57306c14545f9fdce1fef51fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Reflection;
using System.Threading.Tasks;

using Discord;
using Discord.Commands;
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();

        public async Task StartAsync()
        {
            Dsa.Startup();
            this.client = new DiscordSocketClient();
            this.commands = new CommandService();

            string token = Properties.Settings.Default.Token;

            this.services = new ServiceCollection()
                    .BuildServiceProvider();
            AppDomain.CurrentDomain.ProcessExit += OnProcessExit;

            await this.InstallCommandsAsync();

            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;
            
            // Discover all of the commands in this assembly and load them.
            return this.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;
            }

            // 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(this.client.CurrentUser, ref argPos)))
            {
                return;
            }

            // Create a Command Context
            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 this.commands.ExecuteAsync(context, argPos, this.services);
            if (!result.IsSuccess)
            {
                await context.Channel.SendMessageAsync(result.ErrorReason);
            }
        }

        private static void OnProcessExit(object sender, EventArgs e)
        {
            Console.WriteLine("I'm out of here");
            Voice.Client.StopAsync();
        }
    }
}