From 6072cb0e8bf03dc819050f0f31a5bd8807c34353 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Thu, 25 Aug 2011 17:54:59 +0100 Subject: [PATCH] Chance in in snippets, valMatch. Reload snippets file. Change youare. --- modules/admin.js | 2 +- modules/youare.js | 5 ++--- run.js | 5 +++++ snippets.js | 21 +++++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/modules/admin.js b/modules/admin.js index 16cd18a..cf6cb75 100644 --- a/modules/admin.js +++ b/modules/admin.js @@ -34,7 +34,7 @@ var adminCommands = function(dbot) { 'unload': function(data, params) { console.log(dbot.moduleNames); if(dbot.moduleNames.include(params[1])) { - dbot.moduleNames[params[1]] = null; + dbot.moduleNames[params[1]] = undefined; dbot.reloadModules(); dbot.say(data.channel, 'Turned off module: ' + params[1]); } else { diff --git a/modules/youare.js b/modules/youare.js index e950264..ab4d228 100644 --- a/modules/youare.js +++ b/modules/youare.js @@ -3,10 +3,9 @@ var youAre = function(dbot) { return { 'listener': function(data) { - var num = Math.floor(Math.random()*4); - var key = data.message.match(/(is|are) ([\d\w\s']*)/); + var key = data.message.valMatch(/(is|are) ([\d\w\s']*)/, 3); - if(num == 1 && key != undefined) { + if(Number.prototype.chanceIn(1, 3) && key) { if(key[2].indexOf('and') !== -1) { key[2] = key[2].split('and')[0]; } // TODO: fix the regex to do this. i hate regex diff --git a/run.js b/run.js index 3218685..5eeca27 100644 --- a/run.js +++ b/run.js @@ -35,6 +35,11 @@ DBot.prototype.reloadModules = function() { this.rawModules = []; this.modules = []; + // Reload snippets + var path = require.resolve('./snippets'); + require.cache[path] = undefined; + require('./snippets'); + this.moduleNames.each(function(name) { var cacheKey = require.resolve('./modules/' + name); require.cache[cacheKey] = undefined; // TODO: snippet to remove element properly diff --git a/snippets.js b/snippets.js index 6746548..d357efb 100644 --- a/snippets.js +++ b/snippets.js @@ -29,6 +29,15 @@ Array.prototype.include = function(value) { /*** String ***/ +String.prototype.valMatch = function(regex, expLength) { + var key = this.match(regex); + if(key !== null && key.length == expLength) { + return key; + } else { + return false; + } +}; + String.prototype.endsWith = function(needle) { var end = this.slice(this.length - needle.length); if(needle === end) { @@ -64,3 +73,15 @@ Object.prototype.isArray = function(obj) { return false; } }; + +/*** Integer ***/ + +Number.prototype.chanceIn = function(x, y) { + var num = Math.floor(Math.random() * (y + 1)) / x; + console.log(num); + if(num == 1) { + return true; + } else { + return false; + } +};