Skip to content

Commit

Permalink
refactor commands into groups and handler functions
Browse files Browse the repository at this point in the history
add logging
  • Loading branch information
Mike Zrimsek committed Nov 2, 2020
1 parent 16cd0f4 commit 4b8e04a
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 89 deletions.
30 changes: 30 additions & 0 deletions constants/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const COMMAND_PREFACE = '!';

const ADMIN_USER = 'bastulos';

const ADMIN_COMMANDS = {
TOGGLE_COMMANDS_ACTIVE: 'active',
RECONNECT_OBS: 'obs'
};

const OBS_COMMANDS = {
RESET: 'reset',
TOGGLE_CAM: 'cam',
TOGGLE_MUTE_MIC: 'mic',
CHANGE_OVERLAY_COLOR: 'color',
TOGGLE_AQUA: 'aqua'
};

const USER_COMMANDS = {
COMMAND_LIST: 'help',
HELLO: 'hello',
HEART: 'heart'
};

module.exports = {
COMMAND_PREFACE,
ADMIN_USER,
ADMIN_COMMANDS,
OBS_COMMANDS,
USER_COMMANDS
};
9 changes: 9 additions & 0 deletions constants/obs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const SOURCES = {
WEBCAM: 'Webcam',
MIC: 'Desktop Mic',
AQUA: 'Aqua'
};

module.exports = {
SOURCES
};
161 changes: 91 additions & 70 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,90 @@ require('dotenv').config();

const tmi = require('tmi.js');
const OBSWebSocket = require('obs-websocket-js');
const logger = require('winston');

logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { colorize: true });
logger.level = 'debug';

const tmiConfig = require('./config/tmi');
const obsConfig = require('./config/obs');

const client = new tmi.client(tmiConfig);
const obs = new OBSWebSocket();
const { COMMAND_PREFACE, ADMIN_USER, ADMIN_COMMANDS, OBS_COMMANDS, USER_COMMANDS } = require('./constants/commands');
const { SOURCES } = require('./constants/obs');

const { getRandomColor } = require('./utils');

const client = new tmi.client(tmiConfig);
client.connect();
obs.connect(obsConfig);

const COMMAND_PREFACE = '!';
const ADMIN_USER = 'bastulos';
const ADMIN_COMMANDS = {
TOGGLE_COMMANDS_ACTIVE: 'active',
RECONNECT_OBS: 'obs'
};
const COMMANDS = {
COMMAND_LIST: 'help',
RESET: 'reset',
TOGGLE_CAM: 'cam',
TOGGLE_MUTE_MIC: 'mic',
CHANGE_OVERLAY_COLOR: 'color',
TOGGLE_AQUA: 'aqua',
HELLO: 'hello',
HEART: 'heart'
};
const SOURCES = {
WEBCAM: 'Webcam',
MIC: 'Desktop Mic',
AQUA: 'Aqua'
};
const obs = new OBSWebSocket();

let active = true;

client.on('chat', async (channel, userInfo, message, self) => {
if (self) return; // ignore messages from the bot
function handleAdminCommand(channel, messageParts) {
const command = messageParts[0];

if (message[0] !== COMMAND_PREFACE) return; // ignore non command messages
switch (command) {
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.TOGGLE_COMMANDS_ACTIVE}`: {
if (active) {
const message = 'Bot commands are disabled!';

const normalizedMessage = message.toLowerCase();
const messageParts = normalizedMessage.split(' ');
const command = messageParts[0];
client.say(channel, message);
logger.info(message);

if (userInfo.username === ADMIN_USER) {
switch (command) {
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.TOGGLE_COMMANDS_ACTIVE}`: {
if (active) {
client.say(channel, `Bot commands are disabled!`);
active = false;
}
else {
client.say(channel, `Bot commands are enabled!`);
active = true;
}
break;
}
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.RECONNECT_OBS}`: {
obs.connect(obsConfig);
break;
active = false;
}
default: {
break;
else {
const message = 'Bot commands are enabled!';

client.say(channel, message);
logger.info(message);

active = true;
}
break;
}
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.RECONNECT_OBS}`: {
obs.connect(obsConfig).then(() => logger.info('Connected to OBSWebSocket'));
break;
}
default: {
break;
}
}
};

