The Connection class makes you able to play a stream in a voice channel.
Creating a connection
A connection can easily be created by using the Connection class. The Connection class works different than the Player and the AudioManager class. The Connection class will make the bot join the voice channel immediately when the Connection class is being called. The Player and AudioManager classes make you join a voice channel when you start playing a song.
The Connection class requires the channel parameter and has an optional options parameter. The channel parameter is the voice channel that the bot needs to join. The options parameter has the following options:
selfDeaf: If the bot should deaf itself. (default is true)
selfMute: If the bot should mute itself. (default is false)
You can play a stream by using the play function and returns a Promise. The function requires the stream parameter and has an optional options parameter. The stream parameter is the stream that you want to play. The options parameter has the following options:
noListeners: If there are no members listening in the voice channel.
play: Continues playing the stream. (default and recommended)
pause: Pauses the stream until someone joins again.
stop: Stops the player and leaves the voice channel.
You can subscribe to a Broadcast by using the subscribe function. A Broadcast makes you able to create one source and play it in multiple guilds. The subscribe function only requires the broadcast parameter which is the Broadcast itself which you can create with the Broadcast class. The function returns a Promise which will be fullfilled if the connection successfully subscribed to the broadcast.
You can change the volume by using the volume function. It requires the volume parameter which will be the volume of the stream that's playing. The volume must be at least 0 and may not be higher than 1.
This function does not work for broadcasts! Broadcasts have their own resume function.
Event listeners
The Connection class also has 6 events:
play: When the stream of the connection starts playing.
Returns the stream that plays (not for broadcasts)
end: When the stream of the connection ended.
Returns the stream that ended (not for broadcasts)
disconnect: When the bot got disconnected from the connection.
Returns the channel id
destroy: When the connection got destroyed.
Returns the channel id
subscribe: When the connection got subscribed to a broadcast.
unsubscribe: When the connection got unsubscribed from a broadcast.
...
connection.on('play', stream => {
if(stream) console.log(`Started playing ${stream}`);
else console.log(`Started playing a broadcast`);
});
connection.on('end', stream => {
if(stream) console.log(`The stream ${stream} ended`);
else console.log(`The broadcast ended`);
});
connection.on('disconnect', channelid => console.log(`Disconnected from the channel with the id ${channelid}`));
connection.on('destroy', channelid => console.log(`Destroyed the connection with the channel witht the id ${channelid}`));
connection.on('subscribe', () => console.log(`Subscribed to a broadcast`));
connection.on('unsubscribe', () => console.log(`Unsubscribed to a broadcast`));