I want to use options in choice in slash commands. Let me explain:
/info user
User writes this slash command. The "user" is choice in this command. If we write "user" as the choice, it should give an user option. Like:
/info server
If we write like that, it'll just execute the command, because the server does not need anymore info.
/info user
But if we write like that, it'll ask for an user. You can add user options by writing .addUserOption, but I should add it to choice, I don't know how to do it. Here is my code:
const { SlashCommandBuilder } = require("@discordjs/builders")
module.exports = {
data: new SlashCommandBuilder()
.setName("info")
.setDescription("Get info about X! (usage: /info X)")
.addStringOption((o) =>
o
.setName("choice")
.setDescription("What info do you want to get?")
.setRequired(true)
.addChoice('Server', 'server')
.addChoice('User', 'user')
.addUserOption((o) =>
o
.setName("user")
.setDescription("What user do you want to get info about?")
.setRequired(true)
)
.addChoice('Bot', 'bot')
),
async execute(i) {
if (i.options.getString("choice") == 'server') {
i.reply({
content: `server`,
emphemeral: true
})
}
if (i.options.getString("choice") == 'user') {
i.reply({
content: `user`,
emphemeral: true
})
}
if (i.options.getString("choice") == 'bot') {
i.reply({
content: `bot`,
emphemeral: true
})
}
}
}
That's what I tried. It gives me an error. How can I do it? Thanks.
.addStringOption() has its own choices.
After .addStringOption(), you unindent it so it is on the same line where that started, then add .addUserOption() with its own choices.
// ...
.addStringOption(() => {
// ...
})
.addChoice("Foo", "foo")
.addChoice("Bar", "bar")
.addUserOption(() => {
// ...
})
.addChoice("<@1234567890>")
.addChoice("<@1234567890>")