Skip to content

Commit

Permalink
add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
mzrimsek committed Jun 14, 2021
1 parent 4ddc879 commit 8616969
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 96 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 100,
"trailingComma": "none",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"arrowParens": "avoid",
"bracketSpacing": true
}
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@ A bot to interact with Twitch, Discord, and anything else your mind (and my free

### Admin (only work when triggered from Twitch chat)

* !active - toggles whether commands work or not
* !add <command> <message> - adds a command (or edits the existing command) with the specified message
* !rm <command> - removes the specified command
- !active - toggles whether commands work or not
- !add <command> <message> - adds a command (or edits the existing command) with the specified message
- !rm <command> - removes the specified command

### Mod (only work when triggered from Twitch chat)

* !addw <word/phrase> - adds a word/phrase to be tracked
* !rmw <word/phrase> - removes a word/phrase from being tracked
* !incw <word/phrase> <number:optional> - increments the word/phrase count. Optional number parameter sets the amount to increment by
* !clrw <word/phrase> - resets the word/phrase count back to 0
- !addw <word/phrase> - adds a word/phrase to be tracked
- !rmw <word/phrase> - removes a word/phrase from being tracked
- !incw <word/phrase> <number:optional> - increments the word/phrase count. Optional number parameter sets the amount to increment by
- !clrw <word/phrase> - resets the word/phrase count back to 0

### General (work when triggered from both Twitch chat and Discord)

* !help - displays help command (this shows different commands based on where it is triggered from)
- !help - displays help command (this shows different commands based on where it is triggered from)

### General Twitch Only

* !wordcount <word/phrase> - gets the tracked count of a word/phrase
* !lightsoff - turns off the lights in stream room
* !lightson - turns on the lights in stream room
* !lightscol <color_name/R G B> - sets color of lights in stream room
* color_name must be a [valid CSS3 color name](https://www.w3.org/wiki/CSS/Properties/color/keywords)
* ex: !lightscol red
* RGB value must be 3 numbers between 0 and 255
* ex: !lightscol 255 0 0
* !lightsrand - sets color of lights in stream room to random color
- !wordcount <word/phrase> - gets the tracked count of a word/phrase
- !lightsoff - turns off the lights in stream room
- !lightson - turns on the lights in stream room
- !lightscol <color_name/R G B> - sets color of lights in stream room
- color_name must be a [valid CSS3 color name](https://www.w3.org/wiki/CSS/Properties/color/keywords)
- ex: !lightscol red
- RGB value must be 3 numbers between 0 and 255
- ex: !lightscol 255 0 0
- !lightsrand - sets color of lights in stream room to random color

### OBS (only work when triggered from Twitch chat)

* !cam - toggles face cam visibility
* !mic - toggles desktop mic active
* !color <number:optional> - changes face cam color overlay color. Optional number parameter causes color to change that many times in 1 second (max 1000)
* !reset - reset face cam and mic changes back to default
* !aqua - toggle Aqua visibility on scenes with her in them
- !cam - toggles face cam visibility
- !mic - toggles desktop mic active
- !color <number:optional> - changes face cam color overlay color. Optional number parameter causes color to change that many times in 1 second (max 1000)
- !reset - reset face cam and mic changes back to default
- !aqua - toggle Aqua visibility on scenes with her in them

## Invite to Server

https://discord.com/api/oauth2/authorize?client_id=772707347520946197&permissions=0&scope=bot

## Development Notes

* Command functions return true when it handles the command, false if not
- Command functions return true when it handles the command, false if not
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ services:
- DISCORD_TOKEN=${DISCORD_TOKEN}
- DISCORD_BOT_USER_ID=${DISCORD_BOT_USER_ID}
- MQTT_BROKER_ADDRESS=${MQTT_BROKER_ADDRESS}
restart: unless-stopped
restart: unless-stopped
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"tsc": "tsc",
"dev": "ts-node-dev src/index.ts",
"start": "tsc && node ./dist/index.js"
"start": "tsc && node ./dist/index.js",
"format": "prettier --write ./src/**"
},
"author": "Mike Zrimsek",
"license": "MIT",
Expand All @@ -21,6 +22,7 @@
},
"devDependencies": {
"@types/tmi.js": "^1.7.1",
"prettier": "2.3.1",
"ts-node-dev": "^1.1.6",
"typescript": "^4.3.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/clients/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ discordClient.login(discordConfig.token);
discordClient.on('ready', () => {
logger.info('Connected to Discord');
logger.info(`Logged in as: ${discordClient.user.tag} - (${discordClient.user.id})`);
});
});
5 changes: 1 addition & 4 deletions src/clients/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ try {
const commandsCollection = firestore.collection(COMMANDS_COLLECTION);
const trackingWordsCollection = firestore.collection(WORD_TRACKING_COLLECTION);

export const collections = [
commandsCollection,
trackingWordsCollection
];
export const collections = [commandsCollection, trackingWordsCollection];
2 changes: 1 addition & 1 deletion src/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './twitch';
export * from './obs';
export * from './firebase';
export * from './discord';
export * from './mqtt';
export * from './mqtt';
2 changes: 1 addition & 1 deletion src/clients/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import { mqttConfig, logger } from '../config';
export const mqttClient = mqtt.connect(`tcp://${mqttConfig.address}`);
mqttClient.on('connect', () => {
logger.info('Connected to MQTT Broker');
});
});
2 changes: 1 addition & 1 deletion src/clients/obs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ obsClient.on('ConnectionOpened', () => {
obsClient.on('ConnectionClosed', () => {
obsConnected = false;
logger.info('Disconnected from OBSWebSocket');
});
});
2 changes: 1 addition & 1 deletion src/clients/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import * as tmi from 'tmi.js';
import { twitchConfig } from '../config';

