1
- const _ = require ( 'lodash' ) ;
2
1
const moment = require ( 'moment' ) ;
3
-
2
+ const _ = require ( 'lodash' ) ;
4
3
const Command = require ( '../command-interface' ) ;
5
4
const CommandOutput = require ( '../command-output' ) ;
6
5
const { makeBan } = require ( '../../chat-utils/punishment-helpers' ) ;
7
6
const basePunishmentHelper = require ( '../base-punishment-helper' ) ;
8
7
const formatDuration = require ( '../../chat-utils/format-duration' ) ;
9
8
10
- let voteBanStarted = false ;
11
- const weightedTranslateMap = {
12
- flair8 : 16 ,
13
- flair3 : 8 ,
14
- flair1 : 4 ,
15
- flair13 : 2 ,
16
- flair9 : 2 ,
17
- } ;
18
9
function voteBan ( ipBan , defaultBanTime , weighted ) {
10
+ const pollDuration = 30000 ;
11
+
12
+ let failSafeTimeout = null ;
13
+ let votebanActive = false ;
14
+
19
15
return function ban ( input , services , rawMessage ) {
20
- if ( voteBanStarted ) {
16
+ if ( votebanActive ) {
21
17
return new CommandOutput (
22
18
null ,
23
- 'Vote ban in progress. Please wait for the current vote to finish.' ,
19
+ 'Vote ban in progress. Please wait for the current vote ban to finish.' ,
24
20
) ;
25
21
}
22
+
26
23
const parsedInput = basePunishmentHelper ( input , defaultBanTime ) [ 0 ] ;
24
+
27
25
if ( parsedInput === false ) {
28
26
return new CommandOutput (
29
27
null ,
30
28
'Could not parse the duration. Usage: "!voteban {amount}{m,h,d,w}OR{perm} {user}" !voteban 1d Destiny' ,
31
29
) ;
32
30
}
31
+
33
32
const { isPermanent, userToPunish, parsedDuration, parsedReason } = parsedInput ;
33
+ const muteString = isPermanent
34
+ ? 'PERMANENTLY'
35
+ : formatDuration ( moment . duration ( parsedDuration , 'seconds' ) ) ;
34
36
35
- voteBanStarted = true ;
36
37
const listener = services . messageRelay . startListenerForChatMessages ( 'voteban' ) ;
37
38
if ( listener === false ) {
38
39
return new CommandOutput ( null , 'Something went wrong?? uhh. Restart me.' ) ;
39
40
}
40
41
41
- const muteString = isPermanent
42
- ? 'PERMANENTLY'
43
- : formatDuration ( moment . duration ( parsedDuration , 'seconds' ) ) ;
44
- const votedMap = { } ;
45
- let yes = 0 ;
46
- let no = 0 ;
47
-
48
- listener . on ( 'message' , ( data ) => {
49
- if ( votedMap [ data . user ] ) {
50
- return ;
51
- }
52
- const message = data . message . trim ( ) ;
53
- if ( message . trim ( ) === '1' || message . trim ( ) === '2' ) {
54
- let votes = 1 ;
55
- if ( weighted ) {
56
- votes = _ . max (
57
- Object . keys ( weightedTranslateMap ) . map ( ( k ) => {
58
- const idx = data . roles . indexOf ( k ) ;
59
- if ( idx === - 1 ) return 1 ;
60
- return weightedTranslateMap [ k ] ;
61
- } ) ,
62
- ) ;
63
- }
64
- votedMap [ data . user ] = true ;
65
- // eslint-disable-next-line no-unused-expressions
66
- message . trim ( ) === '1' ? ( yes += votes ) : ( no += votes ) ;
42
+ listener . on ( 'err' , ( message ) => {
43
+ const error = JSON . parse ( message ) ;
44
+ if ( error . description === 'activepoll' && ! votebanActive ) {
45
+ clearTimeout ( failSafeTimeout ) ;
46
+ services . messageRelay . stopRelay ( 'voteban' ) ;
47
+ services . messageRelay . sendOutputMessage (
48
+ 'Poll in progress. Please wait for the current poll to finish.' ,
49
+ ) ;
67
50
}
68
51
} ) ;
69
52
70
- setTimeout ( ( ) => {
71
- voteBanStarted = false ;
53
+ listener . on ( 'pollstart' , ( ) => {
54
+ votebanActive = true ;
55
+ services . messageRelay . sendOutputMessage (
56
+ `Should we ban ${ userToPunish } ${
57
+ muteString === 'PERMANENTLY' ? muteString : `for ${ muteString } `
58
+ } ${ parsedReason ? ` Reason: ${ parsedReason } ` : '' } ?`,
59
+ ) ;
60
+
61
+ // Fail-safe
62
+ failSafeTimeout = setTimeout ( ( ) => {
63
+ votebanActive = false ;
64
+ services . messageRelay . stopRelay ( 'voteban' ) ;
65
+ } , pollDuration + 5000 ) ;
66
+ } ) ;
67
+
68
+ listener . on ( 'pollstop' , ( message ) => {
69
+ clearTimeout ( failSafeTimeout ) ;
70
+ votebanActive = false ;
72
71
services . messageRelay . stopRelay ( 'voteban' ) ;
72
+
73
73
services . messageRelay . sendOutputMessage ( 'Total votes:' ) ;
74
- services . messageRelay . sendOutputMessage ( `Yes votes: ${ yes } ` ) ;
75
- services . messageRelay . sendOutputMessage ( `No votes: ${ no } ` ) ;
76
- if ( yes <= no ) {
74
+
75
+ const poll = JSON . parse ( message ) ;
76
+ const votes = _ . zipObject ( poll . options , poll . totals ) ;
77
+
78
+ services . messageRelay . sendOutputMessage ( `Yes votes: ${ votes . yes } ` ) ;
79
+ services . messageRelay . sendOutputMessage ( `No votes: ${ votes . no } ` ) ;
80
+
81
+ if ( votes . yes <= votes . no ) {
77
82
services . messageRelay . sendOutputMessage (
78
- `No votes win by ${ no - yes } votes, ${ userToPunish } is safe for now.. AYAYA ` ,
83
+ `No votes win by ${ votes . no - votes . yes } votes, ${ userToPunish } is safe for now.. AYAYA ` ,
79
84
) ;
80
85
return ;
81
86
}
87
+
82
88
services . punishmentStream . write (
83
89
makeBan (
84
90
userToPunish ,
@@ -87,17 +93,24 @@ function voteBan(ipBan, defaultBanTime, weighted) {
87
93
isPermanent ,
88
94
`${ userToPunish } banned through bot by a VOTE BAN started by ${ rawMessage . user } . ${
89
95
parsedReason ? `Reason: ${ parsedReason } ` : ''
90
- } Yes votes: ${ yes } No Votes: ${ no } `,
96
+ } Yes votes: ${ votes . yes } No Votes: ${ votes . no } `,
91
97
) ,
92
98
) ;
93
- } , 30500 ) ;
99
+ } ) ;
94
100
95
- return new CommandOutput (
96
- null ,
97
- `/vote Should we ban ${ userToPunish } ${
98
- muteString === 'PERMANENTLY' ? muteString : `for ${ muteString } `
99
- } ${ parsedReason ? ` Reason: ${ parsedReason } ` : '' } ? yes or no 30s`,
101
+ services . messageRelay . emit (
102
+ 'poll' ,
103
+ JSON . stringify ( {
104
+ weighted,
105
+ time : pollDuration ,
106
+ question : `Should we ban ${ userToPunish } ${
107
+ muteString === 'PERMANENTLY' ? muteString : `for ${ muteString } `
108
+ } ${ parsedReason ? ` Reason: ${ parsedReason } ` : '' } `,
109
+ options : [ 'yes' , 'no' ] ,
110
+ } ) ,
100
111
) ;
112
+
113
+ return new CommandOutput ( null , '' ) ;
101
114
} ;
102
115
}
103
116
0 commit comments