From 1ea11d073aac6c1e4132e4cae4616732ea9710dc Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sun, 10 Mar 2019 20:08:51 +0100 Subject: [PATCH] Added logging of messages - added new main database table `messages` to log all messages for statistic reasons --- bot.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/bot.js b/bot.js index 88d2927..947af8f 100644 --- a/bot.js +++ b/bot.js @@ -120,6 +120,14 @@ class Bot { new dblib.Column('text', sql.types.getVarchar(255), [sql.constraints.unique, sql.constraints.notNull]) ])); + await this.maindb.run(sql.createTableIfNotExists('messages', [ + sql.templates.idcolumn, + new dblib.Column('server', sql.types.getVarchar(255)), + new dblib.Column('channel', sql.types.getVarchar(255)), + new dblib.Column('username', sql.types.getVarchar(255), [sql.constraints.notNull]), + new dblib.Column('message', sql.types.text), + new dblib.Column('timestamp', sql.types.datetime, [sql.constraints.notNull, sql.default('NOW()')]) + ])); this.logger.debug('Loading Presences...'); await this.loadPresences(); } @@ -232,6 +240,35 @@ 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); + } + }); } /**