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.

07-choice-slash-command.md 2.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ---
  2. uid: Guides.SlashCommands.Choices
  3. title: Slash Command Choices
  4. ---
  5. # Slash Command Choices.
  6. With slash command options you can add choices, making the user select between some set values. Lets create a command that asks how much they like our bot!
  7. Let's set up our slash command:
  8. ```cs
  9. private async Task Client_Ready()
  10. {
  11. ulong guildId = 848176216011046962;
  12. var guildCommand = new SlashCommandBuilder()
  13. .WithName("feedback")
  14. .WithDescription("Tell us how much you are enjoying this bot!")
  15. .AddOption(new SlashCommandOptionBuilder()
  16. .WithName("rating")
  17. .WithDescription("The rating your willing to give our bot")
  18. .WithRequired(true)
  19. .AddChoice("Terrible", 1)
  20. .AddChoice("Meh", 2)
  21. .AddChoice("Good", 3)
  22. .AddChoice("Lovely", 4)
  23. .AddChoice("Excellent!", 5)
  24. .WithType(ApplicationCommandOptionType.Integer)
  25. ).Build();
  26. try
  27. {
  28. await client.Rest.CreateGuildCommand(guildCommand.Build(), guildId);
  29. }
  30. catch(ApplicationCommandException exception)
  31. {
  32. var json = JsonConvert.SerializeObject(exception.Error, Formatting.Indented);
  33. Console.WriteLine(json);
  34. }
  35. }
  36. ```
  37. > **Note:** Your `ApplicationCommandOptionType` specifies which type your choices are, you need to use `ApplicationCommandOptionType.Integer` if choices whos value are numbers and `ApplicationCommandOptionType.String` for string values.
  38. We have defined 5 choices for the user to pick from, each choice has a value assigned to it. The value can either be a string or an int. In our case we're going to use an int. This is what the command looks like:
  39. ![feedback style](images/feedback1.png)
  40. Lets add our code for handling the interaction.
  41. ```cs
  42. private async Task Client_InteractionCreated(SocketInteraction arg)
  43. {
  44. if(arg is SocketSlashCommand command)
  45. {
  46. // Let's add a switch statement for the command name so we can handle multiple commands in one event.
  47. switch(command.Data.Name)
  48. {
  49. case "list-roles":
  50. await HandleListRoleCommand(command);
  51. break;
  52. case "settings":
  53. await HandleSettingsCommand(HandleFeedbackCommand);
  54. break;
  55. case "feedback":
  56. await HandleFeedbackCommand(command);
  57. break;
  58. }
  59. }
  60. }
  61. private async Task HandleFeedbackCommand(SocketSlashCommand command)
  62. {
  63. var embedBuilder = new EmbedBuilder()
  64. .WithAuthor(command.User)
  65. .WithTitle("Feedback")
  66. .WithDescription($"Thanks for your feedback! You rated us {command.Data.Options.First().Value}/5")
  67. .WithColor(Color.Green)
  68. .WithCurrentTimestamp();
  69. await command.RespondAsync(embed: embedBuilder.Build());
  70. }
  71. ```
  72. And this is the result:
  73. ![feedback working](images/feedback2.png)