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

Commit cc5f14a

Browse files
author
Zirak
committed
added resultIndex parameter for /urban (#144)
1 parent 7c5498b commit cc5f14a

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

source/plugins/urban.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,36 @@
33
var cache = {};
44

55
function urban ( args, cb ) {
6-
if ( !args.length ) {
6+
if ( cache[args] ) {
7+
return finish( cache[args] );
8+
}
9+
10+
var parts = args.parse(),
11+
query, resultIndex;
12+
13+
if ( !parts.length ) {
714
return 'Y U NO PROVIDE ARGUMENTS!?';
815
}
916

10-
if ( cache[args] ) {
11-
return finish( cache[args] );
17+
// /urban query in several words
18+
if ( isNaN(parts[1]) ) {
19+
bot.log( '/urban input isNaN' );
20+
query = args.toString();
21+
resultIndex = 0;
22+
}
23+
// /urban query index
24+
else {
25+
bot.log( '/urban input isn\'t NaN' );
26+
query = parts[ 0 ];
27+
resultIndex = Number( parts[1] );
1228
}
1329

30+
bot.log( query, resultIndex, '/urban input' );
31+
1432
IO.jsonp({
1533
url : 'http://api.urbandictionary.com/v0/define',
1634
data : {
17-
term : args.content
35+
term : query
1836
},
1937
jsonpName : 'callback',
2038
fun : complete
@@ -24,10 +42,14 @@ function urban ( args, cb ) {
2442
var msg;
2543

2644
if ( resp.result_type === 'no_results' ) {
27-
msg = 'No definition found for ' + args;
45+
msg = 'No definition found for ' + query;
46+
}
47+
else if ( resultIndex > resp.list.length ) {
48+
msg = 'Nothing in that index. The last one is:\n' +
49+
formatTop( resp.list.pop() );
2850
}
2951
else {
30-
msg = formatTop( resp.list[0] );
52+
msg = formatTop( resp.list[resultIndex] );
3153
}
3254
cache[ args ] = msg;
3355

@@ -64,7 +86,8 @@ bot.addCommand({
6486

6587
permissions : { del : 'NONE', use : 'ALL' },
6688

67-
description : 'Fetches UrbanDictionary definition. `/urban something`',
89+
description : 'Fetches UrbanDictionary definition. ' +
90+
'`/urban query [index=0]`',
6891
async : true
6992
});
7093

0 commit comments

Comments
 (0)