Handle multiple Autocomplete Events on the same Interaction

Okay so i am trying to handle more than one autocomplete interactions on one command> The command looks like this:
{
name: 'players',
description: 'Gets the scoreboard of a specific player.',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'playername',
description: 'The player you want the scores for.',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
},
{
name: 'action',
description: 'The action to show the scoreboard for.',
type: ApplicationCommandOptionType.String,
required: true,
choices: [...choices],
},
{
name: 'item',
description: 'The item to show the scoreboard for.',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
},
],
},
{
name: 'players',
description: 'Gets the scoreboard of a specific player.',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'playername',
description: 'The player you want the scores for.',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
},
{
name: 'action',
description: 'The action to show the scoreboard for.',
type: ApplicationCommandOptionType.String,
required: true,
choices: [...choices],
},
{
name: 'item',
description: 'The item to show the scoreboard for.',
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
},
],
},
If someone could help me on how to differentiate between those, that would be great!
20 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
if (interaction.commandName === 'scoreboard') {
// const subcommand = interaction.options.getSubcommand() as
// | 'leaderboard'
// | 'players';

console.log(focused);

const objectives = Object.keys(dictionary119).map((key) => key);
const action = interaction.options.getString('action');

if (!action) return interaction.respond([]);

if (action === 'custom') {
return interaction.respond(mapChoices(customScoreboardObjectives));
} else {
const targetObjectives = objectives
.filter((obj) => obj.startsWith(action))
.map((item) => item.replace(action, ''));

return interaction.respond(mapChoices(targetObjectives));
}
}
if (interaction.commandName === 'scoreboard') {
// const subcommand = interaction.options.getSubcommand() as
// | 'leaderboard'
// | 'players';

console.log(focused);

const objectives = Object.keys(dictionary119).map((key) => key);
const action = interaction.options.getString('action');

if (!action) return interaction.respond([]);

if (action === 'custom') {
return interaction.respond(mapChoices(customScoreboardObjectives));
} else {
const targetObjectives = objectives
.filter((obj) => obj.startsWith(action))
.map((item) => item.replace(action, ''));

return interaction.respond(mapChoices(targetObjectives));
}
}
^ taken from autocomplete.ts logging focused yields: { value: '', type: 3, name: 'playername', focused: true } when hovering over playername and { value: '', type: 3, name: 'item', focused: true } when hovering over item
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
if (interaction.commandName === 'scoreboard') {
const objectives = Object.keys(dictionary119).map((key) => key);
const action = interaction.options.getString('action');

if (focused.name === 'playername') {
const { host, rconPort, rconPasswd } = config.mcConfig['smp'];

const whitelistNames = await getWhitelist(host, rconPort, rconPasswd);

return interaction.respond(mapChoices(whitelistNames));
} else if (focused.name === 'item') {
if (action === 'custom') {
return interaction.respond(mapChoices(customScoreboardObjectives));
} else {
if (!action) return interaction.respond([]);

const targetObjectives = objectives
.filter((obj) => obj.startsWith(action))
.map((item) => item.replace(action, ''));

return interaction.respond(mapChoices(targetObjectives));
}
}
}
if (interaction.commandName === 'scoreboard') {
const objectives = Object.keys(dictionary119).map((key) => key);
const action = interaction.options.getString('action');

if (focused.name === 'playername') {
const { host, rconPort, rconPasswd } = config.mcConfig['smp'];

const whitelistNames = await getWhitelist(host, rconPort, rconPasswd);

return interaction.respond(mapChoices(whitelistNames));
} else if (focused.name === 'item') {
if (action === 'custom') {
return interaction.respond(mapChoices(customScoreboardObjectives));
} else {
if (!action) return interaction.respond([]);

const targetObjectives = objectives
.filter((obj) => obj.startsWith(action))
.map((item) => item.replace(action, ''));

return interaction.respond(mapChoices(targetObjectives));
}
}
}
i am sorry about this mess but this actually works
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
yea this works
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
xDDDD nah this is insanely unreadable i hate this code
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
but like it has to actually get the whitelist from a minecraft server because i use the whitelisted people for the autocomplete the user experience for people using that command is insane tho. you can get the score of any player (whitelisted: autocomplete) and the scoreboards also autocomplete
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
defnot
defnotOP2y ago
defnot
defnotOP2y ago
it then goes and requests the score via RCON from the server
defnot
defnotOP2y ago
defnot
defnotOP2y ago
or you can also have a leaderboard that gets generated with canvas of the top 15 players
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
defnot
defnotOP2y ago
thanks frogheart
Want results from more Discord servers?
Add your server