1
- 'use strict' ;
2
-
3
1
// Handler for admin functionality.
4
- let minimap = require ( 'minimap' ) ;
5
- let friends = require ( './friends.js' ) ;
6
- let logger = require ( './logger.js' ) ;
2
+ const minimap = require ( 'minimap' ) ;
3
+ const _ = require ( 'lodash' ) ;
4
+ const friends = require ( './friends.js' ) ;
5
+ const logger = require ( './logger.js' ) ;
6
+
7
7
let DICT ;
8
8
let bot ;
9
9
let shutdown ;
10
10
11
- exports . init = function ( initBot , dictionary , killCommand ) {
12
- bot = initBot ;
13
- DICT = dictionary ;
14
- shutdown = killCommand ;
15
- } ;
16
-
17
- exports . command = function ( source , input , original ) {
18
-
19
- let command = input [ 1 ] ;
20
- if ( actions . hasOwnProperty ( command ) ) {
21
- actions [ command ] ( source , input , original ) ;
22
- return ;
23
- } else {
24
- return ;
25
- }
26
-
27
- } ;
28
-
29
- let actions = {
30
- quit : function ( ) {
11
+ const actions = {
12
+ quit ( ) {
31
13
shutdown ( ) ;
32
14
} ,
33
15
34
- dump : function ( source , input ) {
16
+ dump ( source , input ) {
35
17
if ( input [ 2 ] === 'friends' ) {
36
18
logger . log ( JSON . stringify ( friends . getAllFriends ( ) ) ) ;
37
19
friends . messageUser ( source , DICT . ADMIN . dump_friends ) ;
@@ -41,28 +23,27 @@ let actions = {
41
23
}
42
24
} ,
43
25
44
- lookup : function ( source , input ) {
45
- let lookupList = friends . getAllFriends ( ) ;
46
- if ( lookupList . hasOwnProperty ( input [ 2 ] ) ) {
47
- let friend = lookupList [ input [ 2 ] ] ;
26
+ lookup ( source , input ) {
27
+ const lookupList = friends . getAllFriends ( ) ;
28
+ if ( lookupList [ input [ 2 ] ] ) {
29
+ const friend = lookupList [ input [ 2 ] ] ;
48
30
friends . messageUser ( source , JSON . stringify ( friend , null , ' ' ) ) ;
49
31
} else {
50
32
friends . messageUser ( source , DICT . ADMIN . lookup_error ) ;
51
33
}
52
34
} ,
53
35
54
- inactive : function ( source ) {
55
- let ONE_WEEK = 60 * 60 * 24 * 7 * 1000 ;
56
- let inactiveList = friends . getAllFriends ( ) ;
57
- let inactiveUsers = [ ] ;
58
- for ( let inactiveFriend in inactiveList ) {
59
-
60
- // If this user hasn't used this bot in a week, log it.
61
- if ( new Date ( inactiveList [ inactiveFriend ] . lastMessageTime ) . getTime ( ) <
36
+ inactive ( source ) {
37
+ const ONE_WEEK = 60 * 60 * 24 * 7 * 1000 ;
38
+ const inactiveList = friends . getAllFriends ( ) ;
39
+ const inactiveUsers = _ . compact ( _ . map ( inactiveList , ( inactiveFriend , id ) => {
40
+ if ( new Date ( inactiveFriend . lastMessageTime ) . getTime ( ) <
62
41
( new Date ( ) . getTime ( ) - ONE_WEEK ) ) {
63
- inactiveUsers . push ( inactiveList [ inactiveFriend ] ) ;
42
+ return id ;
64
43
}
65
- }
44
+
45
+ return undefined ;
46
+ } ) ) ;
66
47
67
48
if ( inactiveUsers . length === 0 ) {
68
49
friends . messageUser ( source , DICT . ADMIN . no_inactive_users ) ;
@@ -71,38 +52,58 @@ let actions = {
71
52
}
72
53
} ,
73
54
74
- kick : function ( source , input ) {
75
- let friendId = input [ 2 ] ;
55
+ kick ( source , input ) {
56
+ const friendId = input [ 2 ] ;
76
57
bot . removeFriend ( input [ 2 ] ) ;
77
- friends . removeFriend ( friendId , function ( success ) {
58
+ friends . removeFriend ( friendId , ( success ) => {
78
59
if ( success ) {
79
- friends . messageUser ( source , minimap . map ( { id : friendId } , DICT . ADMIN . remove_friend_success ) ) ;
60
+ friends . messageUser (
61
+ source ,
62
+ minimap . map (
63
+ { id : friendId } ,
64
+ DICT . ADMIN . remove_friend_success ,
65
+ ) ,
66
+ ) ;
80
67
} else {
81
- friends . messageUser ( source , minimap . map ( { id : friendId } , DICT . ADMIN . remove_friend_error ) ) ;
68
+ friends . messageUser ( source , minimap . map ( { id : friendId } , DICT . ADMIN . remove_friend_error ) ) ;
82
69
}
83
70
} ) ;
84
71
} ,
85
72
86
- blacklist : function ( source , input ) {
73
+ blacklist ( source , input ) {
87
74
friends . blacklist ( input [ 2 ] ) ;
88
75
friends . messageUser ( source , DICT . ADMIN . blacklist_add ) ;
89
76
} ,
90
77
91
- unblacklist : function ( source , input ) {
78
+ unblacklist ( source , input ) {
92
79
friends . unBlacklist ( input [ 2 ] ) ;
93
80
friends . messageUser ( source , DICT . ADMIN . blacklist_remove ) ;
94
81
} ,
95
82
96
- add : function ( source , input ) {
83
+ add ( source , input ) {
97
84
bot . addFriend ( input [ 2 ] ) ;
98
85
friends . addFriend ( input [ 2 ] ) ;
99
86
} ,
100
87
101
- broadcast : function ( source , input , original ) {
102
- let adminMessage = original . replace ( 'admin broadcast' , '' ) ;
103
- logger . log ( minimap . map ( { message : adminMessage } , DICT . ADMIN . broadcast_log ) ) ;
88
+ broadcast ( source , input , original ) {
89
+ const adminMessage = original . replace ( 'admin broadcast' , '' ) ;
90
+ logger . log ( minimap . map ( { message : adminMessage } , DICT . ADMIN . broadcast_log ) ) ;
104
91
friends . broadcast ( source , adminMessage ) ;
105
92
friends . messageUser ( source , DICT . ADMIN . broadcast_sent ) ;
106
- }
93
+ } ,
94
+
95
+ } ;
96
+
97
+ exports . init = function init ( initBot , dictionary , killCommand ) {
98
+ bot = initBot ;
99
+ DICT = dictionary ;
100
+ shutdown = killCommand ;
101
+ } ;
107
102
103
+ exports . command = function handleCommand ( source , input , original ) {
104
+ const command = input [ 1 ] ;
105
+ if ( actions [ command ] ) {
106
+ actions [ command ] ( source , input , original ) ;
107
+ }
108
108
} ;
109
+
0 commit comments