From ddd33d4851fd0a3e3320327083ad9e395b4cd8a4 Mon Sep 17 00:00:00 2001 From: Mentor Date: Fri, 17 Dec 2021 16:28:33 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20discord=20webhook=20functiona?= =?UTF-8?q?lity=20to=20the=20changing=20room?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/integrations/discord.js | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 functions/integrations/discord.js diff --git a/functions/integrations/discord.js b/functions/integrations/discord.js new file mode 100644 index 0000000..33d3fa4 --- /dev/null +++ b/functions/integrations/discord.js @@ -0,0 +1,33 @@ +const functions = require( 'firebase-functions' ) +const { discord } = functions.config() +const fetch = require( 'isomorphic-fetch' ) + +exports.notifyDiscordWebhook = async function( username, content, avatar_url, image_title, image_url ) { + + try { + + // Construct discord webhook message + const message = { + username, + content, + avatar_url, + embeds: [ { + title: image_title, image: { url: image_url } + } ] + } + + // Construct request options + const options = { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify( message ) + } + + // Make webhook request + await fetch( discord.webhookurl, options ) + + } catch( e ) { + console.error( 'Discord error ', e ) + } + +} \ No newline at end of file