diff --git a/CHANGELOG.md b/CHANGELOG.md index 3679780..85216b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - bug on`AnilistApi` where the `.gql` files couldn't be found. - Typo in changelog - bug on `~np` message that causes the player to crash +- database handler using release on pooled client ### Changed - name of MiscCommands module from `TemplateCommandModule` to `MiscCommandModule` @@ -22,7 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - all hard coded sql statements to generic sql generation - MusicPlayer to extend the default EventEmitter - MessageHandler to accept instances of Response and redirect events to it -- switched to `ytdl-core-discord` for youtube audio playback ### Added - Utility classes for generic SQL Statements diff --git a/bot.js b/bot.js index 947af8f..bd63aa0 100644 --- a/bot.js +++ b/bot.js @@ -240,35 +240,6 @@ class Bot { gh.musicPlayer.checkListeners(); } }); - - this.client.on('message', async (msg) => { - try { - let sql = this.maindb.sql; - let server = null; - let channel = null; - let user = msg.author.tag; - let message = msg.content; - if (msg.guild) { - server = msg.guild.name; - channel = msg.channel.name; - await this.maindb.run(sql.insert('messages', { - server: sql.parameter(1), - channel: sql.parameter(2), - username: sql.parameter(3), - message: sql.parameter(4) - }), [server, channel, user, message]); - } else { - await this.maindb.run(sql.insert('messages', { - channel: sql.parameter(1), - username: sql.parameter(2), - message: sql.parameter(3) - }), ['PRIVATE', user, message]); - } - } catch (err) { - this.logger.warn(err.message); - this.logger.debug(err.stack); - } - }); } /** diff --git a/commands/MusicCommands/index.js b/commands/MusicCommands/index.js index a348806..fa6ec77 100644 --- a/commands/MusicCommands/index.js +++ b/commands/MusicCommands/index.js @@ -322,7 +322,6 @@ class MusicCommandModule extends cmdLib.CommandModule { }) ); - /* TODO: Delete completely on release let volume = new cmdLib.Command( this.template.volume, new cmdLib.Answer(async (m, k) => { @@ -336,7 +335,7 @@ class MusicCommandModule extends cmdLib.CommandModule { return this.template.volume.response.invalid; } }) - );*/ + ); let quality = new cmdLib.Command( this.template.quality, @@ -370,6 +369,7 @@ class MusicCommandModule extends cmdLib.CommandModule { .registerCommand(saveMedia) .registerCommand(deleteMedia) .registerCommand(savedMedia) + .registerCommand(volume) .registerCommand(quality); } } diff --git a/lib/database/index.js b/lib/database/index.js index f3e30c2..fc2f088 100644 --- a/lib/database/index.js +++ b/lib/database/index.js @@ -145,7 +145,7 @@ class Database { * Run a sql statement with seperate values and all result rows as return. * @param sql {String} - the sql statement with escaped values ($1, $2... for postgres, ? for sqlite) * @param [values] {Array} - the seperate values - * @returns {Promise} + * @returns {Promise} */ async all(sql, values) { this._logger.debug(`Running SQL "${sql}" with values ${values}`); @@ -166,8 +166,6 @@ class Database { close() { if (this._dbType === 'sqlite') this.database.close(); - else if (this._dbType === 'postgresql') - this.database.release(); } } diff --git a/lib/guilds/index.js b/lib/guilds/index.js index 866846b..cbc771f 100644 --- a/lib/guilds/index.js +++ b/lib/guilds/index.js @@ -1,10 +1,6 @@ const music = require('../music'), - utils = require('../utils'), - config = require('../../config.json'), dblib = require('../database'), - logging = require('../utils/logging'), - fs = require('fs-extra'), - dataDir = config.dataPath || './data'; + logging = require('../utils/logging'); /** * GuildDatabase class has abstraction for some sql statements. @@ -156,6 +152,7 @@ class GuildHandler { */ async applySettings() { this.settings = await this.db.getSettings(); + this.musicPlayer.setVolume(Number(this.settings.musicPlayerVolume) || 0.5); this.musicPlayer.quality = this.settings.musicPlayerQuality || 'lowest'; } diff --git a/lib/music/index.js b/lib/music/index.js index 4cdde4a..9375876 100644 --- a/lib/music/index.js +++ b/lib/music/index.js @@ -1,4 +1,4 @@ -const ytdl = require("ytdl-core-discord"), +const ytdl = require("ytdl-core"), ypi = require('youtube-playlist-info'), yttl = require('get-youtube-title'), config = require('../../config.json'), @@ -24,6 +24,7 @@ class MusicPlayer extends xevents.ExtendedEventEmitter { this.playing = false; this.current = null; this.repeat = false; + this.volume = 0.5; this.voiceChannel = voiceChannel; this.exitTimeout = null; this._logger = new logging.Logger(this); @@ -178,6 +179,7 @@ class MusicPlayer extends xevents.ExtendedEventEmitter { this.queue.push(this.current); let toggleNext = () => { if (this.queue.length > 0) { + this.disp = null; this.current = this.queue.shift(); this.emit('next', this.current); this.playYouTube(this.current.url).catch((err) => this._logger.warn(err.message)); @@ -188,8 +190,8 @@ class MusicPlayer extends xevents.ExtendedEventEmitter { } }; try { - this.disp = this.conn.playOpusStream(await ytdl(url, - {filter: 'audioonly', quality: this.quality, liveBuffer: this.liveBuffer})); + this.disp = this.conn.playStream(await ytdl(url, + {filter: 'audioonly', quality: this.quality, liveBuffer: this.liveBuffer}, {volume: this.volume})); this.disp.on('error', (err) => { this._logger.error(err.message); this._logger.debug(err.stack); @@ -232,6 +234,17 @@ class MusicPlayer extends xevents.ExtendedEventEmitter { }); } + /** + * Sets the volume of the dispatcher to the given value + * @param percentage {Number} + */ + setVolume(percentage) { + this._logger.verbose(`Setting volume to ${percentage}`); + this.volume = percentage; + if (this.disp !== null) + this.disp.setVolume(percentage); + } + /** * Pauses if a dispatcher exists */ @@ -295,7 +308,9 @@ class MusicPlayer extends xevents.ExtendedEventEmitter { skip() { this._logger.debug("Skipping song"); if (this.disp !== null) { - this.disp.end(); + let disp = this.disp; + this.disp = null; + disp.end(); } else { this.playing = false; if (this.queue.length > 0) { diff --git a/package.json b/package.json index 75af553..b667b60 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "compression": "1.7.4", "connect-sqlite3": "0.9.11", "cors": "2.8.5", + "discord.js": "^11.5.1", + "express": "4.16.4", "discord.js": "11.5.1", "express": "4.17.1", "express-compile-sass": "4.0.0", @@ -34,7 +36,7 @@ "winston": "3.2.1", "winston-daily-rotate-file": "4.2.1", "youtube-playlist-info": "1.1.2", - "ytdl-core-discord": "1.0.3" + "ytdl-core": "^1.0.0", }, "devDependencies": { "assert": "2.0.0",