diff --git a/bot.js b/bot.js index c54a4a7..5d64582 100644 --- a/bot.js +++ b/bot.js @@ -1,9 +1,10 @@ const tmi = require('tmi.js'); -const { getCpuInfo, getMemoryInfo, getDiskInfo, getTempInfo, getDevicesInfo, toggleIrlboxStream } = require('./commands'); +const { getCpuInfo, getMemoryInfo, getDiskInfo, getTempInfo, getDevicesInfo, toggleIrlboxStream, rebootIrlboxServer } = require('./commands'); const winston = require('winston'); const path = require('path'); const fs = require('fs'); +// Function to read config.json from the current working directory function readConfig() { const configPath = path.join(process.cwd(), 'config.json'); if (!fs.existsSync(configPath)) { @@ -20,8 +21,6 @@ const logger = require('./logger'); // Import the logger function hasRole(user, role, userState) { const cleanChannels = config.twitch.channels.map(channel => channel.replace('#', '').toLowerCase()); const isStreamer = cleanChannels.includes(user.toLowerCase()); - console.log(user) - console.log(config.twitch.channels.map(channel => channel.replace('#', '').toLowerCase())) const isMod = userState.mod || userState['user-type'] === 'mod' || (userState.badges && userState.badges.moderator); const isVip = userState.badges && userState.badges.vip; @@ -39,11 +38,11 @@ function hasRole(user, role, userState) { } } -// # ToDo: Add kick.com and break off chat into their own directory async function main() { try { logger.info('Starting Twitch bot...'); + // Define configuration options const opts = { identity: { username: config.twitch.username, @@ -52,6 +51,7 @@ async function main() { channels: config.twitch.channels }; + // Create a client with our options const client = new tmi.client(opts); // Register event handlers @@ -72,6 +72,7 @@ async function main() { logger.info(`Received command: ${commandName} from ${user}`); try { + // Execute the appropriate command if (commandName === '!cpu') { await getCpuInfo(client, target); } else if (commandName === '!memory') { @@ -86,6 +87,8 @@ async function main() { await toggleIrlboxStream(client, target, false); } else if (commandName === '!irlbox stop') { await toggleIrlboxStream(client, target, true); + } else if (commandName === '!reboot') { + await rebootIrlboxServer(client, target); } } catch (error) { logger.error(`Error executing command ${commandName}: ${error.message}`); diff --git a/commands.js b/commands.js index 1b8f6b6..8d1bc5e 100644 --- a/commands.js +++ b/commands.js @@ -2,9 +2,7 @@ const axios = require('axios'); const config = require('./config.json'); const logger = require('./logger'); // Import the logger - -// # ToDo: Clean up commands functions, turn each function into it's own module - +// Function to fetch data from the API async function fetchData() { try { const response = await axios.get(`${config.api.baseUrl}/realtimedata?boardCuid=${config.api.boardCuid}`, { @@ -21,6 +19,7 @@ async function fetchData() { } } +// Functions to get specific information async function getCpuInfo(client, target) { try { const data = await fetchData(); @@ -138,11 +137,32 @@ async function toggleIrlboxStream(client, target, isStreaming) { } } +// Function to reboot the IRLBox server +async function rebootIrlboxServer(client, target) { + try { + await axios.post(`${config.api.baseUrl}/reboot`, null, { + params: { boardCuid: config.api.boardCuid }, + headers: { + 'accept': '*/*', + 'Authorization': config.api.authToken + } + }); + + const message = 'IRLBox is rebooting.'; + client.say(target, message); + logger.info(`Output for !reboot: ${message}`); + } catch (error) { + logger.error('Error in rebootIrlboxServer:', error); + client.say(target, 'Error rebooting IRLBox server.'); + } +} + module.exports = { getCpuInfo, getMemoryInfo, getDiskInfo, getTempInfo, getDevicesInfo, - toggleIrlboxStream + toggleIrlboxStream, + rebootIrlboxServer }; diff --git a/config.json b/config.json index c01c00e..750daa8 100644 --- a/config.json +++ b/config.json @@ -10,12 +10,13 @@ "authToken": "Bearer YOUR_API_AUTH_TOKEN" }, "commands": { - "!cpu": "mods", + "!cpu": "everyone", "!memory": "mods", - "!disk": "mods", + "!disk": "streamer", "!temp": "everyone", "!devices": "mods", "!irlbox start": "streamer", - "!irlbox stop": "streamer" + "!irlbox stop": "streamer", + "!reboot": "streamer" } }