summaryrefslogtreecommitdiff
path: root/DiscordBot/CommandHandler.cs
blob: b41756f4a1fbff5a3256cb8dbbbb2f21d550fefb (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;

#endregion

namespace DiscordBot
{
    public class CommandHandler
    {
        private static readonly HttpClient _HttpClient = new HttpClient();
        private readonly DiscordSocketClient _client;
        private readonly CommandService _commands;

        public CommandHandler(DiscordSocketClient client, CommandService commands) {
            _commands = commands;
            _client = client;
        }

        public  Task InstallCommandsAsync() {
            // Hook the MessageReceived event into our command handler
            _client.MessageReceived += HandleCommandAsync;

            // Here we discover all of the command modules in the entry 
            // assembly and load them. Starting from Discord.NET 2.0, a
            // service provider is required to be passed into the
            // module registration method to inject the 
            // required dependencies.
            //
            // If you do not use Dependency Injection, pass null.
            // See Dependency Injection guide for more information.
            return _commands.AddModulesAsync(Assembly.GetEntryAssembly(),
                null);
        }


        private static async Task<string> SendCommand(string name, string command, string url) {
            command = command.Remove(0, 1);
            var args = command.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

            string cmdContent = string.Empty;
            if (args.Length > 1) {
                cmdContent = "\"" + args.Skip(1).Aggregate((s, n) => s + "\", \"" + n) + "\"";
            }

            var values = new Dictionary<string, string> {
                {"Name", name},
                {"CmdIdentifier", args.First()},
                {"CmdTexts", "[" + cmdContent + "]"}
            };

            var content = new FormUrlEncodedContent(values);

            var response = await _HttpClient.PostAsync(url, content);

            return await response.Content.ReadAsStringAsync();
        }

        private 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;
            }

            // Create a number to track where the prefix ends and the command begins
            var argPos = 0;

            // Determine if the message is a command based on the prefix and make sure no bots trigger commands
            if (!(message.HasCharPrefix('!', ref argPos) ||
                  message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
                message.Author.IsBot) {
                return;
            }

            // Create a WebSocket-based command context based on the message
            var context = new SocketCommandContext(_client, message);

            // Execute the command with the command context we just
            // created, along with the service provider for precondition checks.

            // Keep in mind that result does not indicate a return value
            // rather an object stating if the command executed successfully.
            var result = await _commands.ExecuteAsync(
                context,
                argPos,
                null);

            // Optionally, we may inform the user if the command fails
            // to be executed; however, this may not always be desired,
            // as it may clog up the request queue should a user spam a
            // command.

            if (result.Error == CommandError.UnknownCommand) {
                string response = await SendCommand(message.Author.Username, message.Content,
                    "https://kobert.dev/api/dsa/commands");
                //var response = "invalid";
                await context.Channel.SendMessageAsync(response);
            }
            else if (!result.IsSuccess) {
                await context.Channel.SendMessageAsync(result.ErrorReason);
            }
        }
    }
}