Play command

I was making a play command
await interaction.deferReply();

const search = await ytse(given_song);
const search_res = search.videos[0].url;

const userchannel = interaction.member.voice;
if (!userchannel) {return interaction.followUp('You must be in a voice channel to use this command');}

const audioplayer = createAudioPlayer();

const connection = joinVoiceChannel({
channelId:userchannel.channelId,
guildId:interaction.guildId,
adapterCreator:interaction.guild.voiceAdapterCreator
});

connection.subscribe(audioplayer);

const ytdlprocess = ytdl(search_res, {filter:'audioonly'});

ytdlprocess.on("error",(error) => {console.error(error);});

audioplayer.play(createAudioResource(ytdlprocess));

await interaction.followUp({content:'Song is playing'});

audioplayer.on(AudioPlayerStatus.Idle, (oldState, newState) => {//song ended
connection.destroy();
return interaction.followUp({content:'Song ended. Leaving channel'});
});
await interaction.deferReply();

const search = await ytse(given_song);
const search_res = search.videos[0].url;

const userchannel = interaction.member.voice;
if (!userchannel) {return interaction.followUp('You must be in a voice channel to use this command');}

const audioplayer = createAudioPlayer();

const connection = joinVoiceChannel({
channelId:userchannel.channelId,
guildId:interaction.guildId,
adapterCreator:interaction.guild.voiceAdapterCreator
});

connection.subscribe(audioplayer);

const ytdlprocess = ytdl(search_res, {filter:'audioonly'});

ytdlprocess.on("error",(error) => {console.error(error);});

audioplayer.play(createAudioResource(ytdlprocess));

await interaction.followUp({content:'Song is playing'});

audioplayer.on(AudioPlayerStatus.Idle, (oldState, newState) => {//song ended
connection.destroy();
return interaction.followUp({content:'Song ended. Leaving channel'});
});
But sometimes it joins the channel and says 'Song is playing' but does not play anything Sometimes it does not join the channel only and says 'Song is playing' It does not log any error also
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ShompiFlen
ShompiFlen2y ago
The bot saying Song is Playing does not represent anything here because you are not doing enough checks. You should check if the connection enters the ready state so you are sure its ready to transmit audio. Then your intents, do you have the GuildVoiceStates intent enabled?
Incorecc
IncoreccOP2y ago
omg i am so dumb I did not have intents on thanks should I also add a connection listener
ShompiFlen
ShompiFlen2y ago
maybe for good measure but as long as the connection enter the ready state there shouldnt be a problem

Did you find this page helpful?