export const twitchClient = new tmi.client(twitchConfig);
twitchClient.connect();
twitchClient.connect();
6 changes: 4 additions & 2 deletions src/commands/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ async function handleHelpCommand(messageParts, printFunc, clients, ...extraComma
const userCommandList = userCommands.map(userCommand => userCommand.command);
const allCommandList = [...userCommandList, ...extraCommandsList, HELP_COMMAND];

const helpMessageList = allCommandList.map(command => `${COMMAND_PREFACE}${command}`).join(', ');
const helpMessageList = allCommandList
.map(command => `${COMMAND_PREFACE}${command}`)
.join(', ');
printFunc(`Here are the available commands: \n${helpMessageList}`);

return true;
Expand All @@ -48,4 +50,4 @@ async function handleHelpCommand(messageParts, printFunc, clients, ...extraComma
module.exports = {
handleUserCommand,
handleHelpCommand
};
};
95 changes: 70 additions & 25 deletions src/commands/twitch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { obsConfig, logger } from '../config';
import { COMMAND_PREFACE, ADMIN_COMMANDS, OBS_COMMANDS, WORD_TRACKING_COMMANDS, LIGHT_COMMANDS, LIGHT_TOPICS, SOURCES } from '../constants';
import {
COMMAND_PREFACE,
ADMIN_COMMANDS,
OBS_COMMANDS,
WORD_TRACKING_COMMANDS,
LIGHT_COMMANDS,
LIGHT_TOPICS,
SOURCES
} from '../constants';
const { getRandomColor, loadTrackingPhrases, getRandomInt } = require('../utils');

async function handleOBSCommand(messageParts, clients, obsConnected) {
Expand All @@ -11,7 +19,7 @@ async function handleOBSCommand(messageParts, clients, obsConnected) {
await obsClient.connect(obsConfig);
return true;
} catch {
logger.info('Unable to connect to OBSWebsocket')
logger.info('Unable to connect to OBSWebsocket');
return false;
}
}
Expand All @@ -28,16 +36,24 @@ async function handleOBSCommand(messageParts, clients, obsConnected) {
filterName: 'Color Correction',
filterEnabled: false
});
obsClient.send('SetSceneItemRender', { source: SOURCES.WEBCAM, render: true });
obsClient.send('SetSceneItemRender', {
source: SOURCES.WEBCAM,
render: true
});
obsClient.send('SetMute', { source: SOURCES.MIC, mute: false });
}
return true;
}
case `${COMMAND_PREFACE}${OBS_COMMANDS.TOGGLE_CAM}`: {
if (await isOBSClientConnected()) {
const properties = await obsClient.send('GetSceneItemProperties', { item: { name: SOURCES.WEBCAM } });
const properties = await obsClient.send('GetSceneItemProperties', {
item: { name: SOURCES.WEBCAM }
});
const { visible } = properties;
obsClient.send('SetSceneItemRender', { source: SOURCES.WEBCAM, render: !visible });
obsClient.send('SetSceneItemRender', {
source: SOURCES.WEBCAM,
render: !visible
});
}
return true;
}
Expand Down Expand Up @@ -74,7 +90,7 @@ async function handleOBSCommand(messageParts, clients, obsConnected) {
color: randomColor
}
});
};
}

const rate = 1000 / numTimes;
for (let i = 0; i < numTimes; i++) {
Expand All @@ -85,9 +101,14 @@ async function handleOBSCommand(messageParts, clients, obsConnected) {
}
case `${COMMAND_PREFACE}${OBS_COMMANDS.TOGGLE_AQUA}`: {
if (await isOBSClientConnected()) {
const properties = await obsClient.send('GetSceneItemProperties', { item: { name: SOURCES.AQUA } });
const properties = await obsClient.send('GetSceneItemProperties', {
item: { name: SOURCES.AQUA }
});
const { visible } = properties;
obsClient.send('SetSceneItemRender', { source: SOURCES.AQUA, render: !visible });
obsClient.send('SetSceneItemRender', {
source: SOURCES.AQUA,
render: !visible
});
}
return true;
}
Expand All @@ -97,7 +118,13 @@ async function handleOBSCommand(messageParts, clients, obsConnected) {
}
}

