Skip to content

Commit

Permalink
add firebase to store user commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Zrimsek committed Nov 2, 2020
1 parent 4b8e04a commit 32618a8
Show file tree
Hide file tree
Showing 4 changed files with 1,131 additions and 55 deletions.
18 changes: 18 additions & 0 deletions config/firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const config = {
service_account: {
type: "service_account",
project_id: process.env.FIREBASE_PROJECT_ID,
private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
// https://stackoverflow.com/questions/50299329/node-js-firebase-service-account-private-key-wont-parse
private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: process.env.FIREBASE_CLIENT_EMAIL,
client_id: process.env.FIREBASE_CLIENT_ID,
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_x509_cert_url: process.env.FIREBASE_CLIENT_CERT_URL
},
database_url: process.env.FIREBASE_DATABASE_URL
};

module.exports = config;
44 changes: 39 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('dotenv').config();

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

logger.remove(logger.transports.Console);
Expand All @@ -10,39 +11,72 @@ logger.level = 'debug';

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

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

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

// init twitch client
const client = new tmi.client(tmiConfig);
client.connect();

// init obs client
const obs = new OBSWebSocket();

let active = true;
// init firebase client
admin.initializeApp({
credential: admin.credential.cert(firebaseConfig.service_account),
databaseURL: firebaseConfig.database_url
});
const firestoreSettings = {
timestampsInSnapshots: true
};

let firestore = null;
let userCommands = null;
try {
firestore = admin.firestore();
firestore.settings(firestoreSettings);
logger.info('Connection to Firebase established');

loadUserCommands().then(commands => {
userCommands = commands;
console.log(userCommands);
logger.info('User commands loaded');
});
} catch {
logger.info('Failed to connect to Firebase');
}

async function loadUserCommands() {
const commandsSnapshot = await firestore.collection('commands').get();
return commandsSnapshot.docs.map(doc => doc.data());
}

let commandsActive = true;

function handleAdminCommand(channel, messageParts) {
const command = messageParts[0];

switch (command) {
case `${COMMAND_PREFACE}${ADMIN_COMMANDS.TOGGLE_COMMANDS_ACTIVE}`: {
if (active) {
if (commandsActive) {
const message = 'Bot commands are disabled!';

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

active = false;
commandsActive = false;
}
else {
const message = 'Bot commands are enabled!';

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

active = true;
commandsActive = true;
}
break;
}
Expand Down Expand Up @@ -165,7 +199,7 @@ client.on('chat', async (channel, userInfo, message, self) => {
handleAdminCommand(channel, messageParts);
}

if (!active) return;
if (!commandsActive) return;

handleUserCommand(channel, userInfo, messageParts);
handleOBSCommand(messageParts);
Expand Down
Loading

0 comments on commit 32618a8

Please sign in to comment.