Browse Source

restore changes

isundil 6 years ago
parent
commit
c12e46f9e0
1 changed files with 19 additions and 19 deletions
  1. 19 19
      quizz.js

+ 19 - 19
quizz.js

@@ -6,7 +6,7 @@ const
 
 Object.assign(global, require("./config.js"));
 
-const MySQL = USE_MYSQL ? require("mysql2").createConnection({host: MySQL_HOST, user: MySQL_USER, database: MySQL_DB, password: MySQL_PASS}) : null;
+const MySQL = USE_MYSQL ? (function() { return require("mysql2").createConnection({host: MySQL_HOST, user: MySQL_USER, database: MySQL_DB, password: MySQL_PASS}); })  : null;
 
 const HOSTNAME = require('os').hostname(); // For Mysql bot identification
 
@@ -436,7 +436,6 @@ QuizzBot.prototype.onMessageInternal = function(username, user, msg) {
             this.bot.sendMsg(this.room, "Question #" +questionId +" marquée comme restaurée");
         } else if (Cache.isReportedBy(questionId, username)) {
             Cache.unreportQuestion(questionId, username);
-            this.bot.sendMsg(this.room, "Question #" +questionId +" marquée comme restaurée");
             this.bot.sendMsg(this.room, "Question #" +questionId +" n'est plus marquée comme défectueuse par " +username);
         } else {
             this.bot.sendMsg(this.room, "Must be channel operator");
@@ -464,7 +463,7 @@ QuizzBot.prototype.onMessageInternal = function(username, user, msg) {
         else
             this.bot.sendMsg(this.room, "Must be channel operator");
     }
-    else if (lmsg.startsWith("!rename")) {
+    else if (lmsg.startsWith("!rename ")) {
         if (!user.admin) {
             this.bot.sendMsg(this.room, "Must be channel operator");
             return;
@@ -493,7 +492,9 @@ QuizzBot.prototype.onMessageInternal = function(username, user, msg) {
         }
         if (!userToMod) {
             userToMod = this.users[target] = this.bot.createUser(target);
-            this.bot.sendMsg(this.room, "Created user " +target);
+            this.bot.sendMsg(this.room, "Created user " +target +" with " +sum +" points");
+        } else if (sum) {
+            this.bot.sendMsg(this.room, "Added " +sum +" points to " +target);
         }
         userToMod.score += sum;
         Cache.SetScores(this.users);
@@ -537,16 +538,6 @@ QuizzBot.prototype.onMessageInternal = function(username, user, msg) {
     }
 };
 
-
-
-
-
-
-
-
-
-
-
 QuizzBot.prototype.mySQLExportWrapper = function() {
     if (!USE_MYSQL)
         return;
@@ -572,10 +563,14 @@ QuizzBot.prototype.mySQLExport = function() {
 }
 
 QuizzBot.prototype.exportScores = function() {
+    console.log("Start exporting scores");
     return new Promise((ok, ko) => {
+        if (!MySQL)
+            return ko();
         var ts = Cache.getLastMysqlSave() || this.config.START_TIME;
+            mySQL = MySQL();
         ts = Math.floor(ts / 1000) *1000;
-        ts = MySQL.escape(new Date(ts));
+        ts = mySQL.escape(new Date(ts));
         ts = ts.substr(1, ts.length -2);
         var sep = ts.lastIndexOf('.');
         if (sep > 12) ts = ts.substr(0, sep);
@@ -586,12 +581,17 @@ QuizzBot.prototype.exportScores = function() {
                 toSave.push(this.users[i].name);
                 toSave.push(this.users[i].score);
             }
-        if (toSave.length == 0)
+        if (toSave.length == 0) {
+            mySQL.end();
             return ok();
-        MySQL.execute("INSERT INTO " +this.config.MySQL_PERIOD_TABLE +" (start, host) VALUES(?, ?)", [ts, HOSTNAME], (err, result) => {
-            if (err || !result.insertId)
+        }
+        mySQL.execute("INSERT INTO " +this.config.MySQL_PERIOD_TABLE +" (start, host) VALUES(?, ?)", [ts, HOSTNAME], (err, result) => {
+            if (err || !result.insertId) {
+                mySQL.end();
                 return ko(err || "Cannot get last inserted id");
-            MySQL.execute("INSERT INTO " +this.config.MySQL_SCORES_TABLE +"(period_id, pseudo, score) VALUES " +(",("+result.insertId+",?,?)").repeat(toSave.length /2).substr(1), toSave, (err) => {
+            }
+            mySQL.execute("INSERT INTO " +this.config.MySQL_SCORES_TABLE +"(period_id, pseudo, score) VALUES " +(",("+result.insertId+",?,?)").repeat(toSave.length /2).substr(1), toSave, (err) => {
+                mySQL.end();
                 if (err)
                     ko(err);
                 else