Skip to content

Commit

Permalink
feat: dropdown-select-list
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Jun 26, 2024
1 parent 848919c commit a225246
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Commands/Components/DiscordComponentCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;

namespace csharp_discord_bot.Commands.Components
{
/// <summary>
/// Discord Components [Dropdowns, Modal]
/// </summary>
public class DiscordComponentCommands : BaseCommandModule
{
[Command("dropdown-list")]
public async Task DropDownList(CommandContext ctx)
{
List<DiscordSelectComponentOption> optionsList = [
new DiscordSelectComponentOption("Option 1", "option1"),
new DiscordSelectComponentOption("Option 2", "option2"),
new DiscordSelectComponentOption("Option 3", "option3")
];

var options = optionsList.AsEnumerable();

var dropdown = new DiscordSelectComponent("dropDownList", "Select....", options);

var dropdownMessage = new DiscordMessageBuilder()
.AddEmbed(new DiscordEmbedBuilder()
.WithColor(DiscordColor.Gold)
.WithTitle("This embed has a dropdown list on it")
).AddComponents(dropdown);

await ctx.Channel.SendMessageAsync(dropdownMessage);
}
}
}
29 changes: 29 additions & 0 deletions Helpers/DiscordSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static async Task MainAsync()
Commands.RegisterCommands<CardGameEmbed>();
Commands.RegisterCommands<PollCommand>();
Commands.RegisterCommands<InteractionComponents>();
Commands.RegisterCommands<DiscordComponentCommands>();

await Client.ConnectAsync();
await Task.Delay(-1); // to keep bot running forever, as long as the program is running
Expand All @@ -97,8 +98,36 @@ private static async Task GuildMemberHandler(DiscordClient sender, GuildMemberAd
await defaultChannel.SendMessageAsync(embed: welcomeEmbed);
}

/// <summary>
/// Component Interactons Created Event
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
private static async Task ComponentInteractionCreated(DiscordClient sender, ComponentInteractionCreateEventArgs args)
{
if (args.Id == "dropDownList" && args.Interaction.Data.ComponentType == ComponentType.StringSelect)
{
Console.WriteLine("Dropdown....");
var options = args.Values;
foreach (var option in options)
{
switch (option)
{
case "option1":
await args.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, new DiscordInteractionResponseBuilder().WithContent($"{args.User.Username} has Selected Option 1"));
break;
case "option2":
await args.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, new DiscordInteractionResponseBuilder().WithContent($"{args.User.Username} has Selected Option 2"));
break;
case "option3":
await args.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, new DiscordInteractionResponseBuilder().WithContent($"{args.User.Username} has Selected Option 3"));
break;
}
}
}

// --------- Button Events
switch (args.Interaction.Data.CustomId)
{
case "button1":
Expand Down

0 comments on commit a225246

Please sign in to comment.