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.

04-parameters.md 3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ---
  2. uid: Guides.SlashCommands.Parameters
  3. title: Slash Command Parameters
  4. ---
  5. # Slash command parameters
  6. Slash commands can have a bunch of parameters, each their own type. Let's first go over the types of parameters we can have.
  7. | Name | Description |
  8. | --------------- | -------------------------------------------------- |
  9. | SubCommand | A subcommand inside of a subcommand group. |
  10. | SubCommandGroup | The parent command group of subcommands. |
  11. | String | A string of text. |
  12. | Integer | A number. |
  13. | Boolean | True or False. |
  14. | User | A user |
  15. | Channel | A channel, this includes voice text and categories |
  16. | Role | A role. |
  17. | Mentionable | A role or a user. |
  18. Each one of the parameter types has its own DNET type in the `SocketSlashCommandDataOption`'s Value field:
  19. | Name | C# Type |
  20. | --------------- | ------------------------------------------------ |
  21. | SubCommand | NA |
  22. | SubCommandGroup | NA |
  23. | String | `string` |
  24. | Integer | `int` |
  25. | Boolean | `bool` |
  26. | User | `SocketGuildUser` or `SocketUser` |
  27. | Role | `SocketRole` |
  28. | Channel | `SocketChannel` |
  29. | Mentionable | `SocketUser`, `SocketGuildUser`, or `SocketRole` |
  30. Let's start by making a command that takes in a user and lists their roles.
  31. ```cs
  32. client.Ready += Client_Ready;
  33. ...
  34. public async Task Client_Ready()
  35. {
  36. ulong guildId = 848176216011046962;
  37. var guildCommand = new SlashCommandBuilder()
  38. .WithName("list-roles")
  39. .WithDescription("Lists all roles of a user.")
  40. .AddOption("user", ApplicationCommandOptionType.User, "The users whos roles you want to be listed", isRequired: true);
  41. try
  42. {
  43. await client.Rest.CreateGuildCommand(guildCommand.Build(), guildId);
  44. }
  45. catch(ApplicationCommandException exception)
  46. {
  47. var json = JsonConvert.SerializeObject(exception.Error, Formatting.Indented);
  48. Console.WriteLine(json);
  49. }
  50. }
  51. ```
  52. ![list roles command](images/listroles1.png)
  53. That seems to be working, now Let's handle the interaction.
  54. ```cs
  55. private async Task SlashCommandHandler(SocketSlashCommand command)
  56. {
  57. // Let's add a switch statement for the command name so we can handle multiple commands in one event.
  58. switch(command.Data.Name)
  59. {
  60. case "list-roles":
  61. await HandleListRoleCommand(command);
  62. break;
  63. }
  64. }
  65. private async Task HandleListRoleCommand(SocketSlashCommand command)
  66. {
  67. // We need to extract the user parameter from the command. since we only have one option and it's required, we can just use the first option.
  68. var guildUser = (SocketGuildUser)command.Data.Options.First().Value;
  69. // We remove the everyone role and select the mention of each role.
  70. var roleList = string.Join(",\n", guildUser.Roles.Where(x => !x.IsEveryone).Select(x => x.Mention));
  71. var embedBuiler = new EmbedBuilder()
  72. .WithAuthor(guildUser.ToString(), guildUser.GetAvatarUrl() ?? guildUser.GetDefaultAvatarUrl())
  73. .WithTitle("Roles")
  74. .WithDescription(roleList)
  75. .WithColor(Color.Green)
  76. .WithCurrentTimestamp();
  77. // Now, Let's respond with the embed.
  78. await command.RespondAsync(embed: embedBuiler.Build());
  79. }
  80. ```
  81. ![working list roles](images/listroles2.png)
  82. That has worked! Next, we will go over responding ephemerally.