Selaa lähdekoodia

Initial Upload

Owen Diffey 8 vuotta sitten
vanhempi
sitoutus
5a62dd6a79
4 muutettua tiedostoa jossa 92 lisäystä ja 0 poistoa
  1. 3 0
      .gitignore
  2. 62 0
      app.js
  3. 7 0
      config/template.json
  4. 20 0
      package.json

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+/node_modules
+/.git
+/config/default.json

+ 62 - 0
app.js

@@ -0,0 +1,62 @@
+const Discord = require("discord.js");
+const client = new Discord.Client();
+const config = require("config");
+const WebHooks = require('node-webhooks');
+const express = require("express");
+const myParser = require("body-parser");
+const fs = require('fs');
+const app = express();
+let currentComponent;
+
+client.on('ready', () => {
+ 	discordClientCBS.forEach((cb) => {
+ 		cb();
+ 	});
+ 	console.log(`DISCORD Logged in as ${client.user.username}!`);
+ });
+
+ client.login(config.get('discord.token'));
+
+ let discordClientCBS = [];
+ const getDiscordClient = (cb) => {
+ 	if (client.status === 0) return cb();
+ 	else discordClientCBS.push(cb);
+ };
+
+ const logToDiscord = (message, color, critical, extraFields, cb = ()=>{}) => {
+ 	getDiscordClient(() => {
+ 		let richEmbed = new Discord.RichEmbed();
+ 		richEmbed.setAuthor("Musare Logger", "https://uptimerobot.com/assets/ico/favicon.ico", "https://status.musare.com");
+        if (critical === true) {
+            richEmbed.setColor("#d9534f");
+        } else if (critical === false) {
+            richEmbed.setColor("#4ca74c");
+        }
+ 		richEmbed.setDescription(message);
+ 	// 	richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
+ 	// 	richEmbed.setImage("https://musare.com/favicon-194x194.png");
+ 		richEmbed.setThumbnail("https://pbs.twimg.com/profile_images/453444308650061824/G22d2Q6n_400x400.png");
+ 		richEmbed.setTimestamp(new Date());
+ 		richEmbed.setTitle("MUSARE ALERT");
+ 		richEmbed.setURL("https://status.musare.com");
+        if(typeof extraFields !== 'undefined' && extraFields) {
+            extraFields.forEach((extraField) => {
+     			richEmbed.addField(extraField.name, extraField.value, extraField.inline);
+     		});
+        }
+ 		client.channels.get(config.get('discord.loggingChannel')).sendEmbed(richEmbed).then(() => {
+ 			cb();
+ 		}).then((reason) => {
+ 			cb(reason);
+ 		});
+ 	});
+ };
+
+app.use(myParser.urlencoded({extended : true}));
+app.post("/", function(request, response) {
+    console.log(request.body);
+    console.log(request.body.alertType);
+    logToDiscord("The backend server started successfully.", "#00AA00", true, [{name: "Name:", value: request.body.monitorFriendlyName, inline: false}, {name: "URL:", value: request.body.monitorURL, inline: false}, {name: "Status:", value: request.body.alertTypeFriendlyName, inline: true}]);
+});
+
+app.listen(3001);

+ 7 - 0
config/template.json

@@ -0,0 +1,7 @@
+{
+    "discord": {
+        "token": "",
+        "loggingChannel": "",
+        "loggingServer": ""
+    }
+}

+ 20 - 0
package.json

@@ -0,0 +1,20 @@
+{
+  "name": "uptimebot",
+  "version": "1.0.0",
+  "description": "",
+  "main": "app.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "Owen Diffey",
+  "license": "MIT",
+  "dependencies": {
+    "body-parser": "^1.17.1",
+    "config": "^1.25.1",
+    "discord.js": "^11.0.0",
+    "express": "^4.15.2",
+    "fs": "0.0.1-security",
+    "http": "0.0.0",
+    "node-webhooks": "^1.1.32"
+  }
+}