Broadcast
The Broadcast class makes you able to create easily a broadcast.
Creating a broadcast
You can easily create a broadcast by using the broadcast class. The Broacast class requires the stream
parameter and has an optional options
parameter. 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 discordaudio = require('discordaudio');
const broadcast = new discordaudio.Broadcast(`https://somecoolsite.com/somecoolstream.mp3`, {
noListeners: 'play',
volume: 1,
audiotype: 'arbitrary'
});
Pausing a broadcast
You can easily pause a broadcast by using the pause function.
...
broadcast.pause();
Resuming a broadcast
You can easily resume a broadcast by using the resume function.
...
broadcast.resume();
Destroy a broadcast
You can destroy a broadcast by using the destroy function. You should first unsubscribe the broadcast of all connections before using this function.
...
broadcast.destroy();
Changing the broadcast volume
You can change the volume of the broadcast by using the volume function. The volume has to be at least 0 and can maximum be 1.
...
broadcast.volume(1);
Event listeners
The Broadcast class also has 2 events:
play: When the broadcast starts playing.
end: When the broadcast ended.
Both events return the stream that is playing or has ended.
...
broadcast.on('play', stream => console.log(`Started playing ${stream}`));
broadcast.on('end', stream => console.log(`The stream ${stream} ended`));
Recommended is that if you want to play another stream you first destroy the broadcast and then play a new stream. Otherwise the old resource still will be available and can cause memory leaks.
Playing a broadcast
You can play a broadcast by using the Connection class. The Connection class also has some events which are being used for the Broadcast.
const {Connection, Broadcast} = require('discordaudio');
const discord = require('discord.js');
const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILDS, discord.Intents.FLAGS.GUILD_MESSAGES, discord.Intents.FLAGS.GUILD_VOICE_STATES]});
const config = {
token: 'Your-Secret-Token',
prefix: '-'
};
const broadcast = new Broadcast(`https://somecoolsite.com/somecoolstream.mp3`, {
noListeners: 'play',
volume: 1,
audiotype: 'arbitrary'
});
const connections = new Map();
client.once('ready', () => console.log(`${client.user.username} is online`);
client.on('messageCreate', message => {
if(message.author.bot || message.channel.type === `DM`) return;
if(!message.content.startsWith(config.prefix)) return;
let args = message.content.substring(config.prefix.length).split(" ");
switch(args[0].toLowerCase()){
case 'connect':
if(connections.get(message.guild.id)) return message.channel.send({content: `The bot is already connected to a voice channel!`});
if(!message.member.voice.channel)) return message.channel.send({content: `Please join a voice channel in order to connect the bot to a voice channel`});
const connection = new Connection(message.member.voice.channel, {
selfDeaf: true,
selfMute: false
});
connection.subscribe(broadcast).then(() => {
message.channel.send(`Connected to ${message.member.voice.channel.name}`)
connections.set(message.guild.id, connection);
}).catch(err => {
console.log(err);
message.channel.send({content: `There was an error while playing the broadcast!`});
});
break;
}
});
client.login(config.token);
Last updated