Actualmente estoy codificando un bot de discordia y quería jugar con la nueva función de interacción (también conocida como comandos de barra oblicua).
Logré hacer funcionar una interacción "/test", y el bot recibe el comando y ejecuta mi código (siguiendo el manual de discord.js). Ahora sé por interaction.commandName qué nombre tiene, por ejemplo, el comando (en este caso, "prueba"), pero parece que no puedo obtener los argumentos que se pasan.
El siguiente es mi "test.js", que es para el comando "/test":
const { SlashCommandBuilder } = require('@discordjs/builders'); module.exports = { data: new SlashCommandBuilder() .setName('test') .setDescription('Test with arguments!') .addStringOption(option => option.setName("device") .setDescription("Some argument") .setRequired(true) .addChoice("Arg A", "A") .addChoice("Arg B", "B") ), async execute(interaction, log) { // log is a function to log to a file... // Ignore it, since it is not *currently* in use. return interaction.reply("You selected the argument ????") // Delete message after 5 seconds .then(setTimeout(() => interaction.deleteReply(), 5000)); }, };El comando en sí es llamado por una función que también proviene de la documentación de discord.js:
client.on('interactionCreate', async(interaction) => { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction, log); } catch (error) { console.error(error); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } });Revisé la documentación y busqué un poco en línea, pero parece que no encuentro nada. Sería genial si alguien pudiese ayudarme.
Advertencia: no probé este código CommandInteraction s tiene una propiedad de options . Puedes usar el método get en él.
//the option name is 'TEST' and the user inputted 'hey' let option = interaction.options.get("TEST") console.log(option.value) .value muestra la entrada como se muestra aquí . Puede estar vacío si no hay entrada (no se requirió la opción y el usuario no ingresó esa opción)