-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
90 lines (76 loc) · 2.85 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { getUserInfo, getUserDonations } from 'extra-life-api';
import dotenv, { config } from 'dotenv';
import tmi from 'tmi.js';
import 'log-timestamp';
// Load config from disk
dotenv.config();
// Validate we have all the required variables
const configErrors = ['TWITCH_CHANNEL', 'TWITCH_USERNAME', 'TWITCH_OAUTH', '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 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(`ExtraLife ExtraLife ${displayName} just donated ${amount}${message}! ExtraLife ExtraLife`);
});
if (!silent) {
msgQueue.forEach(msg => client.say(process.env.TWITCH_CHANNEL, msg));
}
}).catch(err => {
console.error("Error getting Donations:", err);
})
}
setInterval(getLatestDonation, 30000);
// Be quiet the first time
getLatestDonation(true);
// Setup a formatter
const moneyFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
// Create the client
const client = new tmi.Client({
options: { debug: true, messagesLogLevel: "info" },
connection: {
reconnect: true,
secure: true
},
identity: {
username: `${process.env.TWITCH_USERNAME}`,
// Don't forget to use https://twitchapps.com/tmi/ to get this value
password: `${process.env.TWITCH_OAUTH}`
},
channels: [`${process.env.TWITCH_CHANNEL}`]
});
// Connect!
client.connect().catch(console.error);
// Listen for messages
client.on('message', (channel, tags, message, self) => {
// Ignore self
if (self) return;
switch (message.toLowerCase()) {
case '!goal':
getUserInfo(process.env.EXTRALIFE_PARTICIPANT_ID).then(data => {
const sumDonations = moneyFormatter.format(data.sumDonations),
fundraisingGoal = moneyFormatter.format(data.fundraisingGoal),
percentComplete = Math.round(data.sumDonations / data.fundraisingGoal * 100);
client.say(channel, `${data.displayName} has raised ${sumDonations} out of ${fundraisingGoal} (${percentComplete}%)`);
});
break;
}
});