-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandHandler.cs
102 lines (90 loc) · 3.77 KB
/
CommandHandler.cs
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
using AutoCompleteBot.Commands;
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using System.Reflection;
using System.Diagnostics;
namespace AutoCompleteBot
{
public class CommandHandler
{
private readonly DiscordSocketClient _client;
private readonly CommandService _commands;
private readonly IServiceProvider _serviceProvider;
private InteractionService _interactionService { get; set; }
// Retrieve client and CommandService instance via vector
public CommandHandler(DiscordSocketClient client, CommandService commands, IServiceProvider serviceProvider)
{
InteractionServiceConfig _interationServiceConfig = new InteractionServiceConfig();
_interationServiceConfig.EnableAutocompleteHandlers = true;
_interactionService = new(_client, config: _interationServiceConfig);
_commands = commands;
_client = client;
_serviceProvider = serviceProvider;
}
/// <summary>
/// Initialize the command handler
/// </summary>
/// <returns></returns>
public async Task InstallCommandsAsync()
{
// Hook the MessageReceived event into our command handler
_client.AutocompleteExecuted += AutocompleteExecuted;
_client.SlashCommandExecuted += SlashCommandHandler;
_client.InteractionCreated += InteractionCreated;
// 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.
await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
services: _serviceProvider);
}
private async Task AutocompleteExecuted(SocketAutocompleteInteraction interaction)
{
var context = new SocketInteractionContext(_client, interaction);
await _interactionService.ExecuteCommandAsync(context, services: _serviceProvider);
}
private async Task InteractionCreated(SocketInteraction interaction)
{
if (interaction.Type == InteractionType.ApplicationCommandAutocomplete)
{
var context = new SocketInteractionContext(_client, interaction);
await _interactionService.ExecuteCommandAsync(context, services: _serviceProvider);
}
}
/// <summary>
/// Slash command handler
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
private async Task SlashCommandHandler(SocketSlashCommand command)
{
// TODO : Improve data options
switch (command.Data.Name)
{
case CMD_Say.SLASH_CMD_ID:
{
CMD_Say cmd = new();
string echo = "Nothing to repeat" ;
var option = command.Data.Options.First().Value.ToString();
if (option != null)
{
echo = option;
}
await cmd.HandlerSlashCommand(command, echo);
}
break;
default:
{
await command.RespondAsync($"You executed {command.Data.Name}");
}
break;
}
}
}
}