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.

InteractionHandlingService.cs 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Discord;
  2. using Discord.Interactions;
  3. using Discord.WebSocket;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using System;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace ShardedClient.Services
  9. {
  10. public class InteractionHandlingService
  11. {
  12. private readonly InteractionService _service;
  13. private readonly DiscordShardedClient _client;
  14. private readonly IServiceProvider _provider;
  15. public InteractionHandlingService(IServiceProvider services)
  16. {
  17. _service = services.GetRequiredService<InteractionService>();
  18. _client = services.GetRequiredService<DiscordShardedClient>();
  19. _provider = services;
  20. _service.Log += LogAsync;
  21. _client.InteractionCreated += OnInteractionAsync;
  22. _client.ShardReady += ReadyAsync;
  23. // For examples on how to handle post execution,
  24. // see the InteractionFramework samples.
  25. }
  26. // Register all modules, and add the commands from these modules to either guild or globally depending on the build state.
  27. public async Task InitializeAsync()
  28. {
  29. await _service.AddModulesAsync(typeof(InteractionHandlingService).Assembly, _provider);
  30. }
  31. private async Task OnInteractionAsync(SocketInteraction interaction)
  32. {
  33. _ = Task.Run(async () =>
  34. {
  35. var context = new ShardedInteractionContext(_client, interaction);
  36. await _service.ExecuteCommandAsync(context, _provider);
  37. });
  38. await Task.CompletedTask;
  39. }
  40. private Task LogAsync(LogMessage log)
  41. {
  42. Console.WriteLine(log.ToString());
  43. return Task.CompletedTask;
  44. }
  45. private async Task ReadyAsync(DiscordSocketClient _)
  46. {
  47. #if DEBUG
  48. await _service.RegisterCommandsToGuildAsync(1 /* implement */);
  49. #else
  50. await _service.RegisterCommandsGloballyAsync();
  51. #endif
  52. }
  53. }
  54. }