Connection

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)

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});

Play a stream

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.

  • volume: The volume of the stream. Max 1, min 0.

  • audiotype: The audio type of the stream.

    • 'arbitrary': For mp3 audio (default)

    • 'ogg/opus': For ogg audio

    • 'webm/opus': For webm audio

    • 'opus': For opus audio

    • 'raw': For raw audio

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});

connection.play(`https://somecoolsite.com/somecoolstream.mp3`, {
    noListeners: 'play',
    volume: 1,
    audiotype: 'arbitrary'
}).then(stream => console.log(`Playing ${stream}`)).catch(console.log);

Subscribe to a broadcast

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.

const {Connection, Broadcast} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});

const broadcast = new Broadcast(`https://somecoolsite.com/somecoolstream.mp3`, {
    noListeners: 'play',
    volume: 1,
    audiotype: 'arbitrary'
});

connection.subscribe(broadcast).then(() => console.log(`Playing the broadcast!`).catch(console.log);

Unsubscribe to a broadcast

You can easily unsubscribe by using the unsubscribe function.

const {Connection, Broadcast} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.unsubcribe();

Changing the volume

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.

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.volume(1);

This function does not work for broadcasts! Broadcasts have their own volume function.

Disconnect from a connection

You can disconnect from a connection by using the disconnect function.

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.disconnect();

If the connection is subscribed to a broadcast the disconnect function will automatically unsubscribe from the broadcast.

Destroy a connection

You can destroy a connection by using the destroy function.

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.destroy();

If the connection is subscribed to a broadcast, the destroy function will automatically unsubscribe from the broadcast.

Pause a stream

You can pause a stream by using the pause function.

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.pause();

This function does not work for broadcasts! Broadcasts have their own pause function.

Resuming a stream

You can resume a stream by using the resume function.

const {Connection} = require('discordaudio');
const discord = require('discord.js');

const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS]});

const channel = client.channels.cache.get('0123456789');

const connection = new Connection(channel, {
    selfDeaf: true,
    selfMute: false
});
...
connection.resume();

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`));

Last updated