Skip to content

Instantly share code, notes, and snippets.

@HugoVG
Created May 15, 2022 14:29
Show Gist options
  • Save HugoVG/28e258296767720e857ed77dc0767d72 to your computer and use it in GitHub Desktop.
Save HugoVG/28e258296767720e857ed77dc0767d72 to your computer and use it in GitHub Desktop.
Popup Modal
// In here(the command file for the !help or /Dosomething ) you make the modal pop up
[SlashCommand("newModalPopup", "popsup a sample Modal")]
public async Task newModalPopup(InteractionContext ctx)
{
var response = new DiscordInteractionResponseBuilder();
response
.WithTitle("the title of the modal ")
.WithCustomId(ModalEvent.modalNames.{name you created in ModalEvent.cs line 5 }) //name you created in ModalEvent.cs line 5
.AddComponents(new TextInputComponent(label: "Favorite food", customId: "myvaluefrominputfield", placeholder: "Pizza, Icecream, etc", max_length: 30)) //customId is used in the Handler to get the value later
.AddComponents(new TextInputComponent("Why?", "why-fav", "Because it tastes good", required: false, style: TextInputStyle.Paragraph));
//.AddComponents() accepts multiple elements, 2 components in 1 call will place them on the same row
await ctx.CreateResponseAsync(InteractionResponseType.Modal, response); // create modal respone
}
public static class modalNames
{
public const string myModal = "my-modal";
//#######################################
//Insert a new public const string with the Custom-id you gave your window
////#######################################
}
//#########################################
//Insert a new case in the Switch statement
//Make a new class for the modal see {newModal.cs}
//#########################################
public static async Task ModalSubmitHandler(DiscordClient s, ModalSubmitEventArgs e)
{
string name = e.Interaction.Data.CustomId;
Console.WriteLine(name);
switch (name)
{
case (modalNames.myModal):
{
new AddActivityModal(s, e);
break;
}
//##############
// <-- Here
//##############
default:
{
break;
}
}
}
//in /SupportClasses/ModalFunctions
//make a new cs file called {something}Modal so we know what it is
//the modal will look like this
public class newModal : ModalBase //inherit from ModalBase so we get s and e. s is for the bot like change activity, e is for values and the channel
{
public newModal(DiscordClient s, ModalSubmitEventArgs e) : base(s, e) {} //constructor that calls ModalHandler no need to call it
public override void ModalHandler()
{
//Your custom logic here
//Always end with this so the pop up will go away
e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder { Content = {your message}, IsEphemeral = false });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment