Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate clap command from lioranboard #4

Merged
merged 3 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bot_brain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";
const switchGreenScreenBG = require("./obs_helper.js").switchGreenScreenBG;
const obs = require("./obs_helper.js");

let matanbot_mention_count = 0;

Expand Down Expand Up @@ -39,7 +39,7 @@ const changeGreenScreenBackground = async (image_request) => {
for(let image of images) {
if (image_request === image.command_name) {
try {
if(await switchGreenScreenBG(image.obs_name)) {
if(await obs.switchGreenScreenBG(image.obs_name)) {
return `Background has been changed.`
} else {
return `Cannot change background right now.`
Expand All @@ -61,6 +61,9 @@ const message_main = async (user_info, user_msg) => {
// Remove whitespace from chat message
const user_command = user_msg.trim().toLowerCase();

if (user_command.search("clap") != -1) {
obs.showHeartEyes();
}
if (user_command.search("discord") != -1) {
return "Did someone say discord? Join Matan's discord to get updates on stream schedule, juggling advice, hangout and all around have a good time! https://discord.gg/bNUaFRE";
}
Expand Down
86 changes: 56 additions & 30 deletions obs_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,43 @@ const OBSWebSocket = require('obs-websocket-js');

const obs = new OBSWebSocket();

const port = process.env.OBS_PORT || 'localhost:4444';
const port = process.env.OBS_PORT;
const pw = process.env.OBS_PW;

async function getScenes() {
try {
let scene_data = await obs.send('GetSceneList');
console.log(scene_data.scenes);
scene_data.scenes.forEach(scene => {
console.log(`Scene: ${scene.name}`);
scene.sources.forEach(source => {
console.log(source);
});
process.exit();
});
} catch (err) {
console.log(err);
throw(err);
}
let current_scene; //keep current with OBS by listening to 'SwitchScenes' event
obs.on('SwitchScenes', (data) => {
current_scene = data.sceneName;
});

async function getScene(scene_name) {
let scene_data = await obs.send('GetSceneList');
return scene_data.scenes.find(scene => {
return scene.name === scene_name;
});
}

async function connect() {
try {
await obs.connect({address : port, password: pw});
console.log('connected to obs websocket');
//getScenes();
current_scene = (await obs.send('GetCurrentScene')).name;
} catch (err) {
console.log('failed to connect to obs websocket');
console.log('OBS may not be open.')
console.log(err);
//throw(err);
}
}

//will look for an image named image_name in the 'green screen effects'
//scene of OBS
//then will reorder the sources such that the image is
//right after the camera, in this case 'green screen - pre'
/*
will look for an image named image_name in the 'green screen effects'
scene of OBS
then will reorder the sources such that the image is
right after the camera, in this case 'green screen - pre'
*/
const switchGreenScreenBG = async (image_name) => {
try {
let scene_data = await obs.send('GetSceneList');
let green_screen_scene = scene_data.scenes.find(scene => {
return scene.name === 'green screen effects';
});
if (scene_data.currentScene != green_screen_scene.name) {
let green_screen_scene = await getScene('green screen effects');
if (current_scene != green_screen_scene.name) {
return false;
}
let image = green_screen_scene.sources.find(source => {
Expand All @@ -56,7 +49,6 @@ const switchGreenScreenBG = async (image_name) => {
let camera_order = green_screen_scene.sources.findIndex(source => {
return source.name === 'green screen - pre';
});
//console.log(camera_order);

re_ordered_sources = [];

Expand All @@ -74,7 +66,6 @@ const switchGreenScreenBG = async (image_name) => {
}
}

//console.log(re_ordered_sources);
await obs.send('ReorderSceneItems', {
"scene": green_screen_scene,
"items": re_ordered_sources
Expand All @@ -86,4 +77,39 @@ const switchGreenScreenBG = async (image_name) => {
}
}

module.exports = { switchGreenScreenBG: switchGreenScreenBG, connect: connect }
/*
OBS scene 'hearts helper' has multiple copies of the same media source
when this method is called, show the first copy that is not
currently being shown.

video is loop of a heart eyes emoji, hence the naming
*/
const showHeartEyes = async () => {
const heart_scene = await getScene('hearts helper');

for (let source of heart_scene.sources) {
if(!source.render) {
//render source
try {
await obs.send('SetSceneItemRender', {
"scene-name": "hearts helper",
"source": source.name,
"render": true
});
//4.5 seconds later (length of video clip), unrender
setTimeout(async () => {
await obs.send('SetSceneItemRender', {
"scene-name": "hearts helper",
"source": source.name,
"render": false
});
}, 4500);
} catch (err) {
console.log(err);
}
break; //only render one source per call
}
}
}

module.exports = { switchGreenScreenBG: switchGreenScreenBG, connect: connect, showHeartEyes: showHeartEyes }