Play Audio Resource even when bot isn't in a voice channel

Hi guys, I've created a bot that gets a live feed from a radio station and then outputs the audio into a voice channel (by using a ReadableStream object as the input for createAudioResource()). Whenever there is nobody active in the voice channel, I make the bot leave the channel, and when someone rejoins, the bot also rejoins. Everything works fine except this: when the bot isn't actively in the voice channel, the playback pauses and then resumes at the same point when someone rejoins, rather than playing the latest chunks in the stream. Every second the bot isn't inside the voice channel, its playback basically delays an additional second from being actually live. Is there an easy way I can get around this? I'm not sure if the solution is JS-based or part of the discord.js library. I saw the noSubscriber: 'play' flag in createAudioPlayer() options but this only seems to ontinue playback when the bot is alone in a channel, not when the bot itself isn't in any channel.
3 Replies
d.js toolkit
d.js toolkit6mo 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
duck
duck6mo ago
this would be better suited to #djs-voice that being said, the noSubscriber behavior is what you would want to use since AudioPlayers aren't dependent on VoiceConnections, that should be how you'd achieve this beyond simply stopping playback and resuming when there's a subscribed VoiceConnection the bot being alone in a channel also should still be considered a valid subscribed connection assuming it's in the Ready state, so this shouldn't be affected by the noSubscriber behavior how have you determined that this is how it behaves?
zeki
zeki6mo ago
thanks so much for responding! Reading the docs I think you're right, noSubscriber should work. so I guess the issue is somewhere else, is there something wrong with the way I'm setting the flag?
const src = await axios.get(url, {
responseType: 'stream'
});
const resource = createAudioResource(src.data);
player = createAudioPlayer({ noSubscriber: 'play' });
player.play(resource);

const joinConfig = {
channelId: mainChannel.id,
guildId: mainGuild.id,
adapterCreator: mainGuild.voiceAdapterCreator,
selfDeaf: false
};

const connection = joinVoiceChannel(joinConfig);
connection.subscribe(player);

client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.member.user.bot) return;

if ( // disconnect
oldState.channelId === mainChannel.id &&
mainChannel.members.size < 2 &&
connection.state.status === 'ready'
) return connection.disconnect();

if ( // connect
newState.channelId === mainChannel.id
) {
connection.subscribe(player);
return connection.rejoin(joinConfig);
}

});
const src = await axios.get(url, {
responseType: 'stream'
});
const resource = createAudioResource(src.data);
player = createAudioPlayer({ noSubscriber: 'play' });
player.play(resource);

const joinConfig = {
channelId: mainChannel.id,
guildId: mainGuild.id,
adapterCreator: mainGuild.voiceAdapterCreator,
selfDeaf: false
};

const connection = joinVoiceChannel(joinConfig);
connection.subscribe(player);

client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.member.user.bot) return;

if ( // disconnect
oldState.channelId === mainChannel.id &&
mainChannel.members.size < 2 &&
connection.state.status === 'ready'
) return connection.disconnect();

if ( // connect
newState.channelId === mainChannel.id
) {
connection.subscribe(player);
return connection.rejoin(joinConfig);
}

});
omg that turned out so ugly sorry oh I'm so dumb I need to pass in behaviors as a nested object to make the flag work my bad solved!!