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

Setup Twitch Timers #5

Merged
merged 7 commits into from
Jan 8, 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
117 changes: 59 additions & 58 deletions bot_brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,79 @@ const obs = require("./obs_helper.js");
let matanbot_mention_count = 0;

function respondToMatanbotMention(user_info) {
//random number 1 to 10
const random_num = Math.floor(Math.random() * 10 + 1);
let msg_out = "";
//random number 1 to 10
const random_num = Math.floor(Math.random() * 10 + 1);
let msg_out = "";

matanbot_mention_count = matanbot_mention_count + 1;
if (random_num > 9) {
msg_out =
"matanbot is currently under development, please excuse the lack of functionality";
} else if (random_num > 8) {
msg_out =
"I'm so glad someone recognizes my potential as being the best bot on the channel";
} else if (random_num > 6) {
msg_out = "I am a juggle bot, that is all I am";
} else {
msg_out = "hi " + user_info.username + "!";
}
if (matanbot_mention_count % 11 == 0) {
return `yall said my name ${count.toString()} times? wow!`;
} else {
return msg_out;
}
matanbot_mention_count = matanbot_mention_count + 1;
if (random_num > 9) {
msg_out =
"matanbot is currently under development, please excuse the lack of functionality";
} else if (random_num > 8) {
msg_out =
"I'm so glad someone recognizes my potential as being the best bot on the channel";
} else if (random_num > 6) {
msg_out = "I am a juggle bot, that is all I am";
} else {
msg_out = "hi " + user_info.username + "!";
}
if (matanbot_mention_count % 11 == 0) {
return `yall said my name ${count.toString()} times? wow!`;
} else {
return msg_out;
}
}

//make not case sensitive
const changeGreenScreenBackground = async (image_request) => {
let images = [
{ obs_name: "pool", command_name: "pool" },
{ obs_name: "tile-gradient", command_name: "tile" },
{ obs_name: "earth stock", command_name: "earth" },
{ obs_name: "duck", command_name: "duck" },
{ obs_name: "universe 1", command_name: "universe" },
];
let images = [
{ obs_name: "pool", command_name: "pool" },
{ obs_name: "tile-gradient", command_name: "tile" },
{ obs_name: "earth stock", command_name: "earth" },
{ obs_name: "duck", command_name: "duck" },
{ obs_name: "universe 1", command_name: "universe" },
{ obs_name: "invisible", command_name: "invisible" },
{ obs_name: "tornado", command_name: "fire" },
];

for(let image of images) {
if (image_request === image.command_name) {
try {
if(await obs.switchGreenScreenBG(image.obs_name)) {
return `Background has been changed.`
} else {
return `Cannot change background right now.`
for (let image of images) {
if (image_request === image.command_name) {
try {
if (await obs.switchGreenScreenBG(image.obs_name)) {
return `Background has been changed.`;
} else {
return `Cannot change background right now.`;
}
} catch (err) {
console.log(err);
}
}

} catch (err) {
console.log(err);
}
}
}

//cannot find matching image, tell user what possible image names are
return `Possible backgrounds are, pool, tile, earth, duck, universe. E.g. !background duck`;

//cannot find matching image, tell user what possible image names are
return `Possible backgrounds are, pool, tile, earth, duck, universe, invisible, fire. E.g. !background duck`;
}

// Called every time a message comes in
const message_main = async (user_info, user_msg) => {
// Remove whitespace from chat message
const user_command = user_msg.trim().toLowerCase();
// 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";
}
if (user_command.search("matanbot") != -1) {
return respondToMatanbotMention(user_info);
}
const parameters = user_msg.split(" ");
const command = parameters.shift().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";
}
if (user_command.search("matanbot") != -1) {
return respondToMatanbotMention(user_info);
}
const parameters = user_msg.split(" ");
const command = parameters.shift().toLowerCase();

if (command === "!background" || command === "!bg") {
return await changeGreenScreenBackground(parameters[0]);
}
if (command === "!background" || command === "!bg") {
return await changeGreenScreenBackground(parameters[0]);
}
}

