diff --git a/README.md b/README.md index 41c902b0d..560894c71 100644 --- a/README.md +++ b/README.md @@ -32,52 +32,65 @@ webmilio's spin on the SlashCommandService branch, again the state of this is un client.InteractionCreated += Client_InteractionCreated; ... -private async Task Client_InteractionCreated(SocketInteraction arg) +private async Task Client_InteractionCreated(SocketInteraction interaction) { - switch (arg.Type) // We want to check the type of this interaction + // Checking the type of this interaction + switch (interaction) { - //Slash commands - case InteractionType.ApplicationCommand: - await MySlashCommandHandler(arg); + // Slash commands + case SocketSlashCommand commandInteraction: + await MySlashCommandHandler(commandInteraction); break; - //Button clicks/selection dropdowns - case InteractionType.MessageComponent: - await MyMessageComponentHandler(arg); + + // Button clicks/selection dropdowns + case SocketMessageComponent componentInteraction: + await MyMessageComponentHandler(componentInteraction); break; - //Unused - case InteractionType.Ping: - break; - //Unknown/Unsupported + + // Unused or Unknown/Unsupported default: - Console.WriteLine("Unsupported interaction type: " + arg.Type); break; } } ``` -### Handling button clicks and selection dropdowns +### Simple handling slash commands ```cs -private async Task MyMessageComponentHandler(SocketInteraction arg) +private async Task MySlashCommandHandler(SocketSlashCommand interaction) +{ + // Checking command name + if (interaction.Data.Name == "ping") + { + // Respond to interaction with message. + // You can also use "ephemeral" so that only the original user of the interaction sees the message + await interaction.RespondAsync($"Pong!", ephemeral: true); + + // Also you can followup with a additional messages, which also can be "ephemeral" + await interaction.FollowupAsync($"PongPong!", ephemeral: true); + } +} +``` + +### Simple handling button clicks and selection dropdowns +```cs +private async Task MyMessageComponentHandler(SocketMessageComponent interaction) { - // Parse the arg - var parsedArg = (SocketMessageComponent) arg; // Get the custom ID - var customId = parsedArg.Data.CustomId; + var customId = interaction.Data.CustomId; // Get the user - var user = (SocketGuildUser) arg.User; + var user = (SocketGuildUser) interaction.User; // Get the guild var guild = user.Guild; - // Respond with the update message response type. This edits the original message if you have set AlwaysAcknowledgeInteractions to false. - // You can also use "ephemeral" so that only the original user of the interaction sees the message - await parsedArg.RespondAsync($"Clicked {parsedArg.Data.CustomId}!", type: InteractionResponseType.UpdateMessage, ephemeral: true); + // Respond with the update message. This edits the message which this component resides. + await interaction.UpdateAsync(msgProps => msgProps.Content = $"Clicked {interaction.Data.CustomId}!"); - // You can also followup with a second message - await parsedArg.FollowupAsync($"Clicked {parsedArg.Data.CustomId}!", type: InteractionResponseType.ChannelMessageWithSource, ephemeral: true); + // Also you can followup with a additional messages + await interaction.FollowupAsync($"Clicked {interaction.Data.CustomId}!", ephemeral: true); - //If you are using selection dropdowns, you can get the selected label and values using these: - var selectedLabel = ((SelectMenu) parsedArg.Message.Components.First().Components.First()).Options.FirstOrDefault(x => x.Value == parsedArg.Data.Values.FirstOrDefault())?.Label; - var selectedValue = parsedArg.Data.Values.First(); + // If you are using selection dropdowns, you can get the selected label and values using these + var selectedLabel = ((SelectMenu) interaction.Message.Components.First().Components.First()).Options.FirstOrDefault(x => x.Value == interaction.Data.Values.FirstOrDefault())?.Label; + var selectedValue = interaction.Data.Values.First(); } ``` @@ -97,18 +110,14 @@ var builder = new ComponentBuilder() .WithSelectMenu(new SelectMenuBuilder() .WithCustomId("id_2") .WithPlaceholder("This is a placeholder") - .WithOptions(new List() - { - new SelectMenuOptionBuilder() - .WithLabel("Option A") - .WithEmote(Emote.Parse("<:evanpog:810017136814194698>")) - .WithDescription("Evan pog champ") - .WithValue("value1"), - new SelectMenuOptionBuilder() - .WithLabel("Option B") - .WithDescription("Option B is poggers") - .WithValue("value2") - })); + .AddOption( + label: "Option", + value: "value1", + description: "Evan pog champ", + emote: Emote.Parse("<:evanpog:810017136814194698>") + ) + .AddOption("Option B", "value2", "Option B is poggers"); + await Context.Channel.SendMessageAsync("Test selection!", component: builder.Build()); ``` diff --git a/docs/guides/emoji/emoji.md b/docs/guides/emoji/emoji.md index 60a84409c..dbf654bbf 100644 --- a/docs/guides/emoji/emoji.md +++ b/docs/guides/emoji/emoji.md @@ -46,14 +46,16 @@ form; this can be obtained in several different ways. ### Emoji Declaration After obtaining the Unicode representation of the emoji, you may -create the @Discord.Emoji object by passing the string into its +create the @Discord.Emoji object by passing the string with unicode into its constructor (e.g. `new Emoji("👌");` or `new Emoji("\uD83D\uDC4C");`). Your method of declaring an @Discord.Emoji should look similar to this: - [!code-csharp[Emoji Sample](samples/emoji-sample.cs)] +Also you can use `Emoji.Parse()` or `Emoji.TryParse()` methods +for parsing emojis from strings like `:heart:`, `<3` or `❤`. + [FileFormat.Info]: https://www.fileformat.info/info/emoji/list.htm ## Emote @@ -97,4 +99,4 @@ this: ## Additional Information To learn more about emote and emojis and how they could be used, -see the documentation of @Discord.IEmote. \ No newline at end of file +see the documentation of @Discord.IEmote. diff --git a/docs/guides/slash-commands/03-responding-to-slash-commands.md b/docs/guides/slash-commands/03-responding-to-slash-commands.md index 8ab7307d4..ad3c7145b 100644 --- a/docs/guides/slash-commands/03-responding-to-slash-commands.md +++ b/docs/guides/slash-commands/03-responding-to-slash-commands.md @@ -45,6 +45,6 @@ Let's try this out! Let's go over the response types quickly, as you would only change them for style points :P -> After receiving an interaction, you must respond to acknowledge it. You can choose to respond with a message immediately using `ChannelMessageWithSource` or you can choose to send a deferred response with `DeferredChannelMessageWithSource`. If choosing a deferred response, the user will see a loading state for the interaction, and you'll have up to 15 minutes to edit the original deferred response using Edit Original Interaction Response. You can read more about response types [here](https://discord.com/developers/docs/interactions/slash-commands#interaction-response) +> After receiving an interaction, you must respond to acknowledge it. You can choose to respond with a message immediately using `RespondAsync()` or you can choose to send a deferred response with `DeferAsync()`. If choosing a deferred response, the user will see a loading state for the interaction, and you'll have up to 15 minutes to edit the original deferred response using `ModifyOriginalResponseAsync()`. You can read more about response types [here](https://discord.com/developers/docs/interactions/slash-commands#interaction-response) This seems to be working! Next, we will look at parameters for slash commands.