1414// Author:
1515// Anthony Liu, wiredfool, patcon@gittip
1616
17+ // config
18+ const config = require ( 'hubot-conf' ) ;
19+
1720// helpers
18- function bind ( fn , context ) {
19- return function ( ) {
20- return fn . apply ( context , arguments ) ;
21+ function bind ( fn , context , ... args ) {
22+ return function apply ( ) {
23+ return fn . apply ( context , args ) ;
2124 } ;
2225}
2326
@@ -52,23 +55,22 @@ class ISawYou {
5255
5356 // adds this message to the msg's user's count if its a pic
5457 add ( msg ) {
55- var username = msg . message . user . name ;
58+ const username = msg . message . user . name ;
5659 if ( this . isPictureMessage ( msg ) ) {
5760 this . robot . logger . debug ( 'isawyou.add #{username}' ) ;
58- if ( this . cache . hasOwnProperty ( username ) ) {
61+ if ( username in this . cache ) {
5962 this . cache [ username ] += 1 ;
6063 } else {
6164 this . cache [ username ] = 1 ;
6265 }
6366 return true ;
64- } else {
65- return false ;
6667 }
68+ return false ;
6769 }
6870
6971 // increments username's points
7072 increment ( username ) {
71- if ( this . cache . hasOwnProperty ( username ) ) {
73+ if ( username in this . cache ) {
7274 this . cache [ username ] += 1 ;
7375 } else {
7476 this . cache [ username ] = 1 ;
@@ -77,7 +79,7 @@ class ISawYou {
7779
7880 // decrements username's points
7981 decrement ( username ) {
80- if ( this . cache . hasOwnProperty ( username ) ) {
82+ if ( username in this . cache ) {
8183 this . cache [ username ] -= 1 ;
8284 if ( this . cache [ username ] < 0 ) {
8385 this . cache [ username ] = 0 ;
@@ -95,31 +97,25 @@ class ISawYou {
9597 // returns the specific user's count
9698 getCount ( username ) {
9799 this . robot . logger . debug ( 'isawyou.get #{username}' ) ;
98- if ( this . cache . hasOwnProperty ( username ) ) {
100+ if ( username in this . cache ) {
99101 return this . cache [ username ] ;
100- } else {
101- return 0 ;
102102 }
103+ return 0 ;
103104 }
104105
105106 // return the top k-scoring users
106107 // format: [{user: , points: }, ...]
107108 getTopK ( k ) {
108109 // yes there are more efficient ways to do this but
109110 // practically it really isn't going to make a differnce
110- var self = this ;
111+ const self = this ;
111112 return Object . keys ( this . cache )
112- . map ( function ( username ) {
113- return {
114- username : username ,
115- points : self . cache [ username ]
116- } ;
117- } ) . sort ( function ( a , b ) {
113+ . map ( ( username ) => ( { username, points : self . cache [ username ] } ) )
114+ . sort ( ( a , b ) => {
118115 if ( a . points === b . points ) {
119116 return a . username > b . username ? 1 : - 1 ;
120- } else {
121- return b . points - a . points ;
122117 }
118+ return b . points - a . points ;
123119 } ) . slice ( 0 , k ) ;
124120 }
125121
@@ -129,30 +125,27 @@ class ISawYou {
129125 }
130126}
131127
132- module . exports = function ( robot ) {
133- 'use strict' ;
134-
135- // config
136- var config = require ( 'hubot-conf' ) ( 'isawyou' , robot ) ;
128+ module . exports = ( robot ) => {
129+ const robotConfig = config ( 'isawyou' , robot ) ;
137130
138131 // working variables
139- var iSawYou = new ISawYou ( robot ) ;
132+ const iSawYou = new ISawYou ( robot ) ;
140133
141134 function reportCount ( msg , username ) {
142- var count = iSawYou . getCount ( username ) ;
143- msg . send ( '*' + username + ' * has ' + count + ' i-saw-you points.' ) ;
135+ const count = iSawYou . getCount ( username ) ;
136+ msg . send ( `* ${ username } * has ${ count } i-saw-you points.` ) ;
144137 }
145138
146139 // update i-saw-you counts
147- robot . hear ( / .* / , function ( msg ) {
140+ robot . hear ( / .* / , ( msg ) => {
148141 // only listen to messages in the #i-saw-you channel
149- if ( config ( 'room' ) === msg . message . room ) {
150- var username = msg . message . user . name ;
151- var matchesInc = msg . message . text . match (
152- / ^ @ ( [ - \w . \\ ^ | { } ` \ [\] ] + ) \+ \+ $ /
142+ if ( robotConfig ( 'room' ) === msg . message . room ) {
143+ const username = msg . message . user . name ;
144+ const matchesInc = msg . message . text . match (
145+ / ^ @ ( [ - \w . \\ ^ | { } ` [ \] ] + ) \+ \+ $ / ,
153146 ) ;
154- var matchesDec = msg . message . text . match (
155- / ^ @ ( [ - \w . \\ ^ | { } ` \ [\] ] + ) - - $ /
147+ const matchesDec = msg . message . text . match (
148+ / ^ @ ( [ - \w . \\ ^ | { } ` [ \] ] + ) - - $ / ,
156149 ) ;
157150 if ( matchesInc && matchesInc . length === 2 ) {
158151 iSawYou . increment ( matchesInc [ 1 ] ) ;
@@ -161,7 +154,7 @@ module.exports = function(robot) {
161154 iSawYou . decrement ( matchesDec [ 1 ] ) ;
162155 reportCount ( msg , matchesDec [ 1 ] ) ;
163156 } else {
164- var added = iSawYou . add ( msg ) ;
157+ const added = iSawYou . add ( msg ) ;
165158 if ( added ) {
166159 reportCount ( msg , username ) ;
167160 }
@@ -171,51 +164,48 @@ module.exports = function(robot) {
171164
172165 // respond to i-saw-you increments
173166 robot . respond (
174- / i s a w y o u @ ? ( [ - \w . \\ ^ | { } ` \ [\] ] + ) \+ \+ $ / ,
175- function ( msg ) {
176- var username = msg . match [ 1 ] ;
167+ / i s a w y o u @ ? ( [ - \w . \\ ^ | { } ` [ \] ] + ) \+ \+ $ / ,
168+ ( msg ) => {
169+ const username = msg . match [ 1 ] ;
177170 iSawYou . increment ( username ) ;
178171 reportCount ( msg , username ) ;
179- }
172+ } ,
180173 ) ;
181174
182175 // respond to i-saw-you leaderboard requests
183176 robot . respond (
184177 / i s a w y o u l e a d e r b o a r d / ,
185- function ( msg ) {
186- var k = parseInt ( config ( 'k' ) ) ;
187- var users = iSawYou . getTopK ( k ) ;
188- var prefix = '~ i-saw-you leaderboard ~\n' ;
189- var message = prefix + users
190- . reduce ( function ( a , user ) {
191- return a + '*' + user . username + '*: ' +
192- user . points + ' points\n' ;
193- } , '' ) ;
178+ ( msg ) => {
179+ const k = parseInt ( robotConfig ( 'k' ) , 10 ) ;
180+ const users = iSawYou . getTopK ( k ) ;
181+ const prefix = '~ i-saw-you leaderboard ~\n' ;
182+ const message = prefix + users
183+ . reduce ( ( a , user ) => `${ a } *${ user . username } *: ${ user . points } points\n` , '' ) ;
194184 msg . send ( message ) ;
195- }
185+ } ,
196186 ) ;
197187
198188 // respond to i-saw-you get requests
199189 robot . respond (
200- / i s a w y o u g e t @ ? ( [ - \w . \\ ^ | { } ` \ [\] ] + ) $ / ,
201- function ( msg ) {
202- var username = msg . match [ 1 ] ;
190+ / i s a w y o u g e t @ ? ( [ - \w . \\ ^ | { } ` [ \] ] + ) $ / ,
191+ ( msg ) => {
192+ const username = msg . match [ 1 ] ;
203193 reportCount ( msg , username ) ;
204- }
194+ } ,
205195 ) ;
206196
207197 // respond to point set requests
208198 robot . respond (
209- / i s a w y o u s e t @ ? ( [ - \w . \\ ^ | { } ` \ [\] ] + ) ( \d + ) / ,
210- function ( msg ) {
199+ / i s a w y o u s e t @ ? ( [ - \w . \\ ^ | { } ` [ \] ] + ) ( \d + ) / ,
200+ ( msg ) => {
211201 if ( msg . match . length < 3 ) {
212202 msg . send ( 'Usage: hackbot isawyou set <user> <number>' ) ;
213203 } else {
214- var username = msg . match [ 1 ] ;
215- var newPoints = parseInt ( msg . match [ 2 ] ) ;
204+ const username = msg . match [ 1 ] ;
205+ const newPoints = parseInt ( msg . match [ 2 ] , 10 ) ;
216206 iSawYou . set ( username , newPoints ) ;
217- msg . send ( '*' + username + ' * now has ' + newPoints + ' i-saw-you points.' ) ;
207+ msg . send ( `* ${ username } * now has ${ newPoints } i-saw-you points.` ) ;
218208 }
219- }
209+ } ,
220210 ) ;
221211} ;
0 commit comments