forked from CodingGarden/stream-overlay-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
216 lines (192 loc) · 6.34 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
let greeted = {};
let haveHosted = {};
let haveRaided = {};
const alerts = document.getElementById("alerts");
const speechBubble = document.getElementById("speech");
const messageQueue = [];
const sounds = {
host: 'sounds/drop.mp3',
raid: 'sounds/wavey-piano-with-marimba.mp3',
sub: 'sounds/guitar-delay.mp3',
bits: 'sounds/delay-grand-arpeggio.mp3'
};
function playAlertSound(src) {
const sound = new Audio(src || 'sounds/drop.mp3');
sound.play();
sound.addEventListener('ended', () => {
sound.remove();
});
}
const client = new tmi.Client({
connection: {
secure: true,
reconnect: true
},
identity: {
username: 'codinggarden',
password: config.token,
},
channels: [`codinggarden`]
});
client.connect();
client.on('connected', (channel, userstate) => {
console.log("connected");
})
client.on('chat', (channel, userstate, message) => {
if (userstate.username === 'streamlabs') return;
var args = message.split(" ");
//Regular Greets
var greets = [];
//Subscriber Greets
if (userstate.badges) {
if (userstate.badges.hasOwnProperty('subscriber') || userstate.badges.hasOwnProperty('founder')) {
greets = [
`Subscriber <span class="bold">${userstate['display-name']}</span>, is digging in the garden again!`,
`Subscriber <span class="bold">${userstate['display-name']}</span>, has appeared!`,
];
}
//VIP Greets
if (userstate.badges.hasOwnProperty('vip')) {
greets = [
`VIP <span class="bold">${userstate['display-name']}</span>, has planted themselves!`,
`Welcome VIP <span class="bold">${userstate['display-name']}</span>, to the garden!.`,
];
}
//Moderator Greets
if (userstate.badges.hasOwnProperty('moderator')) {
greets = [
`Pruner <span class="bold">${userstate['display-name']}</span>, has appeared in the garden!`,
`Sharp sheers <span class="bold">${userstate['display-name']}</span> has, keeping the hedges neat!`
];
}
//Broadcaster Greets
if (userstate.badges.hasOwnProperty('broadcaster')) {
greets = [
`Shh, CJ is talking!`,
'CJ, appreciates all of his seedlings!'
];
}
if (args[0] == "!speech") {
if (userstate.badges.hasOwnProperty('broadcaster')) {
messageQueue.push(message.slice(args[0].length));
return;
}
}
if (greeted[userstate.username]) return;
if (greets.length) {
randomGreet = Math.floor(Math.random() * greets.length)
greeted[userstate.username] = true;
messageQueue.push(greets[randomGreet]);
}
}
});
client.on('cheer', (channel, userstate) => {
messageQueue.push({
message: `Thanks for the ${parseInt(userstate.bits)} bits <span class="bold">${userstate.username}</span>!`,
sound: sounds.bits,
});
});
let giftTimeout = null;
let lastGifter = '';
let lastGiftAmount = 0;
client.on('subgift', (channel, username, streakMonths, recipient, methods, userstate) => {
if (subgiftCheck = `1`) {
if (username == lastGifter) {
clearTimeout(giftTimeout);
lastGiftAmount++;
} else {
lastGifter = username;
lastGiftAmount = 1;
}
giftTimeout = setTimeout(() => {
messageQueue.push({
message: `<span class="bold">${username}</span>, has gifted ${lastGiftAmount} subscription(s) to the garden!`,
sound: sounds.bits,
});
lastGiftAmount = 0;
allRecipients = ``;
}, 1500);
}
});
client.on('anongiftpaidupgrade', (channel, username, sender, userstate) => {
messageQueue.push({
message: `<span class="bold">${username}</span>, upgraded their subscription. (Originally from an anonymous user.)`,
sound: sounds.sub,
});
});
client.on('giftpaidupgrade', (channel, username, sender, userstate) => {
messageQueue.push({
message: `<span class="bold">${username}</span>, upgraded their subscription. (Originally from ${sender}.)`,
sound: sounds.sub,
});
});
client.on('resub', (channel, username, months, message, userstate, methods) => {
let cumulativeMonths = ~~userstate["msg-param-cumulative-months"];
if (userstate["msg-param-should-share-streak"] = true) {
messageQueue.push({
message: `Thanks for re-subscribing for ${cumulativeMonths} months <span class="bold">${username}</span>.`,
sound: sounds.sub,
});
} else {
messageQueue.push({
message: `Thanks for re-subscribing <span class="bold">${username}</span>.`,
sound: sounds.sub,
});
}
});
const planTypes = {
'2000': 'Tier 2',
'3000': 'Tier 3',
};
client.on('subscription', (channel, username, { prime, plan, planName }, msg, userstate) => {
let message = '';
if (prime) {
message = `Thanks for subscribing with Twitch Prime <span class="bold">${username}</span>!`;
} else if (planTypes[plan]) {
message = `Thanks for the ${planTypes[plan]} subscription <span class="bold">${username}</span>!`;
} else {
message = `Thanks for the subscription <span class="bold">${username}</span>!`;
}
messageQueue.push({
message,
sound: sounds.sub,
});
});
client.on('hosted', (channel, username, viewers, autohost) => {
if (haveHosted[username]) return;
haveHosted[username] = true;
messageQueue.push({
message: `<span class="bold">${username}</span>, has hosted with ${viewers} viewers!`,
sound: viewers > 1 ? sounds.host : '',
});
});
client.on('raided', (channel, username, viewers) => {
if (haveRaided[username]) return;
haveRaided[username] = true;
messageQueue.push({
message: `<span class="bold">${username}</span>, is raiding with ${viewers} viewers!`,
sound: sounds.raid,
});
});
var speechTimer = null;
drawSpeech();
function drawSpeech() {
if (messageQueue.length) {
const item = messageQueue.shift();
speechBubble.innerHTML = item.message || item;
if (item.sound) {
playAlertSound(item.sound);
}
clearTimeout(speechTimer)
alerts.style.opacity = '1';
alerts.style.transform = 'scale(1)';
speechTimer = setTimeout(pauseAndFade, 7500)
setTimeout(drawSpeech, item.message ? 5000 : 2000);
} else {
setTimeout(drawSpeech, 2000);
}
};
function pauseAndFade() {
alerts.style.opacity = '0';
alerts.style.transform = 'scale(0)';
};