C
C#ā€¢3y ago
REAPER

Slash Command optional parameter [Answered]

Hi, I'm currently working on a poll command where I'm using SelectMenuBuilder and I want for there to be 2 required answers to create the poll. I want the rest of the answers to be optional. Is it possible to make parameters optional or will they always be required? So far I've only been able to find the [Optional] but it only hides it when using the command, but I'm still required to enter all 8 answers to make the command work...
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, [Optional] string answer3, [Optional] string answer4,
[Optional] string answer5, [Optional] string answer6, [Optional] string answer7, [Optional] string answer8)
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2")
.AddOption(answer3, "answer-3")
.AddOption(answer4, "answer-4")
.AddOption(answer5, "answer-5")
.AddOption(answer6, "answer-6")
.AddOption(answer7, "answer-7")
.AddOption(answer8, "answer-8");

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, [Optional] string answer3, [Optional] string answer4,
[Optional] string answer5, [Optional] string answer6, [Optional] string answer7, [Optional] string answer8)
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2")
.AddOption(answer3, "answer-3")
.AddOption(answer4, "answer-4")
.AddOption(answer5, "answer-5")
.AddOption(answer6, "answer-6")
.AddOption(answer7, "answer-7")
.AddOption(answer8, "answer-8");

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
60 Replies
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
REAPER
REAPEROPā€¢3y ago
Wouldn't that just make the unused answers pre-filled? rooThink
pip
pipā€¢3y ago
I think OP's question is more referring to Discord command arguments. To answer your question, is it possible to have 2 required answers, then each subsequent answer optional? The answer is no, all of this information is registered at the Discord level as a part of the command itself. There's no dynamic checks on the Discord level to see if the parameters match your logic Notice how your bot's logic doesn't receive the command until it is actually executed, but Discord knows all of your parameters. This is because you register the parameters of the command initially, and when the command itself is executed then you're able to perform your logic
REAPER
REAPEROPā€¢3y ago
What I'm looking for is something like this bot. When using the Slash command you can enter different answers but you can leave the unwanted once blank. @pip If I'm understanding your message correctly then it's not possible for me to do this with my variables as parameters, is that correct? rooThink
pip
pipā€¢3y ago
well isn't that just using MaxValues(2)? in your example that's maybe slightly different from what you described of creating optional parameters after hitting max values
REAPER
REAPEROPā€¢3y ago
If I use MaxValues(2) then I will allow the user to vote for 2 things in the poll
pip
pipā€¢3y ago
Ok I think i see the problem Yea so you want all parameters optional, but you need at least one parameters for the command to execute
REAPER
REAPEROPā€¢3y ago
I need the Question and at least 2 answers and then the rest of the answers are optional
pip
pipā€¢3y ago
So just use MinValues(2)? I'm following with you I'm sorry if I'm misunderstanding the point, but I've read everything you've written so far
REAPER
REAPEROPā€¢3y ago
I might be explaining things a bit weird some times šŸ˜…
pip
pipā€¢3y ago
AH
REAPER
REAPEROPā€¢3y ago
The MinValues forces the user to select at least 2 things to vote for right?
pip
pipā€¢3y ago
allow me to fix your code
REAPER
REAPEROPā€¢3y ago
I'm perfectly happy with my pollBuilder but I'm having issues with the answer3,4,5,6,7,8 in the parameters if I don't enter something in them when using the slash command
pip
pipā€¢3y ago
public async Task PollAsync(string Question, string answer, [Optional] params string[] options) { var pollBuilder = new SelectMenuBuilder() .WithPlaceholder("Select an option") .WithCustomId("menu-1") .WithMinValues(1) .WithMaxValues(1).AddOption(answer); foreach(var option in options) { pollBuilder = pollBuilder.AddOption(option); } var builder = new ComponentBuilder() .WithSelectMenu(pollBuilder); await RespondAsync(Question, components: builder.Build()); } this worked a year ago or so i'm not sure if it still does with slash commands so try that out, see if that fixes your problem the other way to do it is with the parameters you have, then just do null checks on each one. if not null, add to selectmenubuilder
REAPER
REAPEROPā€¢3y ago
REAPER
REAPEROPā€¢3y ago
It's apparently not happy with the type
pip
pipā€¢3y ago
public async Task PollAsync(string Question, string answer, [Optional] params string[] options)
{

var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1).AddOption(answer);

int counter = 3;
foreach(var option in options) { pollBuilder = pollBuilder.AddOption(option, $"answer-{counter}"); counter++;}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
}
public async Task PollAsync(string Question, string answer, [Optional] params string[] options)
{

var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1).AddOption(answer);

