You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

InteractionHandler.cs 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Discord;
  2. using Discord.Interactions;
  3. using Discord.WebSocket;
  4. using Microsoft.Extensions.Configuration;
  5. using System;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. namespace InteractionFramework
  9. {
  10. public class InteractionHandler
  11. {
  12. private readonly DiscordSocketClient _client;
  13. private readonly InteractionService _handler;
  14. private readonly IServiceProvider _services;
  15. private readonly IConfiguration _configuration;
  16. public InteractionHandler(DiscordSocketClient client, InteractionService handler, IServiceProvider services, IConfiguration config)
  17. {
  18. _client = client;
  19. _handler = handler;
  20. _services = services;
  21. _configuration = config;
  22. }
  23. public async Task InitializeAsync()
  24. {
  25. // Process when the client is ready, so we can register our commands.
  26. _client.Ready += ReadyAsync;
  27. _handler.Log += LogAsync;
  28. // Add the public modules that inherit InteractionModuleBase<T> to the InteractionService
  29. await _handler.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
  30. // Process the InteractionCreated payloads to execute Interactions commands
  31. _client.InteractionCreated += HandleInteraction;
  32. }
  33. private async Task LogAsync(LogMessage log)
  34. => Console.WriteLine(log);
  35. private async Task ReadyAsync()
  36. {
  37. // Context & Slash commands can be automatically registered, but this process needs to happen after the client enters the READY state.
  38. // Since Global Commands take around 1 hour to register, we should use a test guild to instantly update and test our commands.
  39. if (Program.IsDebug())
  40. await _handler.RegisterCommandsToGuildAsync(_configuration.GetValue<ulong>("testGuild"), true);
  41. else
  42. await _handler.RegisterCommandsGloballyAsync(true);
  43. }
  44. private async Task HandleInteraction(SocketInteraction interaction)
  45. {
  46. try
  47. {
  48. // Create an execution context that matches the generic type parameter of your InteractionModuleBase<T> modules.
  49. var context = new SocketInteractionContext(_client, interaction);
  50. // Execute the incoming command.
  51. var result = await _handler.ExecuteCommandAsync(context, _services);
  52. if (!result.IsSuccess)
  53. switch (result.Error)
  54. {
  55. case InteractionCommandError.UnmetPrecondition:
  56. // implement
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. catch
  63. {
  64. // If Slash Command execution fails it is most likely that the original interaction acknowledgement will persist. It is a good idea to delete the original
  65. // response, or at least let the user know that something went wrong during the command execution.
  66. if (interaction.Type is InteractionType.ApplicationCommand)
  67. await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync());
  68. }
  69. }
  70. }
  71. }