3
3
var cache = { } ;
4
4
5
5
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 ) {
7
14
return 'Y U NO PROVIDE ARGUMENTS!?' ;
8
15
}
9
16
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 ] ) ;
12
28
}
13
29
30
+ bot . log ( query , resultIndex , '/urban input' ) ;
31
+
14
32
IO . jsonp ( {
15
33
url : 'http://api.urbandictionary.com/v0/define' ,
16
34
data : {
17
- term : args . content
35
+ term : query
18
36
} ,
19
37
jsonpName : 'callback' ,
20
38
fun : complete
@@ -24,10 +42,14 @@ function urban ( args, cb ) {
24
42
var msg ;
25
43
26
44
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 ( ) ) ;
28
50
}
29
51
else {
30
- msg = formatTop ( resp . list [ 0 ] ) ;
52
+ msg = formatTop ( resp . list [ resultIndex ] ) ;
31
53
}
32
54
cache [ args ] = msg ;
33
55
@@ -64,7 +86,8 @@ bot.addCommand({
64
86
65
87
permissions : { del : 'NONE' , use : 'ALL' } ,
66
88
67
- description : 'Fetches UrbanDictionary definition. `/urban something`' ,
89
+ description : 'Fetches UrbanDictionary definition. ' +
90
+ '`/urban query [index=0]`' ,
68
91
async : true
69
92
} ) ;
70
93
0 commit comments