int counter = 3;
foreach(var option in options) { pollBuilder = pollBuilder.AddOption(option, $"answer-{counter}"); counter++;}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
}
and yea, keep your stuff with answer1 and 2 at the top
REAPER
REAPEROPā€¢3y ago
This is what I currently have
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, [Optional] params string[] options)
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2");

int counter = 3;
foreach (var option in options)
{
pollBuilder = pollBuilder.AddOption(option, $"answer-{counter}");
counter++;
}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, [Optional] params string[] options)
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2");

int counter = 3;
foreach (var option in options)
{
pollBuilder = pollBuilder.AddOption(option, $"answer-{counter}");
counter++;
}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
REAPER
REAPEROPā€¢3y ago
When I run the bot I get this error?
pip
pipā€¢3y ago
hmm so it doesn't like the optional params let me check the docs rq
REAPER
REAPEROPā€¢3y ago
Gotta admit sometimes the written commands were easier to make šŸ˜…
pip
pipā€¢3y ago
They for sure are much easier. Slash commands are rough to work with
REAPER
REAPEROPā€¢3y ago
On the other hand they are way smoother to use rooEZ
pip
pipā€¢3y ago
yea better for users um just use your initial solution and do null checks
REAPER
REAPEROPā€¢3y ago
REAPER
REAPEROPā€¢3y ago
Like this?
pip
pipā€¢3y ago
nah remove the params thing I told you to add. use your initial optional stuff. if(option3 is not null) { pollBuilder = pollBuilder.AddOption(option3, "option3"); } and the same for 4,5,6,etc it's not pretty but i'm not sure how to do params with slash commands
REAPER
REAPEROPā€¢3y ago
Is this what you meant?
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, [Optional] string answer3, [Optional] string answer4, [Optional] string answer5,
[Optional] string answer6, [Optional] string answer7, [Optional] string answer8)
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2");

if (answer3 is not null)
{
pollBuilder = pollBuilder.AddOption(answer3, $"answer-3");
}
if (answer4 is not null)
{
pollBuilder = pollBuilder.AddOption(answer4, "answer-4");
}
if (answer5 is not null)
{
pollBuilder = pollBuilder.AddOption(answer5, "answer-5");
}
if (answer6 is not null)
{
pollBuilder = pollBuilder.AddOption(answer6, "answer-6");
}
if (answer7 is not null)
{
pollBuilder = pollBuilder.AddOption(answer7, "answer-7");
}
if (answer8 is not null)
{
pollBuilder = pollBuilder.AddOption(answer8, "answer-8");
}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
}
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, [Optional] string answer3, [Optional] string answer4, [Optional] string answer5,
[Optional] string answer6, [Optional] string answer7, [Optional] string answer8)
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2");

if (answer3 is not null)
{
pollBuilder = pollBuilder.AddOption(answer3, $"answer-3");
}
if (answer4 is not null)
{
pollBuilder = pollBuilder.AddOption(answer4, "answer-4");
}
if (answer5 is not null)
{
pollBuilder = pollBuilder.AddOption(answer5, "answer-5");
}
if (answer6 is not null)
{
pollBuilder = pollBuilder.AddOption(answer6, "answer-6");
}
if (answer7 is not null)
{
pollBuilder = pollBuilder.AddOption(answer7, "answer-7");
}
if (answer8 is not null)
{
pollBuilder = pollBuilder.AddOption(answer8, "answer-8");
}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
}
pip
pipā€¢3y ago
yup its ugly af but it'll get the job done
REAPER
REAPEROPā€¢3y ago
Well sad news is that it didn't rooCry still can't leave the other answers empty/null
pip
pipā€¢3y ago
o
REAPER
REAPEROPā€¢3y ago
Yeah rooFeels
pip
pipā€¢3y ago
try Optional<string> instead of string? Now im just guessing lol their docs aren't great ok that doesn't work
REAPER
REAPEROPā€¢3y ago
Yeah I couldn't get it to work either
pip
pipā€¢3y ago
do me a favor hover over "Optional" and tell me where it's from
REAPER
REAPEROPā€¢3y ago
It's from System.Runtime.InteropServices.OptionalAttribute
pip
pipā€¢3y ago
so here's the thing, all this is possible via the SlashCommandBuilder(this is what I use). I can't find any attributes/methodology for this using the interaction module the reason i asked this is because [Optional] isn't a part of Discord.Net, I don't think this will work?
REAPER
REAPEROPā€¢3y ago
Well so far it has worked fine. It doesn't make the the variables optional to fill out but it hides them away so you don't feel obligated to enter them
REAPER
REAPEROPā€¢3y ago
Like this where it shows the first 3 and for the rest it just says +6 more
REAPER
REAPEROPā€¢3y ago
I personally don't think it's coursing any issues rooThink I just tested some of the old code we did. If I remove anything that could course issues and changes the optional strings to the params string[] then I still get the compilor error. I don't think Slash Commands like params... rooThink
REAPER
REAPEROPā€¢3y ago
This is the error
pip
pipā€¢3y ago
yea i tried it as well, this is something that used to work with regular commands causing* not coursing btw
REAPER
REAPEROPā€¢3y ago
Oh yeah sry šŸ˜…
pip
pipā€¢3y ago
all good, so yea I'm still looking through their docs for examples on how to use attributes for parameters
pip
pipā€¢3y ago
Discord.Net Docs
Class ParameterPreconditionAttribute | Discord.Net Documentation
Requires the parameter to pass the specified precondition before execution can begin.
pip
pipā€¢3y ago
i found the answer you ready? it's much easier than i thought
REAPER
REAPEROPā€¢3y ago
Thank god šŸ˜„ I like easy šŸ˜„
pip
pipā€¢3y ago
string optionalParameter = ""
REAPER
REAPEROPā€¢3y ago
Let me test that before celebrating šŸ˜‰
pip
pipā€¢3y ago
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, string answer3 = "", string answer4 = "", string answer5 = "",
string answer6 = "", string answer7 = "", string answer8 = "")
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2");

