Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import fetch from 'node-fetch';
import * as fs from "fs";
import {ColorResolvable, DiscordAPIError, MessageEmbed, WebhookClient} from "discord.js";
import {time} from "@discordjs/builders";
import { ColorResolvable, DiscordAPIError, EmbedBuilder, WebhookClient } from "discord.js";
import { time } from "@discordjs/builders";
import * as config from "./config.json";
const _ = require("lodash");
import _ from "lodash";

const apiUrl = "https://discordstatus.com/api/v2/incidents.json";
const cacheFileName = "./messages.json";

const ignoreDays = config["ignoreDays"]??30;
const ignoreDays = config["ignoreDays"] ?? 30;
const ignoreTime = ignoreDays * 86400000;
console.log(`Ignoring incidents from ${ignoreDays} days ago (${ignoreTime} ms).`);

const webhookClient = new WebhookClient({url: config.url});
const webhookClient = new WebhookClient({ url: config.url });

/**
* Checks if a message exists for the given incident. If so, the message will be updated, if there are new updates to
Expand Down Expand Up @@ -71,15 +71,15 @@ async function checkIncident(incident: any) {
/**
* Creates a new EmbedMessage containing the information about the given incident.
* @param incident
* @return {MessageEmbed} - the newly constructed EmbedMessage
* @return {EmbedBuilder} - the newly constructed EmbedMessage
*/
function buildIncidentEmbed(incident: any) : MessageEmbed {
const embed = new MessageEmbed()
function buildIncidentEmbed(incident: any): EmbedBuilder {
const embed = new EmbedBuilder()
.setTitle(incident.name)
.setURL(incident.shortlink)
.setColor(getStatusColor(incident.status))
.setFooter(incident.id)
.setTimestamp(incident.updated_at);
.setFooter({ text: incident.id, iconURL: 'https://i.imgur.com/AfFp7pu.png' })
.setTimestamp(new Date(incident.updated_at));

// collect affected components
let components = [];
Expand All @@ -88,15 +88,14 @@ function buildIncidentEmbed(incident: any) : MessageEmbed {
components.push(component.name);
}

embed.setDescription(` Impact: ${incident.impact}\n Affected Components: ${components.join(", ")}`);
embed.setDescription(` Impact: ${incident.impact}\n Affected Components: ${components.join(", ")}`);

// collect incident updates
for (let i in incident.incident_updates) {
for (let i in incident.incident_updates.reverse()) {
let update = incident.incident_updates[i];
let timeString = " (" + time(new Date(update.created_at), "R") + ")";
embed.addField(_.startCase(update.status) + timeString, update.body, false);
embed.addFields({name: _.startCase(update.status) + timeString,value: update.body, inline:false});
}
embed.fields.reverse();
return embed;
}

Expand Down Expand Up @@ -131,11 +130,12 @@ async function updateMessage(message, incident: any) {
* Runs the checks for updated incidents.
*/
async function start() {
let obj = await fetchIncidents();
let obj: any = await fetchIncidents();

let incidents = obj.incidents.reverse();
for (let i in incidents) {
let incident = incidents[i];
//console.log(incident)
try {
await checkIncident(incident);
} catch (e) {
Expand Down Expand Up @@ -194,7 +194,7 @@ async function fetchIncidents() {
* @param {String} status - The status of an incident
* @return {ColorResolvable} color - The color corresponding to the given status
*/
function getStatusColor(status: string) : ColorResolvable {
function getStatusColor(status: string): ColorResolvable {
switch (status) {
case "resolved": return "#06a51b";
case "monitoring": return "#a3a506";
Expand All @@ -204,4 +204,4 @@ function getStatusColor(status: string) : ColorResolvable {
}

// check if the message cache file exists, then start the program
checkFile().then(() => start().then(() => console.log("Done.")).catch(console.error)).catch(console.error);
checkFile().then(() => start().then(() => console.log("Done.")).catch(console.error)).catch(console.error);
31 changes: 11 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
{
"name": "discord-api-status-webhook",
"version": "1.0.0",
"version": "1.1.0",
"description": "Discord Webhook to display the discord api's status.",
"keywords": [],
"license": "ISC",
"author": "Laennart & Ariikitty",
"type": "commonjs",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev --respawn --transpile-only index.ts",
"build": "tsc",
"start": "tsc && node ./index.js"
},
"author": "Laennart",
"license": "ISC",
"devDependencies": {
"@types/glob": "^7.1.3",
"@types/luxon": "^2.0.3",
"@types/node": "^14.14.10",
"eslint": "^7.32.0",
"ts-node-dev": "^1.0.0-pre.49",
"tslib": "^2.0.3",
"tslint-config-fire": "^1.0.1",
"typescript": "^4.1.2"
},
"dependencies": {
"@discordjs/builders": "^0.6.0",
"@types/common-tags": "^1.8.0",
"common-tags": "^1.8.0",
"discord-api-types": "^0.22.0",
"discord.js": "^13.1.0",
"glob": "^7.1.6",
"@discordjs/builders": "^1.11.2",
"@types/lodash": "^4.17.20",
"@types/node": "^24.2.0",
"discord.js": "^14.21.0",
"lodash": "^4.17.21",
"node-fetch": "^2.6.1"
"node-fetch": "^3.3.2",
"source-map-support": "^0.5.21"
}
}
7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true
"esModuleInterop": true,
"types": [ "node" ],
"target": "ESNext",
"module": "CommonJS"
}
}
}