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 "DISCORD NAME" // specify a list of bot owners that can use the owner commands
], ],
"music": { "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 "webservice": { // optional
"enabled": true, // enable the server "enabled": true, // enable the server

@ -306,6 +306,17 @@ class Bot {
logger.debug(err.stack); 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.volume = 0.5;
this.voiceChannel = voiceChannel; this.voiceChannel = voiceChannel;
this.quality = 'lowest'; this.quality = 'lowest';
this.exitTimeout = null;
} }
/** /**
@ -41,7 +42,6 @@ exports.DJ = class {
let connection = await this.voiceChannel.join(); let connection = await this.voiceChannel.join();
logger.info(`Connected to Voicechannel ${this.voiceChannel.name}`); logger.info(`Connected to Voicechannel ${this.voiceChannel.name}`);
this.conn = connection; 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. * Plays a file for the given filename.
* TODO: Implement queue * 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 * Checks if there are still members listening and sets an exit timeout (5 min)
* and exiting when noone is listening. Once this function is executed, it calls itself every 10 seconds (stops when
* not connected). * not connected).
*/ */
checkListeners() { checkListeners() {
if (this.connected && this.conn.channel.members.size === 1) { if (this.exitTimeout) {
logger.verbose(`Set exit timout for ${this.voiceChannel.name}`); clearTimeout(this.exitTimeout);
setTimeout(() => { this.exitTimeout = null;
if (this.voiceChannel && this.voiceChannel.members.size === 1) 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}`); logger.verbose(`Exiting ${this.voiceChannel.name}`);
this.stop(); this.stop();
}, config.music.timeout || 300000); }, 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.current = ({'url': url, 'title': await this.getVideoName(url)});
this.disp = this.conn.playStream(ytdl(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}); {volume: this.volume});
this.disp.on('end', (reason) => { // end event triggers the next song to play when the reason is not stop 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 overflow: hidden
max-height: 100% max-height: 100%
max-width: 100% max-width: 100%
::-webkit-scrollbar ::-webkit-scrollbar
width: 12px width: 12px
height: 12px height: 12px
::-webkit-scrollbar-thumb ::-webkit-scrollbar-thumb
background: darken($cBackground, 5) background: darken($cBackground, 5)
border-radius: 10px border-radius: 10px
::-webkit-scrollbar-track ::-webkit-scrollbar-track
background: lighten($cBackground, 5) background: lighten($cBackground, 5)
border-radius: 10px border-radius: 10px
@ -148,15 +151,18 @@ div.cell > *
border-radius: 5px border-radius: 5px
text-decoration: none text-decoration: none
color: $cPrimary color: $cPrimary
> * > *
display: table-column display: table-column
margin: auto margin: auto
img img
max-height: 30px max-height: 30px
max-width: 20% max-width: 20%
height: auto height: auto
width: auto width: auto
border-radius: 2px border-radius: 2px
a a
width: 80% width: 80%
text-decoration: none text-decoration: none
@ -265,7 +271,7 @@ div.cell > *
align-self: center align-self: center
width: 80% width: 80%
height: auto height: auto
margin: 0 10% margin: 0 10%
border-radius: 5% border-radius: 5%
#guildinfo #guildinfo

Loading…
Cancel
Save