if (answer3 is not "")
{
pollBuilder = pollBuilder.AddOption(answer3, $"answer-3");
}
if (answer4 is not "")
{
pollBuilder = pollBuilder.AddOption(answer4, "answer-4");
}
if (answer5 is not "")
{
pollBuilder = pollBuilder.AddOption(answer5, "answer-5");
}
if (answer6 is not "")
{
pollBuilder = pollBuilder.AddOption(answer6, "answer-6");
}
if (answer7 is not "")
{
pollBuilder = pollBuilder.AddOption(answer7, "answer-7");
}
if (answer8 is not "")
{
pollBuilder = pollBuilder.AddOption(answer8, "answer-8");
}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
}
[SlashCommand("poll", "Create a poll where people can vote")]
public async Task PollAsync(string Question, string answer1, string answer2, string answer3 = "", string answer4 = "", string answer5 = "",
string answer6 = "", string answer7 = "", string answer8 = "")
{
var pollBuilder = new SelectMenuBuilder()
.WithPlaceholder("Select an option")
.WithCustomId("menu-1")
.WithMinValues(1)
.WithMaxValues(1)
.AddOption(answer1, "answer-1")
.AddOption(answer2, "answer-2");

if (answer3 is not "")
{
pollBuilder = pollBuilder.AddOption(answer3, $"answer-3");
}
if (answer4 is not "")
{
pollBuilder = pollBuilder.AddOption(answer4, "answer-4");
}
if (answer5 is not "")
{
pollBuilder = pollBuilder.AddOption(answer5, "answer-5");
}
if (answer6 is not "")
{
pollBuilder = pollBuilder.AddOption(answer6, "answer-6");
}
if (answer7 is not "")
{
pollBuilder = pollBuilder.AddOption(answer7, "answer-7");
}
if (answer8 is not "")
{
pollBuilder = pollBuilder.AddOption(answer8, "answer-8");
}

var builder = new ComponentBuilder()
.WithSelectMenu(pollBuilder);

await RespondAsync(Question, components: builder.Build());
}
i tested it on one of my bots
REAPER
REAPEROPā€¢3y ago
OMG you're right it works rooHappy was actually close to this result earlier but ended up taking another route... that was a mistake šŸ¤£
pip
pipā€¢3y ago
I'm almost mad that it's so easy, I wish this was documented anywhere šŸ¤£ (or if it is documented somewhere, maybe easier to find)
REAPER
REAPEROPā€¢3y ago
Oh yeah for sure but on the other hand I'm not complaining xD Once again thanks for taking your time to help me maintain my bot when I'm running into issues rooHappy
pip
pipā€¢3y ago
no prob
Accord
Accordā€¢3y ago
āœ… This post has been marked as answered!
REAPER
REAPEROPā€¢3y ago
I guess the first guy wasn't that far of then šŸ˜…
pip
pipā€¢3y ago
yup he was completely right lmao. šŸ¤·ā€ā™€ļø
REAPER
REAPEROPā€¢3y ago
That's actually even more annoying that we could have found the solution faster if we had just listened more šŸ¤£
pip
pipā€¢3y ago
you're completely right
Want results from more Discord servers?
Add your server