the autocomplete works fine but when im trying to execute the command it stops and idk why.. even the conditional i think the problem is in the autocomplete maybe i can be on a loop but idk how to handle that
my index is fine, it gives no error it just stops when i press enter to execute the command.
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel, VoiceConnectionStatus } = require('@discordjs/voice');
const SpotifyWebApi = require('spotify-web-api-node');
const spotifyApi = new SpotifyWebApi({
clientId: process.env.CLIENT_IDP,
clientSecret: process.env.CLIENT_SECRET
});
module.exports = {
data: new SlashCommandBuilder()
.setName('play')
.setDescription('Makes the bot say the provided text.')
.addStringOption(option =>
option.setName('cancion')
.setDescription('The text you want the bot to say.')
.setRequired(true)
.setAutocomplete(true)),
async autocomplete(interaction) {
try {
const focusedValue = interaction.options.getFocused();
// Verificar si el valor enfocado está vacío o no definido
if (!focusedValue) {
return;
}
const credenciales = await spotifyApi.clientCredentialsGrant();
spotifyApi.setAccessToken(credenciales.body.access_token);
let choices = [];
// Buscar canciones en Spotify basadas en el texto enfocado
const searchResults = await spotifyApi.searchTracks(focusedValue, { limit: 15 });
choices = searchResults.body.tracks.items.map(track => ({ name: track.name, value: track.uri }));
await interaction.respond(choices);
} catch (error) {
console.error("Error al realizar autocompletado de Spotify:", error);
// Manejar el error
}
},
async execute(interaction) {
const voiceChannel = interaction.member.voice.channel;
if (voiceChannel) {
await interaction.reply("name song");
try {
// Unirse al canal de voz
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator
});
connection.on('stateChange', (_, newState) => {
if (newState.status === VoiceConnectionStatus.Disconnected) {
// Manejar la desconexión del canal de voz
}
});
// Desconectar después de 5 segundos
setTimeout(() => {
connection.destroy();
}, 5000);
} catch (error) {
console.error("Error al unirse al canal de voz:", error);
await interaction.followUp({ content: "Hubo un error al unirse al canal de voz.", ephemeral: true });
}
} else {
await interaction.reply({ content: "El usuario no está en un canal de voz.", ephemeral: true });
}
},
};