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

starting work on propify #10

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
'rules': {
"no-console": "off",
'linebreak-style': [
1,
'unix'
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ private/*
!private/README.md
!private/get.js
.DS_Store
tool/*.json
tool/temp.js
tools/*.json
npm-debug.log
.vscode

Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ The Commands of the bot are its API so here's how it will adhere to Semantic Ver
**MINOR** - Adding new commands or functionality that won't affect or break existing ones
**PATCH** - Bug fixes, code improvements (backwards-compatible bug fixes)

## [2.0.0]

Big internal rewrite
- replaced deprecated `request` library with [got](https://github.com/sindresorhus/got#json-mode)
- Added more unit/integration tests (still working on that though)
- updated the code to modern ES (mostly) and using more async await
- Transforming data from API before using it to make handling API changes easier

## Breaking
- `!cat` - the api no longer works so removing this and will eventually replace it using the trigger code
- Completely re-wrote how prop and flow points are added to triggers. Instead of `+prop` and `+flow`, there's now `!propify` and `!flowify` commands. It updates the trigger object in the database instead of relying on parsing.

## [1.24.0] - 2019-07-18
## Fixed
- updated internal dependencies
Expand Down
82 changes: 44 additions & 38 deletions bot/commands/bot/admin.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,93 @@
'use strict';
const _private = require(process.cwd() + '/private/get');
"use strict";
const _private = require(process.cwd() + "/private/get");
const settings = _private.settings;
var historyStore = require(process.cwd()+ '/bot/store/history.js');
const historyStore = require(process.cwd() + "/bot/store/history.js");
// const nmm = require(process.cwd()+ '/bot/store/nmm.js');
// const ff = require(process.cwd()+ '/bot/store/ff.js');
var _ = require('lodash');
// const _ = require("lodash");

function hasPermission(bot, user) {
if (!user || !user.id) {
bot.sendChat('unrecognized user id, try again');
bot.sendChat("unrecognized user id, try again");
return false;
}

// if not at least a MOD, GTFO!
if (!bot.havePermission(user.id, bot.ROOM_ROLE.MANAGER)) {
bot.sendChat('Sorry I only take admin commands from managers and above');
bot.sendChat("Sorry I only take admin commands from managers and above");
return false;
}

return true;
}

module.exports = function(bot, db, data) {
if (typeof bot !== 'object' || typeof data !== 'object') {
/**
* !admin - perfom specific admin actions
* @param {PlugApi} bot
* @param {object} db
* @param {import('../../utilities/typedefs').BotCommand} data
*/
module.exports = function (bot, db, data) {
if (typeof bot !== "object" || typeof data !== "object") {
return;
}

const { user } = data;
if (!hasPermission(bot,user)) return;

if (!hasPermission(bot, user)) return;

// now we can assume all chats are from owner

// if messages was just '!admin' without a any arguments
if (data.args.length === 0) {
bot.sendChat('What would you like me to do master?');
bot.sendChat("What would you like me to do master?");
return;
}
var command = data.args[0];

const [ command, ...extra ] = data.args;

// now to go through possible commands
// !admin command extra stuff
var extra = data.args.slice(1);
switch(command) {
case 'restart':
bot.sendChat(':recycle: brb! :recycle:');
setTimeout(process.exit, 1500);
// !admin command extra stuff
switch (command) {
case "restart":
bot.sendChat(":recycle: brb! :recycle:");
setTimeout(process.exit, 500);
break;
case 'reconnect':
bot.sendChat(':phone: Redialing, brb! :computer:');
case "reconnect":
bot.sendChat(":phone: Redialing, brb! :computer:");
bot.close(true);
setTimeout(function(){
bot.connect(settings.ROOMNAME);
setTimeout(function () {
bot.connect(settings.ROOMNAME);
}, 5000);
break;
case 'mute':
case "mute":
if (!bot.myconfig.muted) {
bot.sendChat('I shut up now :speak_no_evil:');
bot.sendChat("I shut up now :speak_no_evil:");
bot.oldSendChat = bot.sendChat;
bot.myconfig.muted = true;
bot.sendChat = function(x){return;};
bot.sendChat = function (x) {
return;
};
}
break;
case 'history':
bot.sendChat('ok, updating my room playlist history');
case "history":
bot.sendChat("ok, updating my room playlist history");
historyStore.init(bot);
break;
case 'refresh':
bot.sendChat('refreshing all spreadsheet data');
// nmm.load(bot);
// ff.load(bot);
break;
case 'unmute':
// case "refresh":
// bot.sendChat("refreshing all spreadsheet data");
// nmm.load(bot);
// ff.load(bot);
// break;
case "unmute":
if (bot.myconfig.muted) {
bot.sendChat = bot.oldSendChat;
bot.myconfig.muted = false;
bot.sendChat('It\'s good to be back :loudspeaker:');
bot.sendChat("It's good to be back :loudspeaker:");
}
break;
// more commands to come
default:
break;
break;
}

};
14 changes: 10 additions & 4 deletions bot/commands/bot/shuffleplaylist.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ function hasPermission(bot, user) {
return true;
}

/**
* !shuffleplaylist - shuffles the bot's playlist
* @param {PlugApi} bot
* @param {object} db
* @param {import('../../utilities/typedefs').BotCommand} data
*/
module.exports = function(bot, db, data) {
return bot.sendChat("This command is currently disabled");

const { user } = data;

if (!hasPermission(bot,user)) return;
Expand All @@ -24,7 +32,7 @@ module.exports = function(bot, db, data) {
bot.myconfig.playlistID,
function(code, _data){
if (code === 200) {
bot.sendChat('Ok I shuffled my one and only playlist.');
bot.sendChat('Ok I shuffled my playlist.');
} else {
bot.log('error','BOT', `Could not shuffle playlist - code: ${code}`);
bot.sendChat("Got an errror shuffling playlist for some reasons (probably API error), try again.");
Expand All @@ -33,6 +41,4 @@ module.exports = function(bot, db, data) {
);


};

module.exports.extraCommands = ['sp'];
};
8 changes: 7 additions & 1 deletion bot/commands/bot/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ function hasPermission(bot, user) {
return true;
}

/**
* !toggle configItem - toggles boolean config values
* @param {PlugApi} bot
* @param {object} db
* @param {import('../../utilities/typedefs').BotCommand} data
*/
module.exports = function(bot, db, data) {
if (typeof bot !== 'object' || typeof data !== 'object') {
return;
Expand All @@ -24,7 +30,7 @@ module.exports = function(bot, db, data) {
if (!hasPermission(bot,user)) return;

// config item to toggle
var item = data.args[0];
const [ item ] = data.args;

if (!item) {
bot.sendChat(`You must provide a config item to toggle. See full list here: http://franciscog.com/DerpyBot/commands/#config`);
Expand Down
6 changes: 3 additions & 3 deletions bot/commands/bot/version.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
var pkg = require(process.cwd() + '/package.json');
const pkg = require(process.cwd() + '/package.json');

/**
* Displays the current version of the bot listed inthe pacakge.json
* @param {Object} bot Dubapi instance
* Displays the current version of the bot listed in the pacakge.json
* @param {object} bot Dubapi instance
*/
module.exports = function(bot) {
return bot.sendChat(bot.getUser().username + ' version: ' + pkg.version);
Expand Down
41 changes: 26 additions & 15 deletions bot/commands/credits/balance.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';
var repo = require(process.cwd()+'/repo');
const repos = require('../../../repos');

function sayMyBalance(bot, user) {
var flowS = user.flow > 1 || user.flow <= 0 ? 's' : '';
var propS = user.props > 1 || user.props <= 0 ? 's' : '';
const flowS = user.flow > 1 || user.flow <= 0 ? 's' : '';
const propS = user.props > 1 || user.props <= 0 ? 's' : '';

bot.sendChat(`@${user.username} you have ${user.props} prop${propS} :fist: and ${user.flow} flowpoint${flowS} :surfer:`);
}

function sayTheirBalance(bot, whoAsked, user) {
var flowS = user.flow > 1 || user.flow <= 0 ? 's' : '';
var propS = user.props > 1 || user.props <= 0 ? 's' : '';
const flowS = user.flow > 1 || user.flow <= 0 ? 's' : '';
const propS = user.props > 1 || user.props <= 0 ? 's' : '';

bot.sendChat(`@${whoAsked}, the user @${user.username} has ${user.props} prop${propS} :fist: and ${user.flow} flowpoint${flowS} :surfer:`);
}

async function lookUpBalance(bot, db, whoAsked, whoFor, which){
async function lookUpBalance(bot, db, whoAsked, whoForId, which){
try {
const user = await repo.findUserById(db, whoFor.id);
const user = await repos.users.findUserById(db, whoForId);
if (user !== null) {
if (user.props === void(0) || user.props === null) { user.props = 0; }
if (user.flow === void(0) || user.flow === null) { user.flow = 0; }
Expand All @@ -28,17 +28,27 @@ async function lookUpBalance(bot, db, whoAsked, whoFor, which){
sayTheirBalance(bot, whoAsked, user);
}
} else {
bot.sendChat(`Strange, data for that was not found!`);
bot.sendChat(`Strange, data for that person was not found!`);
}
} catch (e) {
bot.sendChat(`Strange, data for that was not found!`);
bot.sendChat(`Strange, data for that person was not found!`);
}
}

module.exports = function(bot, db, data) {

/**
* !balance - shows Prop and Flow points for user who sent command
* !balance @username - shows points for another user
* @param {import('plugapi')} bot
* @param {import('firebase-admin').database.Database} db
* @param {import('../../utilities/typedefs').BotCommand} data
*/
function balance(bot, db, data) {
if (!bot || !db || !data) {
return;
}

if (data.args.length === 0) {
return lookUpBalance(bot, db, data.user.username, data.user, 'mine');
return lookUpBalance(bot, db, data.user.username, data.user.id, 'mine');
}

if (data.args.length > 1) {
Expand All @@ -49,15 +59,16 @@ module.exports = function(bot, db, data) {
return bot.sendChat(`@${data.user.username}, use '!balance @[username]' to check another user's balance`);
}

const recipient = data.mentions[0];
const [recipient] = data.mentions;

if (!recipient){
bot.sendChat(`@${data.user.username}, that user was not found!`);
return;
}

return lookUpBalance(bot, db, data.user.username, recipient, 'theirs');
return lookUpBalance(bot, db, data.user.username, recipient.id, 'theirs');

};
}

module.exports = balance;
module.exports.extraCommands = ['stats', 'score'];
31 changes: 15 additions & 16 deletions bot/commands/credits/leaders.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
/**
* !leaders
* returns the top 3 props and flow leaders
*/
'use strict';
const leaderUtils = require(process.cwd() + '/bot/utilities/leaderUtils.js');
const moment = require('moment');
const _get = require('lodash/get');
const handleChat = require("../../utilities/handleChat");

/**
* !leaders - display current leaders in prop and flow
* @param {PlugApi} bot
* @param {object} db
* @param {import('../../utilities/typedefs').BotCommand} data
*/
module.exports = function(bot, db, data) {
if (!bot.leaderboard) {
bot.sendChat("I don't have leader informtaion at the momemt but try again in a minute");
return;
}

const param1 = _get(data, 'params[0]');
const param2 = _get(data, 'params[1]');
const [param1, param2] = data.args;

// get all time leaders
if (param1 && param1 === "all") {
var all_time = leaderUtils.allTimeLeaders(bot);
const all_time = leaderUtils.allTimeLeaders(bot);

var propString = all_time.props.reduce(function(a,v){
const propString = all_time.props.reduce(function(a,v){
return a += `${v[0]} (${v[1]}), `;
}, '').replace(/, $/, '');

var flowString = all_time.flows.reduce(function(a,v){
const flowString = all_time.flows.reduce(function(a,v){
return a += `${v[0]} (${v[1]}), `;
}, '').replace(/, $/, '');

handleChat(
return handleChat(
[
`All time prop leaders are:`,
propString,
`All time flow leaders are:`,
flowString
]
);
return;
}

if (param1 && param2) {
var monthResult = leaderUtils.getLeadersByMonthYear(bot, param1, param2);
const monthResult = leaderUtils.getLeadersByMonthYear(bot, param1, param2);

let monthInfo = `The leaders for ${param1} ${param2} were:
*props*: ${monthResult.props}
Expand All @@ -51,9 +50,9 @@ module.exports = function(bot, db, data) {
return;
}

var year = moment().format('Y');
var month = moment().format('MMM');
var month_full = moment().format('MMMM');
const year = moment().format('Y');
const month = moment().format('MMM');
const month_full = moment().format('MMMM');

const chat_msg = [
`Current leaders for ${month_full} ${year} are:`
Expand Down
Loading