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

Mod add command #13

Merged
merged 6 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
mods can add commands, commands auto count usages
  • Loading branch information
matanp committed Jan 25, 2021
commit a6862275fbffa810d4bbb5d51b0ce643cc525f14
102 changes: 77 additions & 25 deletions src/backend/bot_brain.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
"use strict";
const fs = require("fs");
const obs = require("./obs_helper.js");

let matanbot_mention_count = 0;
let mlk_quote_num = 0;

const commands_json = fs.readFileSync("commands.json");
let added_commands = JSON.parse(commands_json).commands;
function saveCommands(commands) {
let save_commands_json = {
"commands": commands
}

fs.writeFileSync("commands.json", JSON.stringify(save_commands_json));
}

function respondToMatanbotMention(user_info) {
//random number 1 to 10
const random_num = Math.floor(Math.random() * 10 + 1);
Expand Down Expand Up @@ -58,12 +69,34 @@ const changeGreenScreenBackground = async (image_request) => {

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

function mlk_quote() {
const quotes = [
"I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident that all men are created equal. I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character. I have a dream today.",
"The ultimate measure of a man is not where he stands in moments of comfort and convenience, but where he stands at times of challenge and controversy.",
"Injustice anywhere is a threat to justice everywhere.",
"The time is always right to do what is right.",
"I have decided to stick with love. Hate is too great a burden to bear.",
"Darkness can not drive out darkness; only light can do that. Hate cannot drive out hate; only love can do that.",
"Nothing in the world is more dangerous than sincere ignorance and conscientious stupidity.",
"There can be no deep disappointment where there is not deep love.",
"I have a dream that one day this nation will rise up and live out the true meaning of its creed; We hold these truths to be self-evident: that all men are created equal",
"Morality cannot be legislated, but behavior can be regulated. Judicial decrees may not change the heart, but they can restrain the heartless.",
"Everybody can be great... because anybody can serve. You don't have to have a college degree to serve. You don't have to make your subject and verb agree to serve. you only need a heart full of grace. a soul generated by love.",
"He who passively accepts evil is as much involved in it as he who helps to perpetrate it.",
"Riots are the voices of the unheard.",
"I must confess that over the past few years I have been gravely disappointed with the white moderate. I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to 'order' than to justice; who prefers a negative peace which is the absence of tension to a positive peace which is the presence of justice [...]",
];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a file consts.js and move this to the file as mlk_quotes.

Rename mlk_quote() to get_mlk_quote(). It's a standard (at least in python) to start functions with their action verb (since functions tend to perform actions).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of the reason why a common foo function is do_something()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, if you have generic helper methods (not sure if you do), you can stick them in a utils.js file. very helpful


mlk_quote_num = (mlk_quote_num + 1) % quotes.length;
return `${quotes[mlk_quote_num]} - Martin Luther King Jr.`;
}

// 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();
let user_command = user_msg.trim().toLowerCase();

if (user_command.search("clap") != -1) {
obs.showHeartEyes();
Expand All @@ -75,34 +108,53 @@ const message_main = async (user_info, user_msg) => {
return respondToMatanbotMention(user_info);
}

const user_parameters = user_msg.split(" ");
user_command = user_parameters.shift().toLowerCase();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid recycling var names, it makes debugging harder

const mod_privileges = user_info.mod || user_info.badges.broadcaster === `1`;

const parameters = user_msg.split(" ");
const command = parameters.shift().toLowerCase();
if (user_command === "!background" || user_command === "!bg") {
return await changeGreenScreenBackground(user_parameters[0]);
}

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

if (command === '!mlk') {
const quotes = [
"I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident that all men are created equal. I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character. I have a dream today.",
"The ultimate measure of a man is not where he stands in moments of comfort and convenience, but where he stands at times of challenge and controversy.",
"Injustice anywhere is a threat to justice everywhere.",
"The time is always right to do what is right.",
"I have decided to stick with love. Hate is too great a burden to bear.",
"Darkness can not drive out darkness; only light can do that. Hate cannot drive out hate; only love can do that.",
"Nothing in the world is more dangerous than sincere ignorance and conscientious stupidity.",
"There can be no deep disappointment where there is not deep love.",
"I have a dream that one day this nation will rise up and live out the true meaning of its creed; We hold these truths to be self-evident: that all men are created equal",
"Morality cannot be legislated, but behavior can be regulated. Judicial decrees may not change the heart, but they can restrain the heartless.",
"Everybody can be great... because anybody can serve. You don't have to have a college degree to serve. You don't have to make your subject and verb agree to serve. you only need a heart full of grace. a soul generated by love.",
"He who passively accepts evil is as much involved in it as he who helps to perpetrate it.",
"Riots are the voices of the unheard.",
"I must confess that over the past few years I have been gravely disappointed with the white moderate. I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to 'order' than to justice; who prefers a negative peace which is the absence of tension to a positive peace which is the presence of justice [...]"
];

mlk_quote_num = (mlk_quote_num + 1) % quotes.length;
return `${quotes[mlk_quote_num]} - Martin Luther King Jr.`;
if (mod_privileges && (user_command === `!add` || user_command === `!addCommand`)) {
if (user_parameters === null) {
return `Mods can add a command with !add {command} {response} {+m for mod only}`;
}

const now = new Date(Date.now());
const mod_only = (user_parameters[user_parameters.length - 1] === `+m`);
if (mod_only) { user_parameters.pop() }

const new_command = {
"command_word": user_parameters.shift(),
"response": user_parameters.join(` `),
"mod_only": mod_only,
"added_by": user_info['display-name'],
"added_timestamp": `${now.getMonth()+1}-${now.getDate()}, ${now.getFullYear()}`,
"usage_count": 0
};

added_commands.push(new_command);
saveCommands(added_commands);
}

for (let added_command of added_commands) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a short comment describing this loop, cause it's not obvious at first glance

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably make this a commands.js function, maybe named getCommandInfo()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of looping through every persisted command and running the following logic, you can make your mods, count, and age checks first, and then retrieve the relevant command from the persisted set (if it exists)

this 1) makes you code run a modicum faster 2) removes a layer of nesting indents 3) i'd argue makes it a little more readable

if(user_command === `!${added_command.command_word}`) {
if (!added_command.mod_only || mod_privileges) {
if (user_parameters[0] === `count`) {
return `${user_command} has been used ${added_command.usage_count} times.`;
}

added_command.usage_count = added_command.usage_count + 1;
saveCommands(added_commands);

return added_command.response;
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/backend/commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"commands":[{"command_word":"hello","response":"world","mod_only":false,"added_by":"matanjuggles","added_timestamp":"1-25, 2021","usage_count":3}]}