Skip to content

Commit

Permalink
Added !reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
IRLtools authored Jul 12, 2024
1 parent 1c02169 commit c45be7c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
11 changes: 7 additions & 4 deletions bot.js
Original file line number Diff line number Diff line change
@@ -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)) {
Expand All @@ -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;

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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') {
Expand All @@ -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}`);
Expand Down
28 changes: 24 additions & 4 deletions commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, {
Expand All @@ -21,6 +19,7 @@ async function fetchData() {
}
}

// Functions to get specific information
async function getCpuInfo(client, target) {
try {
const data = await fetchData();
Expand Down Expand Up @@ -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
};
7 changes: 4 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit c45be7c

Please sign in to comment.