Bot doesn't stop playing if I try to change what it's playing
I use a slash command to obtain a keyword to search on youtube and it works, but if I try to use the command a second time it downloads the new audio but does not change to playing it immediatly, instead it continues playing the old track for around 10-20 seconds and then starts playing the new song.
Code:
Code:
try {
if (!interaction.options.getString("keyword")) interaction.reply("No prompt given");
await search(interaction.options.getString("keyword"));
//if already playing
if(activeSubscription){
subscription.unsubscribe();
player.stop();
console.log("subscription cancelled")
activeSubscription = false;
};
//getting guild, member and voice channel info
const guild = interaction.client.guilds.cache.get(interaction.guildId);
const member = guild.members.cache.get(interaction.member.user.id);
var voiceChannel = member.voice.channel;
interaction.reply('playing ' + videoLink);
const connection = joinVoiceChannel({ //joins the voice chat
channelId: voiceChannel.id,
guildId: guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
//create audio player
const player = createAudioPlayer();
player.on(AudioPlayerStatus.Playing, () => {
console.log('The audio player has started playing!');
});
player.on('error', error => {
console.error(`Error: ${error.message} with resource`);
});
//create and play audio
const resource = createAudioResource('C:\\Users\\Marco\\Documents\\visual studio projects\\youtube_bot\\audio.mp3');
const subscription = connection.subscribe(player);
activeSubscription = true;
player.play(resource);
} catch (error) {
interaction.reply("Not found.")
} try {
if (!interaction.options.getString("keyword")) interaction.reply("No prompt given");
await search(interaction.options.getString("keyword"));
//if already playing
if(activeSubscription){
subscription.unsubscribe();
player.stop();
console.log("subscription cancelled")
activeSubscription = false;
};
//getting guild, member and voice channel info
const guild = interaction.client.guilds.cache.get(interaction.guildId);
const member = guild.members.cache.get(interaction.member.user.id);
var voiceChannel = member.voice.channel;
interaction.reply('playing ' + videoLink);
const connection = joinVoiceChannel({ //joins the voice chat
channelId: voiceChannel.id,
guildId: guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
//create audio player
const player = createAudioPlayer();
player.on(AudioPlayerStatus.Playing, () => {
console.log('The audio player has started playing!');
});
player.on('error', error => {
console.error(`Error: ${error.message} with resource`);
});
//create and play audio
const resource = createAudioResource('C:\\Users\\Marco\\Documents\\visual studio projects\\youtube_bot\\audio.mp3');
const subscription = connection.subscribe(player);
activeSubscription = true;
player.play(resource);
} catch (error) {
interaction.reply("Not found.")
}