Add 'www/mc_br.js'

This commit is contained in:
Sarimoko 2022-02-16 08:59:40 +00:00
parent 59750a4416
commit b7b3c4ae63
1 changed files with 46 additions and 0 deletions

46
www/mc_br.js Normal file
View File

@ -0,0 +1,46 @@
const countElement = document.querySelector('#mrbr_count');
const usersElement = document.querySelector('#mrbr_users');
const statusElement = document.querySelector('#mrbr_status');
const params = new URLSearchParams(window.location.search);
const channel = params.get('channel') || 'sarimoko';
const client = new tmi.Client({
connection: {
secure: true,
reconnect: true,
},
channels: [channel],
});
client.connect().then(() => {
statusElement.textContent = `Connection to ${channel} successful, listening for votes...`;
});
let listeningForCount = false;
let users = {};
client.on('message', (wat, tags, message, self) => {
if (self) return;
const { username } = tags;
if (username.toLowerCase() === channel.toLowerCase()) {
if (message === '!startmcbr') {
listeningForCount = true;
}
else if (message === '!stopmcbr') {
listeningForCount = false;
// say count out loud.
const sayCount = new SpeechSynthesisUtterance(Object.keys(users).length);
window.speechSynthesis.speak(sayCount);
}
else if (message === '!clearmcbr') {
countElement.textContent = 'Waiting for votes...';
usersElement.textContent = '';
users = {};
}
} else if (listeningForCount && message === '!mcbr') {
users[tags.username] = true;
// display current count page.
countElement.textContent = Object.keys(users).length;
usersElement.textContent = Object.keys(users).join(', ');
}
});