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.

CommandHandlingService.cs 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Discord;
  2. using Discord.Commands;
  3. using Discord.WebSocket;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using System;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. namespace TextCommandFramework.Services
  9. {
  10. public class CommandHandlingService
  11. {
  12. private readonly CommandService _commands;
  13. private readonly DiscordSocketClient _discord;
  14. private readonly IServiceProvider _services;
  15. public CommandHandlingService(IServiceProvider services)
  16. {
  17. _commands = services.GetRequiredService<CommandService>();
  18. _discord = services.GetRequiredService<DiscordSocketClient>();
  19. _services = services;
  20. // Hook CommandExecuted to handle post-command-execution logic.
  21. _commands.CommandExecuted += CommandExecutedAsync;
  22. // Hook MessageReceived so we can process each message to see
  23. // if it qualifies as a command.
  24. _discord.MessageReceived += MessageReceivedAsync;
  25. }
  26. public async Task InitializeAsync()
  27. {
  28. // Register modules that are public and inherit ModuleBase<T>.
  29. await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
  30. }
  31. public async Task MessageReceivedAsync(SocketMessage rawMessage)
  32. {
  33. // Ignore system messages, or messages from other bots
  34. if (!(rawMessage is SocketUserMessage message))
  35. return;
  36. if (message.Source != MessageSource.User)
  37. return;
  38. // This value holds the offset where the prefix ends
  39. var argPos = 0;
  40. // Perform prefix check. You may want to replace this with
  41. // (!message.HasCharPrefix('!', ref argPos))
  42. // for a more traditional command format like !help.
  43. if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos))
  44. return;
  45. var context = new SocketCommandContext(_discord, message);
  46. // Perform the execution of the command. In this method,
  47. // the command service will perform precondition and parsing check
  48. // then execute the command if one is matched.
  49. await _commands.ExecuteAsync(context, argPos, _services);
  50. // Note that normally a result will be returned by this format, but here
  51. // we will handle the result in CommandExecutedAsync,
  52. }
  53. public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
  54. {
  55. // command is unspecified when there was a search failure (command not found); we don't care about these errors
  56. if (!command.IsSpecified)
  57. return;
  58. // the command was successful, we don't care about this result, unless we want to log that a command succeeded.
  59. if (result.IsSuccess)
  60. return;
  61. // the command failed, let's notify the user that something happened.
  62. await context.Channel.SendMessageAsync($"error: {result}");
  63. }
  64. }
  65. }