From ae3c53ac6fde18423b4c21e7c9e407c218e44888 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Fri, 27 May 2022 14:48:43 +0200 Subject: [PATCH] Added discord notifier... --- index.js | 2 +- modules/notifiers/discord.js | 28 ++++++++++++++++++++++++++++ modules/notifiers/xmpp.js | 2 +- package.json | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 modules/notifiers/discord.js diff --git a/index.js b/index.js index 8440749..6fd44d3 100644 --- a/index.js +++ b/index.js @@ -10,4 +10,4 @@ log("Starting up notifiers...") const enabledNotifiers = Object.keys(settings.notifiers).filter(notifier => settings.notifiers[notifier].enabled) const notifiers = await Promise.all(enabledNotifiers.map(notifier => getNotifier(notifier))) -console.dir(notifiers) \ No newline at end of file +notifiers.forEach(notifier => notifier("Started up 'maintenance-bot'...")) \ No newline at end of file diff --git a/modules/notifiers/discord.js b/modules/notifiers/discord.js new file mode 100644 index 0000000..0bba26b --- /dev/null +++ b/modules/notifiers/discord.js @@ -0,0 +1,28 @@ +import { Client, Intents } from "discord.js" +import settings from "../../libs/settings.js" +import getLogger from "../../libs/logger.js" + +const log = getLogger("Discord", settings.notifiers.discord.color) + +export async function start() { + log("Starting Discord notifier...") + + const client = new Client({ intents: [ Intents.FLAGS.DIRECT_MESSAGES ]}) + + async function sendMessage(msg) { + const receivers = await Promise.all(settings.notifiers.discord.receivers.map(id => client.users.fetch(id))) + log("Sending messages to:", receivers.map(e => e.tag)) + receivers.forEach(async receiver => { + await receiver.createDM() + await receiver.send(msg) + }) + } + + client.once("ready", () => { + log(`Logged in as ${client.user.username}`) + }) + + await client.login(settings.notifiers.discord["api-token"]) + + return sendMessage +} \ No newline at end of file diff --git a/modules/notifiers/xmpp.js b/modules/notifiers/xmpp.js index 455952b..9899bca 100644 --- a/modules/notifiers/xmpp.js +++ b/modules/notifiers/xmpp.js @@ -19,6 +19,7 @@ export async function start() { async function sendMessage(msg) { const receivers = settings.notifiers.xmpp.receivers + log("Sending messages to:", receivers) const stanzas = receivers.map( address => xml( "message", @@ -36,7 +37,6 @@ export async function start() { xmpp.on("online", address => { log(`Logged in as ${address.toString()}`) - sendMessage("Started up 'maintenance-bot'...") }) await xmpp.start().catch(console.error) diff --git a/package.json b/package.json index c3ea6fc..29fe70f 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@xmpp/client": "^0.13.1", "@xmpp/debug": "^0.13.0", "colors": "^1.4.0", + "discord.js": "^13.7.0", "yaml": "^2.1.0" } }