support for ankhbot commands, static commands, cooldowns, implemented fun/ham facts

This commit is contained in:
Chris Ham
2018-01-22 18:32:09 -08:00
parent be5d8bc2ec
commit a2a9974108
10 changed files with 249 additions and 15 deletions

49
lib/ankhbot-commands.js Executable file
View File

@@ -0,0 +1,49 @@
module.exports = {
exists: exists,
get: get
};
const fs = require('fs'),
path = require('path');
const cmdPrefix = '!';
const commandsFilePath = path.join(__dirname, '..', 'conf', 'ghbot.abcomg');
let commands = parseCommands(commandsFilePath);
fs.watchFile(commandsFilePath, (curr, prev) => {
if (curr.mtime !== prev.mtime) {
commands = require(commandsFilePath);
}
});
function exists(command)
{
let matches = commands.filter(obj => { return obj.Command === cmdPrefix+command; });
if (matches.length > 0) {
return true;
} else {
return false;
}
}
function get(command)
{
let matches = commands.filter(obj => { return obj.Command === cmdPrefix+command; });
if (matches.length > 0) {
let match = matches[0];
if (match.Enabled === true) {
return matches[0].Response;
} else {
return undefined;
}
} else {
return undefined;
}
}
// Read in the array of objects exported from AnkhBot
function parseCommands(filePath)
{
let data = fs.readFileSync(filePath, 'utf-8');
let commands = eval(data);
return commands;
}

48
lib/cooldowns.js Executable file
View File

@@ -0,0 +1,48 @@
module.exports = {
get: isOnCooldown,
set: placeOnCooldown
};
const memcache = require('memcache'),
md5 = require('md5'),
keyPrefix = 'cd';
const cache = new memcache.Client();
cache.on('error', console.error);
cache.connect();
// Given a cooldownTime in seconds and a command, returns false if the command is not on cooldown
// returns the time in seconds until the command will be ready again otherwise
function isOnCooldown(command, cooldownTime, callback)
{
return new Promise((resolve, reject) => {
let now = Date.now();
let onCooldown = false;
let key = keyPrefix + md5(command);
cache.get(key, function(err, timeUsed) {
if (err) reject(err);
if (timeUsed !== null) {
// Command was recently used, check timestamp to see if it's on cooldown
if ((now - timeUsed) <= (cooldownTime*1000)) {
// Calculate how much longer it's on cooldown
onCooldown = ((cooldownTime*1000) - (now - timeUsed))/1000;
}
}
resolve(onCooldown);
});
});
}
// Places a command on cooldown for cooldownTime (in seconds)
function placeOnCooldown(command, cooldownTime)
{
let key = keyPrefix + md5(command);
return cache.set(key, Date.now(), handleCacheSet, cooldownTime);
}
function handleCacheSet(error, result) {}
process.on('exit', (code) => {cache.close()});

53
lib/static-commands.js Executable file
View File

@@ -0,0 +1,53 @@
module.exports = {
exists: exists,
get: get
};
const fs = require('fs'),
path = require('path');
const commandsFilePath = path.join(__dirname, '..', 'conf', 'text_commands');
// Read in basic text commands / definitions and watch for changes
let commands = parseCommands(commandsFilePath);
fs.watchFile(commandsFilePath, (curr, prev) => {
if (curr.mtime !== prev.mtime) {
commands = parseCommands(commandsFilePath);
}
});
// Read/parse text commands from the "database"
function parseCommands(filePath)
{
let commands = {};
let data = fs.readFileSync(filePath, 'utf-8');
let commandLines = data.toString().split('\n');
let commandParts;
let aliases;
commandLines.forEach(function(line) {
if (line.length > 0 && line.indexOf('|') !== -1) {
commandParts = line.split('|');
// check for aliases
aliases = commandParts[0].split(',');
aliases.forEach(function(cmd) {
commands[cmd] = commandParts[1];
//console.log(`!${cmd},`);
});
}
});
return commands;
}
function exists(command)
{
return commands.hasOwnProperty(command);
}
function get(command)
{
if (exists(command)) {
return commands[command];
} else {
return undefined;
}
}