11'use strict'
22
33const fs = require ( 'fs' )
4+ const path = require ( 'path' )
45
56const Sample = {
67 assignee : null ,
@@ -105,6 +106,21 @@ function help() {
105106 return actions . map ( action => `${ action . regexp . toString ( ) } ` ) . join ( '\n' )
106107}
107108
109+ function getRandomInt ( min , max ) {
110+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
111+ }
112+
113+ var sources = { }
114+
115+ function gimme ( type ) {
116+ if ( ! sources [ type ] ) {
117+ const content = fs . readFileSync ( path . basename ( `${ type } .txt` ) )
118+ sources [ type ] = content . toString ( ) . split ( '\n' )
119+ }
120+ const saying = sources [ type ] [ getRandomInt ( 0 , sources [ type ] . length ) ]
121+ return saying
122+ }
123+
108124const actions = [
109125 {
110126 regexp : / ^ $ / gi,
@@ -127,40 +143,45 @@ const actions = [
127143 reply : ( message , output ) => `will ${ output . content } (ticket #${ output . id } )` ,
128144 } ,
129145 {
130- regexp : / s h o w # ( [ 0 - 9 ] * ) / gi,
146+ regexp : / s h o w # ( [ 0 - 9 ] * ) / gi,
131147 action : ( tickets , message , id ) => findTicket ( tickets , id ) ,
132148 reply : ( message , output ) => showTicket ( output ) ,
133149 } ,
134150 {
135- regexp : / t a k e # ( [ 0 - 9 ] * ) / gi,
151+ regexp : / t a k e # ( [ 0 - 9 ] * ) / gi,
136152 action : ( tickets , message , id ) => assign ( tickets , id , message . userName ) ,
137153 reply : ( message , output ) => `ticket #${ output . id } is assigned to ${ output . assignee } ` ,
138154 } ,
139155 {
140- regexp : / a s s i g n # ( [ 0 - 9 ] * ) t o ( \w * ) / gi,
156+ regexp : / a s s i g n # ( [ 0 - 9 ] * ) t o ( \w * ) / gi,
141157 action : ( tickets , message , id , assignee ) => assign ( tickets , id , assignee ) ,
142158 reply : ( message , output ) => `ticket #${ output . id } is assigned to ${ output . assignee } ` ,
143159 } ,
144160 {
145- regexp : / d e b u g / gi,
161+ regexp : / d e b u g / gi,
146162 action : ( tickets , message , id ) => tickets ,
147163 reply : stringifyThis ,
148164 } ,
149165 {
150- regexp : / t o d o / gi,
166+ regexp : / t o d o / gi,
151167 action : ( tickets , message , id ) => tickets ,
152168 reply : ( message , output ) => showTickets ( output , [ 'open' ] ) ,
153169 } ,
154170 {
155- regexp : / h i s t o r y / gi,
171+ regexp : / h i s t o r y / gi,
156172 action : ( tickets , message , id ) => tickets ,
157173 reply : ( message , output ) => showTickets ( output , [ 'open' , 'closed' ] ) ,
158174 } ,
159175 {
160- regexp : / f o r g e t i t / gi,
176+ regexp : / f o r g e t i t / gi,
161177 action : ( tickets , message , id ) => forget ( tickets ) ,
162178 reply : ( message , output ) => `deleted ${ output } tickets` ,
163179 } ,
180+ {
181+ regexp : / g i m m e \s * a * n * \s * ( \w * ) / gi,
182+ action : ( tickets , message , type ) => gimme ( type ) ,
183+ reply : ( message , output ) => `${ message . userName } ${ output [ 0 ] . toLowerCase ( ) } ${ output . substr ( 1 ) } ` ,
184+ } ,
164185]
165186
166187function searchRegexp ( action ) {
@@ -186,7 +207,7 @@ function process(tickets, message) {
186207 reply = `${ action . reply . bind ( output ) ( message , output ) } `
187208 // }
188209 } else {
189- reply = `I don't understand: ${ message . content } `
210+ reply = `I don't understand: ${ message . content } , can you try again? `
190211 }
191212 return `#${ message . prefix } : ${ reply } `
192213}
0 commit comments