From 443570f8d6173d587eba39422e03dd890f93dbb6 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sat, 19 Jan 2019 00:25:24 +0100 Subject: [PATCH] Fixes and Features - fixed guilds first command problem - switched from markdown to rich embed for displaying saved songs/playlists --- lib/cmd.js | 12 ++++----- lib/guilding.js | 72 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index 642165b..844d57a 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -14,7 +14,7 @@ let logger = require('winston'), /** * @type {Servant} */ -module.exports.Servant = class { +exports.Servant = class { constructor(prefix) { this.commands = {}; this.prefix = prefix; @@ -61,7 +61,7 @@ module.exports.Servant = class { */ createCommand(template, call) { if (!template.name) { - logger.debug(`Name of command template is null or undef. Failed creating command.`) + logger.debug(`Name of command template is null or undef. Failed creating command.`); return; } this.commands[this.prefix + template.name] = { @@ -117,7 +117,7 @@ module.exports.Servant = class { * Getting the logger * @param {Object} newLogger */ -module.exports.setLogger = function (newLogger) { +exports.setLogger = function (newLogger) { logger = newLogger; }; @@ -129,7 +129,7 @@ module.exports.setLogger = function (newLogger) { * @param description * @param role */ -module.exports.createGlobalCommand = function (command, call, args, description, role) { +exports.createGlobalCommand = function (command, call, args, description, role) { globCommands[command] = { 'args': args || [], 'description': description, @@ -145,14 +145,14 @@ module.exports.createGlobalCommand = function (command, call, args, description, * @param msg * @returns {boolean|*} */ -module.exports.parseMessage = function (msg) { +exports.parseMessage = function (msg) { return parseGlobalCommand(msg); }; /** * Initializes the module by creating a help command */ -module.exports.init = function (prefix) { +exports.init = function (prefix) { logger.verbose("Created help command"); this.createGlobalCommand(((prefix || config.prefix) + 'help'), (msg) => { let helpstr = "```markdown\n"; diff --git a/lib/guilding.js b/lib/guilding.js index 26f9111..19d6f33 100644 --- a/lib/guilding.js +++ b/lib/guilding.js @@ -24,13 +24,26 @@ exports.GuildHandler = class { this.mention = false; this.prefix = prefix || config.prefix; this.servant = new cmd.Servant(this.prefix); + this.ready = false; + this.msgsQueue = []; + // checking if the data direcotry exists and if the gdb directory exists and creates them if they don't utils.dirExistence('./data', () => { utils.dirExistence('./data/gdb', () => { - this.db = new sqlite3.Database(`./data/gdb/${guild}.db`); - this.createTables(); + this.db = new sqlite3.Database(`./data/gdb/${guild}.db`, (err) => { + if (err) + logger.error(err.message); + logger.debug(`Connected to the database for ${guild}`); + this.createTables(); + // register commands + this.registerMusicCommands(); + this.ready = true; + // handle all messages that have been received while not being ready + for (let i = 0; i < this.msgsQueue.length; i++) { + this.handleMessage(this.msgsQueue.shift()); + } + }); }) }); - this.registerMusicCommands(); } destroy(){ @@ -67,22 +80,26 @@ exports.GuildHandler = class { * @param msg */ handleMessage(msg) { - if (this.db) { - this.db.run( - 'INSERT INTO messages (author, creation_timestamp, author_name, content) values (?, ?, ?, ?)', - [msg.author.id, msg.createdTimestamp, msg.author.username, msg.content], - (err) => { - if (err) - logger.error(err.message); - } - ); - } - let answer = this.servant.parseCommand(msg); - if (!answer) return; - if (this.mention) { - msg.reply(answer); + if (this.ready) { + if (this.db) { + this.db.run( + 'INSERT INTO messages (author, creation_timestamp, author_name, content) values (?, ?, ?, ?)', + [msg.author.id, msg.createdTimestamp, msg.author.username, msg.content], + (err) => { + if (err) + logger.error(err.message); + } + ); + } + let answer = this.servant.parseCommand(msg); + if (!answer) return; + if (this.mention) { + msg.reply(answer); + } else { + msg.channel.send(answer); + } } else { - msg.channel.send(answer); + this.msgsQueue.push(msg); } } @@ -279,18 +296,22 @@ exports.GuildHandler = class { // saved command - prints out saved playlists this.servant.createCommand(servercmd.music.saved, (msg) => { - let response = '```markdown\nSaved Playlists:\n==\n'; + let response = ''; this.db.all('SELECT name, url FROM playlists', (err, rows) => { if (err) logger.error(err.message); for (let row of rows) { - response += `${row.name.padEnd(10, ' ')}: ${row.url} \n\n`; + response += `[${row.name}](${row.url})\n`; } - response += '```'; - if (rows.length === 0) + if (rows.length === 0) { msg.channel.send(servercmd.music.saved.response.no_saved); - else - msg.channel.send(response); + } else { + msg.channel.send('', {embed: { + "title": "Saved Songs and Playlists", + "description": response, + "fields": [] + }}); + } }); }); } @@ -302,7 +323,8 @@ exports.GuildHandler = class { * @returns {GuildHandler} */ exports.getHandler = function (guild, prefix) { - if (!handlers[guild.id]) handlers[guild.id] = new this.GuildHandler(guild, prefix); + if (!handlers[guild.id]) + handlers[guild.id] = new this.GuildHandler(guild, prefix); return handlers[guild.id]; };