Discord revamp! Changed from bot to webhook, removed discord.js dep

This commit is contained in:
TheGuyDanish
2021-07-12 14:13:36 +01:00
parent a6e16116f2
commit 613c42b6d8
4 changed files with 42 additions and 94 deletions

View File

@@ -2,7 +2,6 @@ const axios = require("axios");
const {R} = require("redbean-node");
const FormData = require('form-data');
const nodemailer = require("nodemailer");
const Discord = require('discord.js');
class Notification {
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
@@ -54,8 +53,45 @@ class Notification {
return await Notification.smtp(notification, msg)
} else if (notification.type === "discord") {
return await Notification.discord(notification, msg)
try {
// If heartbeatJSON is null, assume we're testing.
if(heartbeatJSON == null) {
let data = {
username: 'Uptime-Kuma',
content: msg
}
let res = await axios.post(notification.discordWebhookUrl, data)
return true;
}
// If heartbeatJSON is not null, we go into the normal alerting loop.
if(heartbeatJSON['status'] == 0) {
var alertColor = "16711680";
} else if(heartbeatJSON['status'] == 1) {
var alertColor = "65280";
}
let data = {
username: 'Uptime-Kuma',
embeds: [{
title: "Uptime-Kuma Alert",
color: alertColor,
fields: [
{
name: "Time (UTC)",
value: heartbeatJSON["time"]
},
{
name: "Message",
value: msg
}
]
}]
}
let res = await axios.post(notification.discordWebhookUrl, data)
return true;
} catch(error) {
console.log(error)
return false;
}
} else {
throw new Error("Notification type is not supported")
}
@@ -119,18 +155,6 @@ class Notification {
return true;
}
static async discord(notification, msg) {
const client = new Discord.Client();
await client.login(notification.discordToken)
const channel = await client.channels.fetch(notification.discordChannelID);
await channel.send(msg);
client.destroy()
return true;
}
}
module.exports = {