C
C#2y ago
REAPER

✅ Discord Modal submits but keep pending?

Hi, I've made a Modal on my Discord Bot running on .Net 6 and it work as intended but for some reason it sends the message and keeps having a pending message, like it's trying to send what already got send rooThink the modal:
var modalBuilder = new ModalBuilder()
.WithTitle("Suggestion")
.WithCustomId("suggestion_id")
.AddTextInput("Enter a Title for your suggestion", "title_id", placeholder: "Title examples: Fishing shop")
.AddTextInput("What platform is your suggestion for?", "platform_id", placeholder: "Platforms: Discord server, Scuffed Manager, Scuffed Manager Website", required: true)
.AddTextInput("What is your suggestion?", "suggestion_input_id", placeholder: "Suggestion example: I would like for the bot to have...", required: true);
await Context.Interaction.RespondWithModalAsync(modalBuilder.Build());
var modalBuilder = new ModalBuilder()
.WithTitle("Suggestion")
.WithCustomId("suggestion_id")
.AddTextInput("Enter a Title for your suggestion", "title_id", placeholder: "Title examples: Fishing shop")
.AddTextInput("What platform is your suggestion for?", "platform_id", placeholder: "Platforms: Discord server, Scuffed Manager, Scuffed Manager Website", required: true)
.AddTextInput("What is your suggestion?", "suggestion_input_id", placeholder: "Suggestion example: I would like for the bot to have...", required: true);
await Context.Interaction.RespondWithModalAsync(modalBuilder.Build());
Sending the modal:
_client.ModalSubmitted += async modal =>
{
// Get the values of components.
List<SocketMessageComponentData> components = modal.Data.Components.ToList();
string title = components.First(x => x.CustomId == "title_id").Value;
string platform = components.First(x => x.CustomId == "platform_id").Value;
string suggestion = components.First(x => x.CustomId == "suggestion_input_id").Value;

// Build the message to send.
var embedBuilder = new EmbedBuilder()
{
Title = title,
Description = $"**Platform:** {platform}\n\n**Suggestion:** {suggestion}\n\n**Posted by:** {modal.User.Mention}",
Color = Color.Blue,
Timestamp = DateTime.Now,
};

// Respond to the modal.
await modal.RespondAsync(embed: embedBuilder.Build());
};
_client.ModalSubmitted += async modal =>
{
// Get the values of components.
List<SocketMessageComponentData> components = modal.Data.Components.ToList();
string title = components.First(x => x.CustomId == "title_id").Value;
string platform = components.First(x => x.CustomId == "platform_id").Value;
string suggestion = components.First(x => x.CustomId == "suggestion_input_id").Value;

// Build the message to send.
var embedBuilder = new EmbedBuilder()
{
Title = title,
Description = $"**Platform:** {platform}\n\n**Suggestion:** {suggestion}\n\n**Posted by:** {modal.User.Mention}",
Color = Color.Blue,
Timestamp = DateTime.Now,
};

// Respond to the modal.
await modal.RespondAsync(embed: embedBuilder.Build());
};
13 Replies
D.Mentia
D.Mentia2y ago
It looks like you're doing a .DeferLoadingAsync or DeferAsync on some component in the chain, probably the one where they send the command. You'll need to do a .FollowupAsync on whatever was deferred Or that RespondWithModalAsync is doing it automatically. Most likely you want to send the response as a .Followup on the original command message, not a response to the modal
REAPER
REAPER2y ago
I'm not entirely sure how to do the follow up part...
await Context.Interaction.FollowupAsync();
await Context.Interaction.FollowupAsync();
Something like this? rooThink
D.Mentia
D.Mentia2y ago
it can get complicated. The modal reference in ModalSubmitted might have a link to its parent interaction for you to FollowUp on But I'd mostly suggest scouring the modal docs if you haven't already, find some examples, because this doesn't seem like a thing that should be happening
REAPER
REAPER2y ago
Discord.Net Docs
Getting Started with Modals | Discord.Net Documentation
This guide will show you how to use modals and give a few examples of valid use cases.
D.Mentia
D.Mentia2y ago
dealing with the followups usually only happens if you intentionally deferred something, then you have to keep some reference to the thing you deferred so you can follow it up Hm. Well. Actually now that I look at it, a deferred one I think usually shows something like "Loading..." not 'sending command'.
Discord seems to think the bot didn't respond to or accept the slash command. The code you posted looks right to me, maybe double-check the setup stuff
REAPER
REAPER2y ago
You mean the setup of my bot?
D.Mentia
D.Mentia2y ago
Yeah, the parts that register the slash command handlers and whatnot. Without seeing code I can only guess, but maybe your slash command handler is using a different instance of the client? looking through one of my old bots, this set of commands in particular might be relevant...? Not sure
_client.InteractionCreated += async interaction =>
{
var scope = _serviceProvider.CreateScope();
var ctx = new SocketInteractionContext(_client, interaction);
await _interactionService.ExecuteCommandAsync(ctx, scope.ServiceProvider);
};
_client.InteractionCreated += async interaction =>
{
var scope = _serviceProvider.CreateScope();
var ctx = new SocketInteractionContext(_client, interaction);
await _interactionService.ExecuteCommandAsync(ctx, scope.ServiceProvider);
};
It seems like your bot is, clearly, getting the messages, but not acknowledging the slash commands (but it might be acknowledging the modal). Since it responds to the modal but not to the original slash command, it still kinda seems like whichever _client you put the .ModalSubmitted on, might not be the same client that is handling the slash commands, or something about its context isn't registered right and gets lost
REAPER
REAPER2y ago
REAPER
REAPER2y ago
You think it's because my _client.InteractionCreated is connected with HandleInteraction instead? rooThink
D.Mentia
D.Mentia2y ago
Whatever's in HandleInteraction might not be doing the right thing it should probably look exactly like the code I have above, that's standard stuff to connect up the context
REAPER
REAPER2y ago
Yeah mine is just a bit different 😅
REAPER
REAPER2y ago
Let me try your code and see if it fixes the issue rooHappy it works, hopefully none of my other things are going to break with this new setup 😅 If it does it will be a problem for the future 😉 Thanks a lot for the help rooHappy
Accord
Accord2y ago
Closed!