Last active
September 6, 2024 19:22
-
-
Save StoneyEagle/6d65d1d2660813eba6c8febbce8f6785 to your computer and use it in GitHub Desktop.
Temperature conversion for twitch chat
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
const config = { | |
settings: { | |
TWITCH: { | |
USERNAME: '', | |
OAUTH_TOKEN: '', | |
CHANNEL_NAME: '' | |
}, | |
}, | |
} | |
var client; | |
function sendMessage(message) { | |
console.warn(message); | |
client.say(config.settings.TWITCH.CHANNEL_NAME, message).catch(console.error); | |
} | |
function appendScriptFilesToDocument(filePaths) { | |
return new Promise((resolve, reject) => { | |
let count = 0; | |
const total = filePaths.length; | |
if (total === 0) { | |
resolve(); | |
} | |
function appendFile(filePath) { | |
let file; | |
if (filePath.endsWith('.js')) { | |
file = document.createElement('script'); | |
file.src = filePath; | |
} else if (filePath.endsWith('.css')) { | |
file = document.createElement('link'); | |
file.rel = 'stylesheet'; | |
file.href = filePath; | |
} else { | |
reject(new Error('Unsupported file type')); | |
} | |
if (!file) return reject(new Error('File could not be loaded')); | |
file.addEventListener('load', () => { | |
count++; | |
if (count === total) { | |
resolve(); | |
} else { | |
appendFile(filePaths[count]); | |
} | |
}); | |
file.addEventListener('error', (err) => { | |
reject(err); | |
}); | |
document.head.appendChild(file); | |
} | |
appendFile(filePaths[0]); | |
}); | |
} | |
function truncateDecimals(number, digits) { | |
return number.toFixed(digits); | |
} | |
function convertCelciusToFahrenheit(celcius) { | |
return truncateDecimals(celcius * 9 / 5 + 32, 0); | |
} | |
function convertFahrenheitToCelcius(fahrenheit) { | |
return truncateDecimals((fahrenheit - 32) * 5 / 9, 0); | |
} | |
function messageHandleTempertature(message) { | |
const match = message.match(/(\s|^)\d{1,}[CF](\s|$)/i); | |
if (!match) { | |
return; | |
} | |
const value = parseInt(match[0]); | |
const unit = match[0].replace(value, ""); | |
if (unit === 'C' || unit === 'c') { | |
sendMessage(`${value}°C is ${convertCelciusToFahrenheit(value)}°F`); | |
} else { | |
sendMessage(`${value}°F is ${convertFahrenheitToCelcius(value)}°C`); | |
} | |
} | |
appendScriptFilesToDocument([ | |
'https://nomercy.tv/js/tmi.js', | |
]).then(() => { | |
client = new tmi.Client({ | |
options: { | |
skipUpdatingEmotesets: true, | |
}, | |
identity: { | |
username: config.settings.TWITCH.USERNAME, | |
password: `oauth:${config.settings.TWITCH.OAUTH_TOKEN}`, | |
}, | |
channels: [config.settings.TWITCH.CHANNEL_NAME], | |
}); | |
client.connect().then(() => { | |
console.log('Connected'); | |
}); | |
client.on('message', (channel, tags, message, self) => { | |
console.log(`${tags['display-name']}: ${message}`); | |
if (self) return; | |
messageHandleTempertature(message); | |
}); | |
}).catch((err) => { | |
console.error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment