Browse Source

fix(discord): incorrect function declaration causing errors

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 5 years ago
parent
commit
bf7c119a4e
1 changed files with 94 additions and 71 deletions
  1. 94 71
      backend/logic/discord.js

+ 94 - 71
backend/logic/discord.js

@@ -1,8 +1,8 @@
 let lockdown = false;
 
-const Discord = require('discord.js');
-const logger = require('./logger');
-const config = require('config');
+const Discord = require("discord.js");
+const logger = require("./logger");
+const config = require("config");
 
 const client = new Discord.Client();
 
@@ -12,73 +12,96 @@ let connected = false;
 
 // TODO Maybe we need to only finish init when ready is called, or maybe we don't wait for it
 module.exports = {
-    adminAlertChannelId: "",
-
-	init: (discordToken, adminAlertChannelId, errorCb, cb) => {
-        this.adminAlertChannelId = adminAlertChannelId;
-
-        client.on('ready', () => {
-            logger.info("DISCORD_READY", `Logged in as ${client.user.tag}!`);
-            connected = true;
-            messagesToSend.forEach((message) => {
-                this.sendAdminAlertMessage(message);
-            });
-            messagesToSend = [];
-        });
-
-        client.on('invalidated', () => {
-            logger.info("DISCORD_INVALIDATED", `Discord client was invalidated.`);
-            connected = false;
+  adminAlertChannelId: "",
+
+  init: function(discordToken, adminAlertChannelId, errorCb, cb) {
+    this.adminAlertChannelId = adminAlertChannelId;
+
+    client.on("ready", () => {
+      logger.info("DISCORD_READY", `Logged in as ${client.user.tag}!`);
+      connected = true;
+      messagesToSend.forEach(message => {
+        this.sendAdminAlertMessage(message);
+      });
+      messagesToSend = [];
+    });
+
+    client.on("invalidated", () => {
+      logger.info("DISCORD_INVALIDATED", `Discord client was invalidated.`);
+      connected = false;
+    });
+
+    client.on("disconnected", () => {
+      logger.info("DISCORD_DISCONNECTED", `Discord client was disconnected.`);
+      connected = false;
+    });
+
+    client.on("error", err => {
+      logger.info(
+        "DISCORD_ERROR",
+        `Discord client encountered an error: ${err.message}.`
+      );
+    });
+
+    client.login(discordToken);
+
+    if (lockdown) return this._lockdown();
+    cb();
+  },
+
+  sendAdminAlertMessage: function(message, color, type, critical, extraFields) {
+    if (!connected) return messagesToSend.push(message);
+    else {
+      let channel = client.channels.find("id", this.adminAlertChannelId);
+      if (channel !== null) {
+        let richEmbed = new Discord.RichEmbed();
+        richEmbed.setAuthor(
+          "Musare Logger",
+          config.get("domain") + "/favicon-194x194.png",
+          config.get("domain")
+        );
+        richEmbed.setColor(color);
+        richEmbed.setDescription(message);
+        //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
+        //richEmbed.setImage("https://musare.com/favicon-194x194.png");
+        //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
+        richEmbed.setTimestamp(new Date());
+        richEmbed.setTitle("MUSARE ALERT");
+        richEmbed.setURL(config.get("domain"));
+        richEmbed.addField("Type:", type, true);
+        richEmbed.addField("Critical:", critical ? "True" : "False", true);
+        extraFields.forEach(extraField => {
+          richEmbed.addField(
+            extraField.name,
+            extraField.value,
+            extraField.inline
+          );
         });
 
-        client.on('disconnected', () => {
-            logger.info("DISCORD_DISCONNECTED", `Discord client was disconnected.`);
-            connected = false;
-        });
-
-        client.on('error', (err) => {
-            logger.info("DISCORD_ERROR", `Discord client encountered an error: ${err.message}.`);
-        });
-
-        client.login(discordToken);
-
-		if (lockdown) return this._lockdown();
-		cb();
-    },
-    
-    sendAdminAlertMessage: (message, color, type, critical, extraFields) => {
-        if (!connected) return messagesToSend.push(message);
-        else {
-            let channel = client.channels.find("id", this.adminAlertChannelId);
-            if (channel !== null) {
-
-                let richEmbed = new Discord.RichEmbed();
-                richEmbed.setAuthor("Musare Logger", config.get("domain")+"/favicon-194x194.png", config.get("domain"));
-                richEmbed.setColor(color);
-                richEmbed.setDescription(message);
-                //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
-                //richEmbed.setImage("https://musare.com/favicon-194x194.png");
-                //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
-                richEmbed.setTimestamp(new Date());
-                richEmbed.setTitle("MUSARE ALERT");
-                richEmbed.setURL(config.get("domain"));
-                richEmbed.addField("Type:", type, true);
-                richEmbed.addField("Critical:", (critical) ? 'True' : 'False', true);
-                extraFields.forEach((extraField) => {
-                    richEmbed.addField(extraField.name, extraField.value, extraField.inline);
-                });
-
-
-                channel.send(message, { embed: richEmbed })                     
-                .then(message => logger.success("SEND_ADMIN_ALERT_MESSAGE", `Sent admin alert message: ${message}`))
-                .catch(() => logger.error("SEND_ADMIN_ALERT_MESSAGE", "Couldn't send admin alert message"));
-            } else {
-                logger.error("SEND_ADMIN_ALERT_MESSAGE", "Couldn't send admin alert message, channel was not found.");
-            }
-        }
-    },
-
-	_lockdown: () => {
-		lockdown = true;
-	}
-}
+        channel
+          .send(message, { embed: richEmbed })
+          .then(message =>
+            logger.success(
+              "SEND_ADMIN_ALERT_MESSAGE",
+              `Sent admin alert message: ${message}`
+            )
+          )
+          .catch(() =>
+            logger.error(
+              "SEND_ADMIN_ALERT_MESSAGE",
+              "Couldn't send admin alert message"
+            )
+          );
+      } else {
+        logger.error(
+          "SEND_ADMIN_ALERT_MESSAGE",
+          "Couldn't send admin alert message, channel was not found."
+        );
+      }
+    }
+  },
+
+  _lockdown: () => {
+    lockdown = true;
+  }
+};