function handleAdminCommand(messageParts, printFunc, commandsActive, commandsActiveUpdateFunc, clients) {
function handleAdminCommand(
messageParts,
printFunc,
commandsActive,
commandsActiveUpdateFunc,
clients
) {
const { firebase } = clients;
const { collections } = firebase;
const { commandsCollection } = collections;
Expand All @@ -110,8 +137,7 @@ function handleAdminCommand(messageParts, printFunc, commandsActive, commandsAct
printFunc('Bot commands are disabled!');
logger.info('Twitch commands are disabled');
commandsActiveUpdateFunc(false);
}
else {
} else {
printFunc('Bot commands are enabled!');
logger.info('Twitch commands are enabled');
commandsActiveUpdateFunc(true);
Expand All @@ -121,22 +147,28 @@ function handleAdminCommand(messageParts, printFunc, commandsActive, commandsAct
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.ADD_COMMAND}`: {
const newCommand = messageParts[1];
const newMessage = messageParts.slice(2).join(' ');
commandsCollection.doc(newCommand).set({
command: newCommand,
message: newMessage
}).then(() => logger.info(`Command added: ${newCommand}`));
commandsCollection
.doc(newCommand)
.set({
command: newCommand,
message: newMessage
})
.then(() => logger.info(`Command added: ${newCommand}`));
return true;
}
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.REMOVE_COMMAND}`: {
const commandToRemove = messageParts[1];
commandsCollection.doc(commandToRemove).delete().then(() => logger.info(`Command removed: ${commandToRemove}`));
commandsCollection
.doc(commandToRemove)
.delete()
.then(() => logger.info(`Command removed: ${commandToRemove}`));
return true;
}
default: {
return false;
}
}
};
}

async function handleTwitchUserCommand(messageParts, username, printFunc, clients) {
const { firebase, mqttClient } = clients;
Expand Down Expand Up @@ -227,9 +259,12 @@ async function handleModCommand(messageParts, printFunc, clients) {

const newPhrase = messageParts.slice(1).join('_');
if (newPhrase && !trackingPhrases.includes(newPhrase)) {
trackingWordsCollection.doc(newPhrase).set({
count: 0
}).then(() => logger.info(`Tracking word added: ${newPhrase}`));
trackingWordsCollection
.doc(newPhrase)
.set({
count: 0
})
.then(() => logger.info(`Tracking word added: ${newPhrase}`));
}
return true;
}
Expand All @@ -238,7 +273,10 @@ async function handleModCommand(messageParts, printFunc, clients) {

const phraseToRemove = messageParts.slice(1).join('_');
if (phraseToRemove && trackingPhrases.includes(phraseToRemove)) {
trackingWordsCollection.doc(phraseToRemove).delete().then(() => logger.info(`Tracking word removed: ${phraseToRemove}`));
trackingWordsCollection
.doc(phraseToRemove)
.delete()
.then(() => logger.info(`Tracking word removed: ${phraseToRemove}`));
}
return true;
}
Expand All @@ -247,7 +285,10 @@ async function handleModCommand(messageParts, printFunc, clients) {

const phraseToClear = messageParts.slice(1).join('_');
if (phraseToClear && trackingPhrases.includes(phraseToClear)) {
trackingWordsCollection.doc(phraseToClear).update('count', 0).then(() => logger.info(`Tracking word cleared: ${phraseToClear}`));
trackingWordsCollection
.doc(phraseToClear)
.update('count', 0)
.then(() => logger.info(`Tracking word cleared: ${phraseToClear}`));
}
return true;
}
Expand All @@ -260,14 +301,18 @@ async function handleModCommand(messageParts, printFunc, clients) {
const hasCount = Number.isInteger(lastTokenAsNum);
const count = hasCount ? Number.parseInt(lastToken) : 1;

const phraseParts = hasCount ? messageParts.slice(1, messageParts.length - 1) : messageParts.slice(1);
const phraseParts = hasCount
? messageParts.slice(1, messageParts.length - 1)
: messageParts.slice(1);
const phraseToIncrement = phraseParts.join('_');

if (phraseToIncrement && trackingPhrases.includes(phraseToIncrement)) {
const documentRef = trackingWordsCollection.doc(phraseToIncrement);
const document = await documentRef.get();
const currentCount = document.get('count');
documentRef.update('count', currentCount + count).then(() => logger.info(`Tracking word incremented: ${phraseToIncrement}`));
documentRef
.update('count', currentCount + count)
.then(() => logger.info(`Tracking word incremented: ${phraseToIncrement}`));
}
return true;
}
Expand All @@ -282,4 +327,4 @@ module.exports = {
handleAdminCommand,
handleModCommand,
handleTwitchUserCommand
};
};
2 changes: 1 addition & 1 deletion src/config/discord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
token: process.env.DISCORD_TOKEN,
bot_user_id: process.env.DISCORD_BOT_USER_ID
};
};
Loading

0 comments on commit 8616969

Please sign in to comment.