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 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Reflection;
  3. using System.Threading.Tasks;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Discord;
  6. using Discord.Commands;
  7. using Discord.WebSocket;
  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)) return;
  35. if (message.Source != MessageSource.User) return;
  36. // This value holds the offset where the prefix ends
  37. var argPos = 0;
  38. // Perform prefix check. You may want to replace this with
  39. // (!message.HasCharPrefix('!', ref argPos))
  40. // for a more traditional command format like !help.
  41. if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos)) return;
  42. var context = new SocketCommandContext(_discord, message);
  43. // Perform the execution of the command. In this method,
  44. // the command service will perform precondition and parsing check
  45. // then execute the command if one is matched.
  46. await _commands.ExecuteAsync(context, argPos, _services);
  47. // Note that normally a result will be returned by this format, but here
  48. // we will handle the result in CommandExecutedAsync,
  49. }
  50. public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
  51. {
  52. // command is unspecified when there was a search failure (command not found); we don't care about these errors
  53. if (!command.IsSpecified)
  54. return;
  55. // the command was successful, we don't care about this result, unless we want to log that a command succeeded.
  56. if (result.IsSuccess)
  57. return;
  58. // the command failed, let's notify the user that something happened.
  59. await context.Channel.SendMessageAsync($"error: {result}");
  60. }
  61. }
  62. }