#ComponentInteraction, how to use
1 messages · Page 1 of 1 (latest)
wym well here is my full code
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
namespace ResellerBot.Modules
{
public class Redeem : InteractionModuleBase<SocketInteractionContext>
{
public InteractionService Commands { get; set; }
private CommandHandler _handler;
// Constructor injection is also a valid way to access the dependencies
public Redeem(CommandHandler handler)
{
_handler = handler;
}
private readonly Dictionary<ulong, (Product Product, string License)> _userProductLicenseMap = new Dictionary<ulong, (Product, string)>();
[SlashCommand("showmodal", "d")]
public async Task ShowModalAsync(Product product, string license)
{
var button = new ButtonBuilder()
.WithLabel("test")
.WithStyle(ButtonStyle.Danger)
.WithCustomId("test");
var userId = Context.User.Id;
_userProductLicenseMap[userId] = (product, license);
await RespondAsync("testing", components: new ComponentBuilder().WithButton(button).Build());
}
[ComponentInteraction("test")]
public async Task Handlebutton(SocketMessageComponent interaction)
{
var userId = interaction.User.Id;
if (_userProductLicenseMap.TryGetValue(userId, out var productLicense))
{
var (product, license) = productLicense;
// Validate the key
if (!KeyAuth.KeyExists(Infos.SellerKeys[product], license))
{
var errorEmbed = new EmbedBuilder()
.WithTitle("KeyGen Auth - Error | Download")
.WithColor(Color.DarkBlue)
.WithDescription("Invalid license.")
.WithTimestamp(DateTimeOffset.Now)
.WithFooter(footer =>
{
footer.Text = "KeyGen Auth";
footer.IconUrl = "https://media.discordapp.net/attachments/1198501367078207488/1198672891265437716/411e7bf6b2ff3ed597a4f4a76c541283.png?ex=65bfc21f&is=65ad4d1f&hm=3b82b79d22fd7c8b1e1ff9d3828ba9dbaec881f8b2a2e45e8554c4a3eb2f9912&=&format=webp&quality=lossless";
})
.Build();
await interaction.FollowupAsync(embeds: new[] { errorEmbed }, ephemeral: true);
return;
}
// Proceed with the modal since the key is valid
await interaction.RespondWithModalAsync<Demomodal>("demo_modal");
}
}
[ModalInteraction("demo_modal")]
public async Task Handlemodal(Demomodal modal)
{
string input = modal.Greeting;
// Assume you want to send an embed with the input
var successEmbed = new EmbedBuilder()
.WithTitle("Success")
.WithColor(Color.Green)
.WithDescription($"You entered: {input}")
.WithTimestamp(DateTimeOffset.Now)
.Build();
await FollowupAsync(embeds: new[] { successEmbed }, ephemeral: true);
}
public class Demomodal : IModal
{
public string Title => "Test";
[InputLabel("Test")]
[ModalTextInput("test_input", TextInputStyle.Short, placeholder: "Test", maxLength: 100)]
public string Greeting { get; set; }
}
}
}```
ok so basiclly
the embed sends with button
then button is clicked into a modal
then
enter the key
then if its vaild it sends the embed
and if its invaid it sends the error embed