Skip to content

Commit

Permalink
Merge pull request #3 from stjohnjohnson/status-update
Browse files Browse the repository at this point in the history
Adding support for updating channel titles with summary
  • Loading branch information
stjohnjohnson authored Oct 5, 2022
2 parents 5568c75 + 826383d commit 92baca9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,36 @@ 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

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.
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.
46 changes: 35 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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';

// Load config from disk
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`;
}
Expand All @@ -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 => {
Expand All @@ -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
Expand All @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -25,8 +25,5 @@
"dotenv": "^16.0.3",
"extra-life-api": "^7.1.0",
"log-timestamp": "^0.3.0"
},
"devDependencies": {
"eslint": "^8.24.0"
}
}

0 comments on commit 92baca9

Please sign in to comment.