-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractionHandler.cs
75 lines (63 loc) · 2.08 KB
/
InteractionHandler.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
using System;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
namespace Natsirt;
public class InteractionHandler
{
private readonly DiscordSocketClient _client;
private readonly IConfiguration _configuration;
private readonly InteractionService _handler;
private readonly IServiceProvider _services;
public InteractionHandler(DiscordSocketClient client, InteractionService handler, IServiceProvider services,
IConfiguration config)
{
_client = client;
_handler = handler;
_services = services;
_configuration = config;
}
public async Task InitializeAsync()
{
_client.Ready += ReadyAsync;
_handler.Log += LogAsync;
await _handler.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
_client.InteractionCreated += HandleInteraction;
}
private async Task LogAsync(LogMessage log)
{
Console.WriteLine(log);
}
private async Task ReadyAsync()
{
await _handler.RegisterCommandsGloballyAsync();
}
private async Task HandleInteraction(SocketInteraction interaction)
{
try
{
var context = new SocketInteractionContext(_client, interaction);
if (context.Guild is null)
{
await context.Interaction.RespondAsync("This command can only be used in a server.", ephemeral:true);
return;
};
var result = await _handler.ExecuteCommandAsync(context, _services);
if (!result.IsSuccess)
switch (result.Error)
{
case InteractionCommandError.UnmetPrecondition:
// implement
break;
}
}
catch
{
if (interaction.Type is InteractionType.ApplicationCommand)
await interaction.GetOriginalResponseAsync().ContinueWith(async msg => await msg.Result.DeleteAsync());
}
}
}