From 826383d8ed5e2d7a14f53754fff123d61c30b53e Mon Sep 17 00:00:00 2001 From: "St. John Johnson" Date: Wed, 5 Oct 2022 10:06:08 -0700 Subject: [PATCH] Adding support for updating channel titles with summary Note: this is a breaking change because now I ask for ID instead of Name Fixing #2 --- README.md | 26 ++++++++++++++++++++------ app.js | 46 +++++++++++++++++++++++++++++++++++----------- package.json | 5 +---- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c1e5051..711dc1a 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,13 @@ Bring your Extra-Life donations directly to your Discord channel ```bash $ docker run --rm -it --env-file .env stjohnjohnson/extralife-discord-bridge:latest -> extralife-discord-bridge@1.0.0 start +> extralife-discord-bridge@2.0.0 start > node app.js -[2022-10-03T07:00:42.180Z] Bot Online -[2022-10-03T07:00:42.181Z] Found Channel: 1026381181073240134 -[2022-10-03T07:00:42.594Z] Donation: St. John Johnson / $31.00 +[2022-10-05T06:22:08.636Z] Bot Online +[2022-10-05T06:22:08.637Z] Found Donation Channel: 1026381181073240134 +[2022-10-05T06:22:08.638Z] Found Summary Channel: 1026899238338179182 +[2022-10-05T06:22:42.594Z] Donation: St. John Johnson / $31.00 ``` ## Configuration @@ -18,9 +19,22 @@ $ docker run --rm -it --env-file .env stjohnjohnson/extralife-discord-bridge:lat Set the following environment variables: - `DISCORD_TOKEN`: Your Bot Token -- `DISCORD_CHANNEL`: Name of the channel to write to +- `DISCORD_DONATION_CHANNEL`: ID of the channel to write donations to +- `DISCORD_SUMMARY_CHANNEL`: ID of the channel to update the title of - `EXTRALIFE_PARTICIPANT_ID`: Participant ID from Extra Life +### Discord Bot + +The Discord bot requires `Send Messages` and `Manage Channel` permissions. + ## Features -Every 30s the bot checks ExtraLife to see if there are any donations for that participant. If so, it posts them in the channel you specified. \ No newline at end of file +Every 30s the bot checks ExtraLife to see if there are any donations for that participant. + +### Donation Stream + +If there are new donations, it posts them in the channel you specified. + +### Summary Channel + +After each donation, the bot also updates the specified channel title to show the total & percent raised. \ No newline at end of file diff --git a/app.js b/app.js index 01926c9..0a2e230 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,5 @@ import { getUserInfo, getUserDonations } from 'extra-life-api'; -import dotenv, { config } from 'dotenv'; +import dotenv from 'dotenv'; import { Client, GatewayIntentBits } from 'discord.js'; import 'log-timestamp'; @@ -7,7 +7,12 @@ import 'log-timestamp'; dotenv.config(); // Validate we have all the required variables -const configErrors = ['DISCORD_CHANNEL', 'DISCORD_TOKEN', 'EXTRALIFE_PARTICIPANT_ID'].map(key => { +const configErrors = [ + 'DISCORD_SUMMARY_CHANNEL', // Discord channel ID for updating the title + 'DISCORD_DONATION_CHANNEL', // Discord channel ID for listing donations + 'DISCORD_TOKEN', // Bot token to talk to Discord + 'EXTRALIFE_PARTICIPANT_ID' // Participant ID for Extra Life +].map(key => { if (!process.env[key]) { return `${key} is a required environment variable`; } @@ -17,8 +22,9 @@ if (configErrors != "") { process.exit(1); } -var channel; +var donationChannel, summaryChannel; +// Track all "seen" donations var seenDonationIDs = {}; function getLatestDonation(silent = false) { getUserDonations(process.env.EXTRALIFE_PARTICIPANT_ID).then(data => { @@ -38,12 +44,24 @@ function getLatestDonation(silent = false) { console.log(`Donation: ${displayName} / ${amount}${message}`); }); - if (!silent) { - msgQueue.forEach(msg => channel.send(msg)); + if (msgQueue.length > 0) { + if (!silent) { + msgQueue.forEach(msg => donationChannel.send(msg)); + } + + // Update summary + getUserInfo(process.env.EXTRALIFE_PARTICIPANT_ID).then(data => { + const sumDonations = moneyFormatter.format(data.sumDonations), + percentComplete = Math.round(data.sumDonations / data.fundraisingGoal * 100), + summary = `${sumDonations} (${percentComplete}%) Raised`; + + console.log(`Updating status: "${summary}"`) + return summaryChannel.setName(summary); + }); } }).catch(err => { console.error("Error getting Donations:", err); - }) + }); } // Setup a formatter @@ -59,12 +77,18 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.once('ready', () => { console.log('Bot Online'); - // Find our channel - channel = client.channels.cache.find(channel => channel.name === `${process.env.DISCORD_CHANNEL}`); - if (!channel) { - throw new Error(`Unable to find channel with name ${process.env.DISCORD_CHANNEL}`); + // Find our channels + donationChannel = client.channels.cache.get(`${process.env.DISCORD_DONATION_CHANNEL}`); + if (!donationChannel) { + throw new Error(`Unable to find donation channel with id ${process.env.DISCORD_DONATION_CHANNEL}`); + } + console.log(`Found Donation Channel: ${donationChannel.id}`); + + summaryChannel = client.channels.cache.get(`${process.env.DISCORD_SUMMARY_CHANNEL}`); + if (!summaryChannel) { + throw new Error(`Unable to find summary channel with id ${process.env.DISCORD_SUMMARY_CHANNEL}`); } - console.log(`Found Channel: ${channel.id}`); + console.log(`Found Summary Channel: ${summaryChannel.id}`); // Check for updates (regularly) setInterval(getLatestDonation, 30000); diff --git a/package.json b/package.json index c1feebf..841ea8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "extralife-discord-bridge", - "version": "1.0.0", + "version": "2.0.0", "description": "Post Extra-Life donations to your Discord channels", "main": "app.js", "type": "module", @@ -25,8 +25,5 @@ "dotenv": "^16.0.3", "extra-life-api": "^7.1.0", "log-timestamp": "^0.3.0" - }, - "devDependencies": { - "eslint": "^8.24.0" } }