From 37a55955da3b2c193439961b947ec64cba7bb186 Mon Sep 17 00:00:00 2001 From: Mentor Date: Thu, 14 Jul 2022 11:19:19 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20discord=20webhook=20notificat?= =?UTF-8?q?ion=20by=20Gretal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/integrations/changingroom.js | 6 +++++- functions/integrations/discord.js | 30 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/functions/integrations/changingroom.js b/functions/integrations/changingroom.js index f8c596a..ce36437 100644 --- a/functions/integrations/changingroom.js +++ b/functions/integrations/changingroom.js @@ -4,6 +4,7 @@ const { dev, log } = require( '../modules/helpers' ) const { ask_signer_is_for_available_emails } = require( './signer_is' ) const { send_outfit_available_email } = require( './ses' ) const { throttle_and_retry } = require( '../modules/helpers' ) +const { notify_discord_of_outfit_notifications } = require( './discord' ) // Web3 APIs const { getOwingAddressOfTokenId } = require( '../modules/contract' ) @@ -214,7 +215,7 @@ exports.notify_holders_of_changing_room_updates = async context => { // Get all Rocketeers with outfits available const network = dev ? `rinkeby` : `mainnet` - const limit = dev ? 5000 : 5000 // max supply 3475 + const limit = dev ? 5 : 5000 // max supply 3475 console.log( `Getting ${ limit } rocketeers on ${ network }` ) let all_rocketeers = await db.collection( `${ network }Rocketeers` ).limit( limit ).get().then( dataFromSnap ) console.log( `Got ${ all_rocketeers.length } Rocketeers` ) @@ -301,6 +302,9 @@ exports.notify_holders_of_changing_room_updates = async context => { // Log result console.log( `Sent ${ owners_to_email.length } emails for ${ network } outfits` ) + // Notify Discord too + await notify_discord_of_outfit_notifications( owners_to_email.length ) + } catch( e ) { console.error( `notify_holders_of_changing_room_updates error: `, e ) diff --git a/functions/integrations/discord.js b/functions/integrations/discord.js index 2d463bf..bf86c01 100644 --- a/functions/integrations/discord.js +++ b/functions/integrations/discord.js @@ -1,6 +1,7 @@ const functions = require( 'firebase-functions' ) const { discord } = functions.config() const fetch = require( 'isomorphic-fetch' ) +const { dev } = require('../modules/helpers') exports.notify_discord_of_new_outfit = async function( username, content, avatar_url, image_title, image_url ) { @@ -34,4 +35,33 @@ exports.notify_discord_of_new_outfit = async function( username, content, avatar console.error( 'Discord error ', e ) } +} + +exports.notify_discord_of_outfit_notifications = async function( amount=0 ) { + + if( !dev && amount == 0 ) return console.log( `Not sending Discord message for 0 updates` ) + + try { + + // Construct discord webhook message + const message = { + username: "Gretal Marchall Alon of Jupiter", + content: `I emailed ${ amount } Rocketeer holders to tell them they have new outfits available in the changing room at https://mint.rocketeer.fans/#/outfits. Want to get email notifications too? Create an email address for your wallet at: https://signer.is/#/email, you'll get a monthly email when your Rocketeers have outfits available.`, + avatar_url: "https://storage.googleapis.com/rocketeer-nft.appspot.com/mainnetRocketeers/1.jpg" + } + + // Construct request options + const options = { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify( message ) + } + + // Make webhook request + const data = await fetch( discord.chatterwebhookurl, options ) + + } catch( e ) { + console.error( 'Discord error ', e ) + } + } \ No newline at end of file