module.exports = { message_main: message_main };
82 changes: 45 additions & 37 deletions obs_helper.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
require('dotenv').config();
const OBSWebSocket = require('obs-websocket-js');
require("dotenv").config();
const OBSWebSocket = require("obs-websocket-js");

const obs = new OBSWebSocket();

const port = process.env.OBS_PORT;
const pw = process.env.OBS_PW;

let current_scene; //keep current with OBS by listening to 'SwitchScenes' event
obs.on('SwitchScenes', (data) => {
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 => {
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');
current_scene = (await obs.send('GetCurrentScene')).name;
await obs.connect({ address: port, password: pw });
console.log("connected to obs websocket");
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("failed to connect to obs websocket");
console.log("OBS may not be open.");
console.log(err);
}
}
Expand All @@ -38,44 +38,48 @@ right after the camera, in this case 'green screen - pre'
*/
const switchGreenScreenBG = async (image_name) => {
try {
let green_screen_scene = await getScene('green screen effects');
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 => {
let image = green_screen_scene.sources.find((source) => {
return source.name === image_name;
});
let camera_order = green_screen_scene.sources.findIndex(source => {
return source.name === 'green screen - pre';

let camera_order = green_screen_scene.sources.findIndex((source) => {
return source.name === "green screen - pre";
});

re_ordered_sources = [];

//keep camera and layers above in place
for (i = 0; i <= camera_order; i++) {
re_ordered_sources.push({"name":green_screen_scene.sources[i].name});
re_ordered_sources.push({
name: green_screen_scene.sources[i].name,
});
}
//image we want is right below camera
re_ordered_sources.push({"name":image.name});
re_ordered_sources.push({ name: image.name });

//keep the rest of the order, don't include image twice
for (i = camera_order+1; i < green_screen_scene.sources.length; i++) {
for (i = camera_order + 1; i < green_screen_scene.sources.length; i++) {
if (green_screen_scene.sources[i] != image) {
re_ordered_sources.push({"name": green_screen_scene.sources[i].name});
re_ordered_sources.push({
name: green_screen_scene.sources[i].name,
});
}
}

await obs.send('ReorderSceneItems', {
"scene": green_screen_scene,
"items": re_ordered_sources
});
await obs.send("ReorderSceneItems", {
scene: green_screen_scene,
items: re_ordered_sources,
});
return true;
} catch (err) {
console.log(err);
throw(err);
throw err;
}
}
};

/*
OBS scene 'hearts helper' has multiple copies of the same media source
Expand All @@ -85,23 +89,23 @@ currently being shown.
video is loop of a heart eyes emoji, hence the naming
*/
const showHeartEyes = async () => {
const heart_scene = await getScene('hearts helper');
const heart_scene = await getScene("hearts helper");

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

module.exports = { switchGreenScreenBG: switchGreenScreenBG, connect: connect, showHeartEyes: showHeartEyes }
module.exports = {
switchGreenScreenBG: switchGreenScreenBG,
connect: connect,
showHeartEyes: showHeartEyes,
};
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "matanbot",
"version": "1.0.0",
"description": "Chat bot for matanjuggles social media.",
"main": "twitch_main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Matan Presberg",
"license": "MIT",
"dependencies": {
"obs-websocket-js": "^4.0.2",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.0"
}
"name": "matanbot",
"version": "1.0.0",
"description": "Chat bot for matanjuggles social media.",
"main": "twitch_main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Matan Presberg",
"license": "MIT",
"dependencies": {
"obs-websocket-js": "^4.0.2",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.0"
}
}
22 changes: 22 additions & 0 deletions timers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"timers": [
{
"message": "Find Matan on Instagram:\nhttps://instagram.com/matanjuggles/",
"time": 600,
"minLines": 9,
"prioritizeLines": false
},
{
"message": "Find Matan on Youtube:\nhttps://www.youtube.com/c/MatanPresberg",
"time": 550,
"minLines": 9,
"prioritizeLines": false
},
{
"message": "Hi! I'm matanbot, your friendly neighborhood twitch bot. You can clap or try !background to interact with me.",
"time": 1000,
"minLines": 30,
"prioritizeLines": true
}
]
}
Loading