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:
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.")
}
4 Replies
d.js toolkit
d.js toolkit12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Cryingfreeman74
Cryingfreeman74OP12mo ago
In the discrod chat, the second time I use the command, it responds with not found, and In the console it doesn't log the "subscription cancelled" message, so I thinkth problem might be in the "if already playing" if.
duck
duck12mo ago
while the actual resource you're creating in this code doesn't appear to necessarily be from youtube, your project's name seems to indicate you intend to stream from youtube at some point this is your reminder that streaming from youtube to discord is explicitly against youtube's tos and by extension, discord's tos we do not support tos violations
Cryingfreeman74
Cryingfreeman74OP12mo ago
I'm trying to make it play a track that I download, it could be downloaded from youtubeb or another platform, but the problem is that It doesn't start playing the new downloaded track once I use the command and instead it waits for about 20 seconds
Want results from more Discord servers?
Add your server