123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- const CoreClass = require("../core.js");
- const Discord = require("discord.js");
- const config = require("config");
- class DiscordModule extends CoreClass {
- constructor() {
- super("discord");
- }
- initialize() {
- return new Promise((resolve, reject) => {
- this.log("INFO", "Discord initialize");
- this.client = new Discord.Client();
- this.adminAlertChannelId = config.get(
- "apis.discord"
- ).loggingChannel;
- this.client.on("ready", () => {
- this.log("INFO", `Logged in as ${this.client.user.tag}!`);
- if (this.getStatus() === "INITIALIZING") {
- resolve();
- } else if (this.getStatus() === "RECONNECTING") {
- this.log("INFO", `Discord client reconnected.`);
- this.setStatus("READY");
- }
- });
- this.client.on("disconnect", () => {
- this.log("INFO", `Discord client disconnected.`);
- if (this.getStatus() === "INITIALIZING") reject();
- else {
- this.setStatus("DISCONNECTED");
- }
- });
- this.client.on("reconnecting", () => {
- this.log("INFO", `Discord client reconnecting.`);
- this.setStatus("RECONNECTING");
- });
- this.client.on("error", (err) => {
- this.log(
- "INFO",
- `Discord client encountered an error: ${err.message}.`
- );
- });
- this.client.login(config.get("apis.discord").token);
- });
- }
- SEND_ADMIN_ALERT_MESSAGE(payload) {
- return new Promise((resolve, reject) => {
- const channel = this.client.channels.find(
- (channel) => channel.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(payload.color);
- richEmbed.setDescription(payload.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:", payload.type, true);
- richEmbed.addField(
- "Critical:",
- payload.critical ? "True" : "False",
- true
- );
- payload.extraFields.forEach((extraField) => {
- richEmbed.addField(
- extraField.name,
- extraField.value,
- extraField.inline
- );
- });
- channel
- .send(payload.message, { embed: richEmbed })
- .then((message) =>
- resolve({
- status: "success",
- message: `Successfully sent admin alert message: ${message}`,
- })
- )
- .catch(() =>
- reject(new Error("Couldn't send admin alert message"))
- );
- } else {
- reject(new Error("Channel was not found"));
- }
- // if (true) {
- // resolve({});
- // } else {
- // reject(new Error("Nothing changed."));
- // }
- });
- }
- }
- module.exports = new DiscordModule();
|