@@ -6598,18 +6598,36 @@ bot.addCommand({
6598
6598
var cache = { } ;
6599
6599
6600
6600
function urban ( args , cb ) {
6601
- if ( ! args . length ) {
6601
+ if ( cache [ args ] ) {
6602
+ return finish ( cache [ args ] ) ;
6603
+ }
6604
+
6605
+ var parts = args . parse ( ) ,
6606
+ query , resultIndex ;
6607
+
6608
+ if ( ! parts . length ) {
6602
6609
return 'Y U NO PROVIDE ARGUMENTS!?' ;
6603
6610
}
6604
6611
6605
- if ( cache [ args ] ) {
6606
- return finish ( cache [ args ] ) ;
6612
+ // /urban query in several words
6613
+ if ( isNaN ( parts [ 1 ] ) ) {
6614
+ bot . log ( '/urban input isNaN' ) ;
6615
+ query = args . toString ( ) ;
6616
+ resultIndex = 0 ;
6617
+ }
6618
+ // /urban query index
6619
+ else {
6620
+ bot . log ( '/urban input isn\'t NaN' ) ;
6621
+ query = parts [ 0 ] ;
6622
+ resultIndex = Number ( parts [ 1 ] ) ;
6607
6623
}
6608
6624
6625
+ bot . log ( query , resultIndex , '/urban input' ) ;
6626
+
6609
6627
IO . jsonp ( {
6610
6628
url : 'http://api.urbandictionary.com/v0/define' ,
6611
6629
data : {
6612
- term : args . content
6630
+ term : query
6613
6631
} ,
6614
6632
jsonpName : 'callback' ,
6615
6633
fun : complete
@@ -6619,10 +6637,14 @@ function urban ( args, cb ) {
6619
6637
var msg ;
6620
6638
6621
6639
if ( resp . result_type === 'no_results' ) {
6622
- msg = 'No definition found for ' + args ;
6640
+ msg = 'No definition found for ' + query ;
6641
+ }
6642
+ else if ( resultIndex > resp . list . length ) {
6643
+ msg = 'Nothing in that index. The last one is:\n' +
6644
+ formatTop ( resp . list . pop ( ) ) ;
6623
6645
}
6624
6646
else {
6625
- msg = formatTop ( resp . list [ 0 ] ) ;
6647
+ msg = formatTop ( resp . list [ resultIndex ] ) ;
6626
6648
}
6627
6649
cache [ args ] = msg ;
6628
6650
@@ -6659,7 +6681,8 @@ bot.addCommand({
6659
6681
6660
6682
permissions : { del : 'NONE' , use : 'ALL' } ,
6661
6683
6662
- description : 'Fetches UrbanDictionary definition. `/urban something`' ,
6684
+ description : 'Fetches UrbanDictionary definition. ' +
6685
+ '`/urban query [resultIndex=0]`' ,
6663
6686
async : true
6664
6687
} ) ;
6665
6688
0 commit comments