Skip to content
This repository was archived by the owner on Jul 17, 2020. It is now read-only.

Commit ecd975f

Browse files
author
Zirak
committed
extracted logic from /parse into bot.parseMacro (related to #149)
1 parent 6a99c26 commit ecd975f

File tree

4 files changed

+114
-111
lines changed

4 files changed

+114
-111
lines changed

source/bot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ bot.beatInterval = 5000; //once every 5 seconds is Good Enough ™
556556
//#build eval.js
557557

558558
//#build parseCommandArgs.js
559+
//#build parseMacro.js
559560
//#build suggestionDict.js
560561

561562
//#build commands.js

source/commands.js

Lines changed: 5 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -282,119 +282,15 @@ return function ( args ) {
282282

283283
commands.eval.async = commands.coffee.async = true;
284284

285-
var parse = commands.parse = (function () {
286-
var macros = {
287-
who : function ( msgObj ) {
288-
return msgObj.get( 'user_name' );
289-
},
290-
291-
someone : function () {
292-
var presentUsers = document.getElementById( 'sidebar' )
293-
.getElementsByClassName( 'present-user' );
294-
295-
//the chat keeps a low opacity for users who remained silent for long,
296-
// and high opacity for those who recently talked
297-
var active = [].filter.call( presentUsers, function ( user ) {
298-
return Number( user.style.opacity ) >= 0.5;
299-
}),
300-
user = active[ Math.floor(Math.random() * (active.length-1)) ];
301-
302-
if ( !user ) {
303-
return 'Nobody';
304-
}
285+
commands.parse = function ( args ) {
286+
var msgObj = args.get(),
287+
user = bot.users[ args.get('user_id') ],
305288

306-
return user.getElementsByTagName( 'img' )[ 0 ].title;
307-
},
308-
309-
digit : function () {
310-
return Math.floor( Math.random() * 10 );
311-
},
312-
313-
encode : function ( msgObj, string ) {
314-
return encodeURIComponent( string );
315-
},
316-
317-
//random number, min <= n <= max
318-
//treats non-numeric inputs like they don't exist
319-
rand : function ( msgObj, min, max ) {
320-
min = Number( min );
321-
max = Number( max );
322-
return Math.rand( min, max );
323-
}
324-
};
325-
var macroRegex = /(?:.|^)\$(\w+)(?:\((.*?)\))?/g;
326-
327-
//extraVars is for internal usage via other commands
328-
return function parse ( args, extraVars ) {
329-
var isMsg = !!args.get,
330-
//filler objects, solves
331-
// https://github.com/Zirak/SO-ChatBot/issues/66
332-
msgObj = isMsg ? args.get() : {},
333-
user = isMsg ? bot.users[ args.get('user_id') ] : {};
334-
335-
extraVars = extraVars || {};
289+
extraVars = Object.merge( msgObj, user );
336290
bot.log( args, extraVars, '/parse input' );
337291

338-
return args.replace( macroRegex, replaceMacro );
339-
340-
function replaceMacro ( $0, filler, fillerArgs ) {
341-
//$$ makes a literal $
342-
if ( $0.startsWith('$$') ) {
343-
return $0.slice( 1 );
344-
}
345-
346-
//include the character that was matched in the $$ check, unless
347-
// it's a $
348-
var ret = '';
349-
if ( $0[0] !== '$' ) {
350-
ret = $0[ 0 ];
351-
}
352-
353-
var macro = findMacro( filler );
354-
355-
//not found? bummer.
356-
if ( !macro ) {
357-
return filler;
358-
}
359-
360-
bot.log( macro, filler, fillerArgs, '/parse replaceMacro' );
361-
//when the macro is a function
362-
if ( macro.apply ) {
363-
ret += macro.apply( null, parseMacroArgs(fillerArgs) );
364-
}
365-
//when the macro is simply a substitution
366-
else {
367-
ret += macro;
368-
}
369-
return ret;
370-
}
371-
372-
function parseMacroArgs ( macroArgs ) {
373-
bot.log( macroArgs, '/parse parseMacroArgs' );
374-
if ( !macroArgs ) {
375-
return [ args ];
376-
}
377-
378-
//parse the arguments, split them into individual arguments,
379-
// and trim'em (to cover the case of "arg,arg" and "arg, arg")
380-
return (
381-
[ args ].concat(
382-
parse( macroArgs, extraVars )
383-
.split( ',' ).invoke( 'trim' ) ) );
384-
//this is not good code
385-
}
386-
387-
function findMacro ( macro ) {
388-
var container = [ macros, msgObj, user, extraVars ].first( hasMacro );
389-
390-
return ( container || {} )[ macro ];
391-
392-
function hasMacro ( obj ) {
393-
return obj && obj.hasOwnProperty( macro );
394-
}
395-
}
292+
return bot.parseMacro( args.toString(), extraVars );
396293
};
397-
}());
398294

399295
commands.tell = (function () {
400296
var invalidCommands = { tell : true, forget : true };

source/parseMacro.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//(function () {
2+
"use strict";
3+
4+
var macros = {
5+
who : function ( msgObj ) {
6+
return msgObj.get( 'user_name' );
7+
},
8+
9+
someone : function () {
10+
var presentUsers = document.getElementById( 'sidebar' )
11+
.getElementsByClassName( 'present-user' );
12+
13+
//the chat keeps a low opacity for users who remained silent for long,
14+
// and high opacity for those who recently talked
15+
var active = [].filter.call( presentUsers, function ( user ) {
16+
return Number( user.style.opacity ) >= 0.5;
17+
}),
18+
user = active[ Math.floor(Math.random() * (active.length-1)) ];
19+
20+
if ( !user ) {
21+
return 'Nobody';
22+
}
23+
24+
return user.getElementsByTagName( 'img' )[ 0 ].title;
25+
},
26+
27+
digit : function () {
28+
return Math.floor( Math.random() * 10 );
29+
},
30+
31+
encode : function ( msgObj, string ) {
32+
return encodeURIComponent( string );
33+
},
34+
35+
//random number, min <= n <= max
36+
//treats non-numeric inputs like they don't exist
37+
rand : function ( msgObj, min, max ) {
38+
min = Number( min );
39+
max = Number( max );
40+
return Math.rand( min, max );
41+
}
42+
};
43+
var macroRegex = /(?:.|^)\$(\w+)(?:\((.*?)\))?/g;
44+
45+
bot.parseMacro = function parse ( source, extraVars ) {
46+
return source.replace( macroRegex, replaceMacro );
47+
48+
function replaceMacro ( $0, filler, fillerArgs ) {
49+
//$$ makes a literal $
50+
if ( $0.startsWith('$$') ) {
51+
return $0.slice( 1 );
52+
}
53+
54+
//include the character that was matched in the $$ check, unless
55+
// it's a $
56+
var ret = '';
57+
if ( $0[0] !== '$' ) {
58+
ret = $0[ 0 ];
59+
}
60+
61+
var macro = findMacro( filler );
62+
63+
//not found? bummer.
64+
if ( !macro ) {
65+
return filler;
66+
}
67+
68+
bot.log( macro, filler, fillerArgs, '/parse replaceMacro' );
69+
//when the macro is a function
70+
if ( macro.apply ) {
71+
ret += macro.apply( null, parseMacroArgs(fillerArgs) );
72+
}
73+
//when the macro is simply a substitution
74+
else {
75+
ret += macro;
76+
}
77+
return ret;
78+
}
79+
80+
function parseMacroArgs ( macroArgs ) {
81+
bot.log( macroArgs, '/parse parseMacroArgs' );
82+
if ( !macroArgs ) {
83+
return [ source ];
84+
}
85+
86+
//parse the arguments, split them into individual arguments,
87+
// and trim'em (to cover the case of "arg,arg" and "arg, arg")
88+
return (
89+
[ source ].concat(
90+
parse( macroArgs, extraVars )
91+
.split( ',' ).invoke( 'trim' ) ) );
92+
//this is not good code
93+
}
94+
95+
function findMacro ( macro ) {
96+
var container = [ macros, extraVars ].first( hasMacro );
97+
98+
return ( container || {} )[ macro ];
99+
100+
function hasMacro ( obj ) {
101+
return obj && obj.hasOwnProperty( macro );
102+
}
103+
}
104+
};
105+
106+
107+
//})();

source/plugins/learn.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(function () {
22
"use strict";
3-
var parse = bot.getCommand( 'parse' );
43
var storage = bot.memory.get( 'learn' );
54

65
var replyPatterns = /^(<>|<user>|<msg>)/i,
@@ -87,7 +86,7 @@ function makeCustomCommand ( command ) {
8786
return mismatchErrMessage.supplant( command );
8887
}
8988

90-
var res = parse.exec( cmdArgs, parts );
89+
var res = bot.parseMacro( cmdArgs, parts );
9190

9291
switch ( replyMethod ) {
9392
case '':

0 commit comments

Comments
 (0)