'How can i update the choices in a slashcommand on use? (discord.js)
I'm trying to make a discord command update on use. The idea of the slash command is to be able to delete a card on trello using the command. The slash commands command consists of 2 subcommands (create, delete).
I'm trying to get the command to reload and re-read the json file to add the new list of addChoice options to the subcommand.
This is the code for the command:
const fetch = require('node-fetch');
const fs = require('node:fs');
var lists = JSON.parse(fs.readFileSync('./data/lists.json'))
var cards = JSON.parse(fs.readFileSync(`./data/cards.json`));
const { REST } = require('@discordjs/rest');
const { clientId, guildId, token } = require('../config.json');
const { Routes } = require('discord-api-types/v9');
var commands;
module.exports = {
data: new SlashCommandBuilder()
.setName('card')
.setDescription('Card command!')
.addSubcommand(subcommand =>
subcommand
.setName("create")
.setDescription("Creates a card")
.addStringOption(option => {
option.setName('list')
option.setDescription('Specify the list to add the card to')
option.setRequired(true)
for (let i = 0; i < lists.length; i++) {
option.addChoice(`${lists[i].name}`, `${lists[i].id}`)
}
return option;
})
.addStringOption(option => option.setName("title").setDescription("Specify the title of the card").setRequired(true)))
.addSubcommand(subcommand =>
subcommand
.setName("delete")
.setDescription("Deletes a card")
.addStringOption(option => {
option.setName('list')
option.setDescription('Specify the list to add the card to')
option.setRequired(true)
for (let i = 0; i < cards.length; i++) {
option.addChoice(`${cards[i].name}`, `${cards[i].id}`)
}
return option;
})
),
async execute(interaction, client) {
//CREATE CARD
await (async () => {
if (interaction.options.getSubcommand() === 'create') {
fetch('http://127.0.0.1:3000/trello/createCard', {
method: "POST",
headers: {
'Content-Type': "application/json",
},
body: JSON.stringify({idList: `${interaction.options.getString('list')}`, title: `${interaction.options.getString('title')}`})
}).then(function(res) {
if(res.status !== 200) {
console.log('COULD NOT CREATE CARD')
interaction.reply({content: `ERROR ${res.status}: COULD NOT CREATE CARD`,
ephemeral: true})
} else {
console.log('CARD CREATED')
interaction.reply({content: `CARD CREATED`,
ephemeral: true})
}
})
}
//DELETE CARD
else if (interaction.options.getSubcommand() === 'delete') {
fetch('http://127.0.0.1:3000/trello/deleteCard', {
method: "POST",
headers: {
'Content-Type': "application/json",
},
body: JSON.stringify({cardId: `${interaction.options.getString('list')}`})
}).then(async function(res) {
if(res.status !== 200) {
console.log('COULD NOT DELETE CARD')
interaction.reply({content: `ERROR ${res.status}: COULD NOT DELETE CARD`,
ephemeral: true})
} else {
console.log('CARD DELETED')
interaction.reply({content: `CARD DELETED`,
ephemeral: true})
}
})
}
else {
interaction.reply("ERROR 40: NOT A VALID SUBCOMMAND")
}
})()
},
};
Is there anything i could add to the bot.js maybe to reload the command completely and make the module re-read the json.
Solution 1:[1]
The only thing you can do in this situation is re-registering that slash command with new data.
Or, you can try using new autocomplete interaction option type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | itzJOH_ |