if (!active) return;
function handleUserCommand(channel, userInfo, messageParts) {
const command = messageParts[0];

switch (command) {
case `${COMMAND_PREFACE}${COMMANDS.RESET}`: {
case `${COMMAND_PREFACE}${USER_COMMANDS.HEART}`:
case `${COMMAND_PREFACE}${USER_COMMANDS.HELLO}`: {
client.say(channel, `@${userInfo.username}, may your heart be your guiding key`);
break;
}
case `${COMMAND_PREFACE}${USER_COMMANDS.COMMAND_LIST}`: {
const allCommands = {
...OBS_COMMANDS,
...USER_COMMANDS
};
const commands = Object.keys(allCommands);
const commandList = commands.map(commandKey => `${COMMAND_PREFACE}${allCommands[commandKey]}`).join(', ');
client.say(channel, `Here are the available commands: \n${commandList}`);
break;
}
default: {
break;
}
}
}

async function handleOBSCommand(messageParts) {
const command = messageParts[0];

switch (command) {
case `${COMMAND_PREFACE}${OBS_COMMANDS.RESET}`: {
obs.send('SetSourceFilterVisibility', {
sourceName: SOURCES.WEBCAM,
filterName: 'Color Correction',
Expand All @@ -81,28 +95,17 @@ client.on('chat', async (channel, userInfo, message, self) => {
obs.send('SetMute', { source: SOURCES.MIC, mute: false });
break;
}
case `${COMMAND_PREFACE}${COMMANDS.HEART}`:
case `${COMMAND_PREFACE}${COMMANDS.HELLO}`: {
client.say(channel, `@${userInfo.username}, may your heart be your guiding key`);
break;
}
case `${COMMAND_PREFACE}${COMMANDS.COMMAND_LIST}`: {
const commands = Object.keys(COMMANDS);
const commandList = commands.map(commandKey => `${COMMAND_PREFACE}${COMMANDS[commandKey]}`).join(', ');
client.say(channel, `Here are the available commands: \n${commandList}`);
break;
}
case `${COMMAND_PREFACE}${COMMANDS.TOGGLE_CAM}`: {
case `${COMMAND_PREFACE}${OBS_COMMANDS.TOGGLE_CAM}`: {
const properties = await obs.send('GetSceneItemProperties', { item: { name: SOURCES.WEBCAM } });
const { visible } = properties;
obs.send('SetSceneItemRender', { source: SOURCES.WEBCAM, render: !visible });
break;
}
case `${COMMAND_PREFACE}${COMMANDS.TOGGLE_MUTE_MIC}`: {
case `${COMMAND_PREFACE}${OBS_COMMANDS.TOGGLE_MUTE_MIC}`: {
obs.send('ToggleMute', { source: SOURCES.MIC });
break;
}
case `${COMMAND_PREFACE}${COMMANDS.CHANGE_OVERLAY_COLOR}`: {
case `${COMMAND_PREFACE}${OBS_COMMANDS.CHANGE_OVERLAY_COLOR}`: {
let numTimes = messageParts[1] ? parseInt(messageParts[1]) : 1;

if (numTimes < 0) {
Expand All @@ -119,7 +122,7 @@ client.on('chat', async (channel, userInfo, message, self) => {
filterEnabled: true
});

const setColorCorrectionToRandomColor = () => {
function setColorCorrectionToRandomColor() {
const randomColor = getRandomColor();
obs.send('SetSourceFilterSettings', {
sourceName: SOURCES.WEBCAM,
Expand All @@ -137,7 +140,7 @@ client.on('chat', async (channel, userInfo, message, self) => {

break;
}
case `${COMMAND_PREFACE}${COMMANDS.TOGGLE_AQUA}`: {
case `${COMMAND_PREFACE}${OBS_COMMANDS.TOGGLE_AQUA}`: {
const properties = await obs.send('GetSceneItemProperties', { item: { name: SOURCES.AQUA } });
const { visible } = properties;
obs.send('SetSceneItemRender', { source: SOURCES.AQUA, render: !visible });
Expand All @@ -147,8 +150,26 @@ client.on('chat', async (channel, userInfo, message, self) => {
break;
}
}
});
}

client.on('chat', async (channel, userInfo, message, self) => {
if (self) return; // ignore messages from the bot

if (message[0] !== COMMAND_PREFACE) return; // ignore non command messages

function getRandomColor() {
return (Math.random() * 4294967296) >>> 0;
}
const normalizedMessage = message.toLowerCase();
const messageParts = normalizedMessage.split(' ');

try {
if (userInfo.username === ADMIN_USER) {
handleAdminCommand(channel, messageParts);
}

if (!active) return;

handleUserCommand(channel, userInfo, messageParts);
handleOBSCommand(messageParts);
} catch (error) {
logger.error(error);
}
});
Loading

0 comments on commit 4b8e04a

Please sign in to comment.