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.

DiscordClient.cs 2.9 kB

Implemented basic parameter recognition and passing them to the method. Details: Subcommands and Subcommand groups not yet implemented, they will require for some parts of the code to be re-done. More attributes can and should be implemented, such as [Required] and [Choice(... , ...)]. Breakdown: * Rectified line endings to LF, as per the settings of the project. * Added a new command to SlashCommandService and SlashCommandServiceHelper to register the found commands to discord. * Implemented CommandRegistrationOptions that can be used to configure the behaviour on registration - what to do with old commands, and with commands that already exist with the same name. A default version exists and can be accessed with CommandRegistrationOptions.Default * Modified the sample program to reflect the changes made to the SlashCommandService and to also register a new command that tests all 6 types of CommandOptions (except subcommand and subcommand group) * At the moment all commands are registered in my test guild, because the update for global commands is not instant. See SlashCommandServiceHelper.RegisterCommands(...) or line 221. * Modified SlashCommandInfo to parse arguments given from Interaction, unde in ExecuteAsync, and added method BuilDcommand that returns SlashCommandCreationProperties - which can be registered to Discord. * Renamed in the sample project PingCommand.cs to DevModule.cs * Added custom attribute Description for the command method's parameters. * Implemented SlashParameterInfo - and extension of the OptionBuilder that implements a method name Parse - takes DataOptions and gives out a cast object to be passed to the command Delegate. Planning on doing more with it. * Moved SlashCommandBuilder.cs to the same directory structure * Moved SlashCommandModule.cs and ISlashCommandModule.cs to its own folder.
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Discord;
  2. using Discord.Commands;
  3. using Discord.SlashCommands;
  4. using Discord.WebSocket;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using System;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. namespace SlashCommandsExample
  10. {
  11. class DiscordClient
  12. {
  13. public static DiscordSocketClient socketClient { get; set; } = new DiscordSocketClient();
  14. public static SlashCommandService _commands { get; set; }
  15. public static IServiceProvider _services { get; set; }
  16. private string botToken = "<YOUR TOKEN HERE>";
  17. public DiscordClient()
  18. {
  19. _commands = new SlashCommandService();
  20. _services = new ServiceCollection()
  21. .AddSingleton(socketClient)
  22. .AddSingleton(_commands)
  23. .BuildServiceProvider();
  24. socketClient.Log += SocketClient_Log;
  25. _commands.Log += SocketClient_Log;
  26. socketClient.InteractionCreated += InteractionHandler;
  27. // This is for dev purposes.
  28. // To avoid the situation in which you accidentally push your bot token to upstream, you can use
  29. // EnviromentVariables to store your key.
  30. botToken = Environment.GetEnvironmentVariable("DiscordSlashCommandsBotToken", EnvironmentVariableTarget.User);
  31. // Uncomment the next line of code to set the environment variable.
  32. // ------------------------------------------------------------------
  33. // | WARNING! |
  34. // | |
  35. // | MAKE SURE TO DELETE YOUR TOKEN AFTER YOU HAVE SET THE VARIABLE |
  36. // | |
  37. // ------------------------------------------------------------------
  38. //Environment.SetEnvironmentVariable("DiscordSlashCommandsBotToken",
  39. // "[YOUR TOKEN GOES HERE DELETE & COMMENT AFTER USE]",
  40. // EnvironmentVariableTarget.User);
  41. }
  42. public async Task RunAsync()
  43. {
  44. await socketClient.LoginAsync(TokenType.Bot, botToken);
  45. await socketClient.StartAsync();
  46. await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
  47. await _commands.RegisterCommandsAsync(socketClient, CommandRegistrationOptions.Default);
  48. await Task.Delay(-1);
  49. }
  50. private async Task InteractionHandler(SocketInteraction arg)
  51. {
  52. if(arg.Type == InteractionType.ApplicationCommand)
  53. {
  54. await _commands.ExecuteAsync(arg);
  55. }
  56. }
  57. private Task SocketClient_Log(LogMessage arg)
  58. {
  59. Console.WriteLine("[Discord] " + arg.ToString());
  60. return Task.CompletedTask;
  61. }
  62. }
  63. }