Changes to lib/music

- changed the listener Checking to event based
- bot updates the VoiceChannel when moved to another one
pull/36/head
Trivernis 6 years ago
parent 9775ab2cdb
commit 13493d8db5

@ -20,7 +20,8 @@ The arguments are optional because the token and youtube-api-key that the bot ne
"DISCORD NAME" // specify a list of bot owners that can use the owner commands
],
"music": {
"timeout": 300000
"timeout": 300000, // exit timeout after noone is left in the voicechannel
"livePuffer": 20000, // the preloaded video length (see ytdl-core module)
},
"webservice": { // optional
"enabled": true, // enable the server

@ -306,6 +306,17 @@ class Bot {
logger.debug(err.stack);
}
});
this.client.on('voiceStateUpdate', async (oldMember, newMember) => {
let gh = await this.getGuildHandler(newMember.guild, prefix);
if (newMember.user === this.client.user) {
if (newMember.voiceChannel)
gh.dj.updateChannel(newMember.voiceChannel);
} else {
if (oldMember.voiceChannel === gh.dj.voiceChannel || newMember.voiceChannel === gh.dj.voiceChannel)
gh.dj.checkListeners();
}
});
}
/**

@ -25,6 +25,7 @@ exports.DJ = class {
this.volume = 0.5;
this.voiceChannel = voiceChannel;
this.quality = 'lowest';
this.exitTimeout = null;
}
/**
@ -41,7 +42,6 @@ exports.DJ = class {
let connection = await this.voiceChannel.join();
logger.info(`Connected to Voicechannel ${this.voiceChannel.name}`);
this.conn = connection;
this.checkListeners();
}
/**
@ -67,6 +67,15 @@ exports.DJ = class {
);
}
/**
* Updates the channel e.g. when the bot is moved to another channel.
* @param voiceChannel
*/
updateChannel(voiceChannel) {
this.voiceChannel = voiceChannel;
logger.debug(`Updated voiceChannel to ${this.voiceChannel.name}`);
}
/**
* Plays a file for the given filename.
* TODO: Implement queue
@ -86,20 +95,22 @@ exports.DJ = class {
}
/**
* Checks if there are still members listening and sets an exit timeout (5 min) before checking again
* and exiting when noone is listening. Once this function is executed, it calls itself every 10 seconds (stops when
* Checks if there are still members listening and sets an exit timeout (5 min)
* not connected).
*/
checkListeners() {
if (this.connected && this.conn.channel.members.size === 1) {
logger.verbose(`Set exit timout for ${this.voiceChannel.name}`);
setTimeout(() => {
if (this.voiceChannel && this.voiceChannel.members.size === 1)
if (this.exitTimeout) {
clearTimeout(this.exitTimeout);
this.exitTimeout = null;
logger.debug(`Cleared exit timout for ${this.voiceChannel.name}`);
}
if (this.connected && this.voiceChannel.members.size === 1) {
logger.debug(`Set exit timout for ${this.voiceChannel.name}`);
this.exitTimeout = setTimeout(() => {
if (this.connected && this.voiceChannel.members.size === 1)
logger.verbose(`Exiting ${this.voiceChannel.name}`);
this.stop();
}, config.music.timeout || 300000);
} else if (this.connected) {
setTimeout(() => this.checkListeners(), 10000);
}
}
@ -147,7 +158,7 @@ exports.DJ = class {
this.current = ({'url': url, 'title': await this.getVideoName(url)});
this.disp = this.conn.playStream(ytdl(url,
{filter: 'audioonly', quality: this.quality, liveBuffer: 40000}),
{filter: 'audioonly', quality: this.quality, liveBuffer: config.music.livePuffer || 20000}),
{volume: this.volume});
this.disp.on('end', (reason) => { // end event triggers the next song to play when the reason is not stop

@ -8,12 +8,15 @@ body
overflow: hidden
max-height: 100%
max-width: 100%
::-webkit-scrollbar
width: 12px
height: 12px
::-webkit-scrollbar-thumb
background: darken($cBackground, 5)
border-radius: 10px
::-webkit-scrollbar-track
background: lighten($cBackground, 5)
border-radius: 10px
@ -148,15 +151,18 @@ div.cell > *
border-radius: 5px
text-decoration: none
color: $cPrimary
> *
display: table-column
margin: auto
img
max-height: 30px
max-width: 20%
height: auto
width: auto
border-radius: 2px
a
width: 80%
text-decoration: none

Loading…
Cancel
Save