-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial build of Discord/ExtraLife bot
- Loading branch information
1 parent
3f0df76
commit 942ff53
Showing
5 changed files
with
2,975 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |
Oops, something went wrong.