add discord webhook notification by Gretal

This commit is contained in:
Mentor 2022-07-14 11:19:19 +02:00
parent 5cb4511dc9
commit 37a55955da
2 changed files with 35 additions and 1 deletions

View File

@ -4,6 +4,7 @@ const { dev, log } = require( '../modules/helpers' )
const { ask_signer_is_for_available_emails } = require( './signer_is' ) const { ask_signer_is_for_available_emails } = require( './signer_is' )
const { send_outfit_available_email } = require( './ses' ) const { send_outfit_available_email } = require( './ses' )
const { throttle_and_retry } = require( '../modules/helpers' ) const { throttle_and_retry } = require( '../modules/helpers' )
const { notify_discord_of_outfit_notifications } = require( './discord' )
// Web3 APIs // Web3 APIs
const { getOwingAddressOfTokenId } = require( '../modules/contract' ) const { getOwingAddressOfTokenId } = require( '../modules/contract' )
@ -214,7 +215,7 @@ exports.notify_holders_of_changing_room_updates = async context => {
// Get all Rocketeers with outfits available // Get all Rocketeers with outfits available
const network = dev ? `rinkeby` : `mainnet` 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 }` ) console.log( `Getting ${ limit } rocketeers on ${ network }` )
let all_rocketeers = await db.collection( `${ network }Rocketeers` ).limit( limit ).get().then( dataFromSnap ) let all_rocketeers = await db.collection( `${ network }Rocketeers` ).limit( limit ).get().then( dataFromSnap )
console.log( `Got ${ all_rocketeers.length } Rocketeers` ) console.log( `Got ${ all_rocketeers.length } Rocketeers` )
@ -301,6 +302,9 @@ exports.notify_holders_of_changing_room_updates = async context => {
// Log result // Log result
console.log( `Sent ${ owners_to_email.length } emails for ${ network } outfits` ) 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 ) { } catch( e ) {
console.error( `notify_holders_of_changing_room_updates error: `, e ) console.error( `notify_holders_of_changing_room_updates error: `, e )

View File

@ -1,6 +1,7 @@
const functions = require( 'firebase-functions' ) const functions = require( 'firebase-functions' )
const { discord } = functions.config() const { discord } = functions.config()
const fetch = require( 'isomorphic-fetch' ) 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 ) { exports.notify_discord_of_new_outfit = async function( username, content, avatar_url, image_title, image_url ) {
@ -35,3 +36,32 @@ exports.notify_discord_of_new_outfit = async function( username, content, avatar
} }
} }
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 )
}
}