Skip to content

Commit

Permalink
Merge pull request #1 from stjohnjohnson/initial-build
Browse files Browse the repository at this point in the history
Initial build of Discord/ExtraLife bot
  • Loading branch information
stjohnjohnson authored Oct 3, 2022
2 parents 3f0df76 + 942ff53 commit 5568c75
Show file tree
Hide file tree
Showing 5 changed files with 2,975 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:18-alpine
WORKDIR /usr/src/app
RUN chown -R node:node /usr/src/app
USER node
COPY --chown=node:node package*.json ./
RUN npm install
COPY app.js .
CMD [ "npm", "start" ]
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# extralife-discord-bridge
Bring your Extra-Life donations directly to your Discord channel

## Running

```bash
$ docker run --rm -it --env-file .env stjohnjohnson/extralife-discord-bridge:latest
> [email protected] 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
```

## Configuration

Set the following environment variables:

- `DISCORD_TOKEN`: Your Bot Token
- `DISCORD_CHANNEL`: Name of the channel to write to
- `EXTRALIFE_PARTICIPANT_ID`: Participant ID from Extra Life

## 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.
76 changes: 76 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { getUserInfo, getUserDonations } from 'extra-life-api';
import dotenv, { config } 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 => {
if (!process.env[key]) {
return `${key} is a required environment variable`;
}
}).join("\n").trim();
if (configErrors != "") {
console.error(configErrors);
process.exit(1);
}

var channel;

var seenDonationIDs = {};
function getLatestDonation(silent = false) {
getUserDonations(process.env.EXTRALIFE_PARTICIPANT_ID).then(data => {
var msgQueue = [];

data.donations.map(donation => {
if (seenDonationIDs[donation.donationID]) {
return;
}
seenDonationIDs[donation.donationID] = true;

const amount = moneyFormatter.format(donation.amount),
displayName = donation.displayName ? donation.displayName : 'Anonymous',
message = donation.message ? ` with the message "${donation.message}"` : '';;

msgQueue.unshift(`${displayName} just donated ${amount}${message}!`);
console.log(`Donation: ${displayName} / ${amount}${message}`);
});

if (!silent) {
msgQueue.forEach(msg => channel.send(msg));
}
}).catch(err => {
console.error("Error getting Donations:", err);
})
}

// Setup a formatter
const moneyFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// When the client is ready, run this code (only once)
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}`);
}
console.log(`Found Channel: ${channel.id}`);

// Check for updates (regularly)
setInterval(getLatestDonation, 30000);
// Be quiet the first time
getLatestDonation(true);
});

// Login to Discord with your client's token
client.login(`${process.env.DISCORD_TOKEN}`);
Loading

0 comments on commit 5568c75

Please sign in to comment.