1
+ const discord = require ( 'discord.js' ) ;
2
+ const client = new discord . Client ( ) ;
3
+ const config = require ( './config.json' ) ;
4
+ const userCreatedPolls = new Map ( ) ;
5
+
6
+ client . login ( config . TOKEN ) ;
7
+ client . on ( 'ready' , ( ) => console . log ( client . user . tag + " has logged in." ) ) ;
8
+
9
+ client . on ( 'message' , async message => {
10
+ if ( message . author . bot ) return ;
11
+ if ( message . content . toLowerCase ( ) === '!createpoll' ) {
12
+ if ( userCreatedPolls . has ( message . author . id ) ) {
13
+ message . channel . send ( "You already have a poll going on right now." ) ;
14
+ return ;
15
+ }
16
+ message . channel . send ( "Enter options. Max 5. Type done when finished." ) ;
17
+ let filter = m => {
18
+ if ( m . author . id === message . author . id ) {
19
+ if ( m . content . toLowerCase ( ) === 'done' ) collector . stop ( ) ;
20
+ else return true ;
21
+ }
22
+ else return false ;
23
+ }
24
+ let collector = message . channel . createMessageCollector ( filter , { maxMatches : 5 } ) ;
25
+ let pollOptions = await getPollOptions ( collector ) ;
26
+ if ( pollOptions . length < 2 ) {
27
+ message . channel . send ( "Not enough options, must contain 2!" ) ;
28
+ return ;
29
+ }
30
+ let embed = new discord . RichEmbed ( ) ;
31
+ embed . setTitle ( "Your Poll" ) ;
32
+ embed . setDescription ( pollOptions . join ( "\n" ) ) ;
33
+ let confirm = await message . channel . send ( embed ) ;
34
+
35
+ await confirm . react ( '✅' ) ;
36
+ await confirm . react ( '❎' ) ;
37
+
38
+ let reactionFilter = ( reaction , user ) => ( user . id === message . author . id ) && ! user . bot ;
39
+ let reaction = ( await confirm . awaitReactions ( reactionFilter , { max : 1 } ) ) . first ( ) ;
40
+ if ( reaction . emoji . name === '✅' ) {
41
+ message . channel . send ( "Poll will begin in 1 seconds." ) ;
42
+ await delay ( 1000 ) ;
43
+ message . channel . send ( "Vote now!" ) ;
44
+ let userVotes = new Map ( ) ;
45
+ let pollTally = new discord . Collection ( pollOptions . map ( o => [ o , 0 ] ) ) ;
46
+ let pollFilter = m => ! m . bot ;
47
+ let voteCollector = message . channel . createMessageCollector ( pollFilter , {
48
+ time : 60000
49
+ } ) ;
50
+ userCreatedPolls . set ( message . author . id , voteCollector ) ;
51
+ await processPollResults ( voteCollector , pollOptions , userVotes , pollTally ) ;
52
+ let max = Math . max ( ...pollTally . array ( ) ) ;
53
+ console . log ( pollTally . entries ( ) ) ;
54
+ let entries = [ ...pollTally . entries ( ) ] ;
55
+ let winners = [ ] ;
56
+ let embed = new discord . RichEmbed ( ) ;
57
+ let desc = '' ;
58
+ entries . forEach ( entry => entry [ 1 ] === max ? winners . push ( entry [ 0 ] ) : null ) ;
59
+ entries . forEach ( entry => desc += entry [ 0 ] + " received " + entry [ 1 ] + " votes(s)\n" ) ;
60
+ embed . setDescription ( desc ) ;
61
+
62
+ if ( winners . length === 1 ) {
63
+ message . channel . send ( winners [ 0 ] + " is the winner!" , embed ) ;
64
+ }
65
+ else {
66
+ message . channel . send ( "We have a draw!" , embed ) ;
67
+ }
68
+ }
69
+ else if ( reaction . emoji . name === '❎' ) {
70
+ message . channel . send ( "Poll cancelled." ) ;
71
+ }
72
+ }
73
+ else if ( message . content . toLowerCase ( ) === '!stopvote' ) {
74
+ if ( userCreatedPolls . has ( message . author . id ) ) {
75
+ console . log ( "Trying to stop poll." ) ;
76
+ userCreatedPolls . get ( message . author . id ) . stop ( ) ;
77
+ userCreatedPolls . delete ( message . author . id ) ;
78
+ }
79
+ else {
80
+ message . channel . send ( "You don't have a poll going on right now." ) ;
81
+ }
82
+ }
83
+ } ) ;
84
+
85
+ function processPollResults ( voteCollector , pollOptions , userVotes , pollTally ) {
86
+ return new Promise ( ( resolve , reject ) => {
87
+ voteCollector . on ( 'collect' , msg => {
88
+ let option = msg . content . toLowerCase ( ) ;
89
+ if ( ! userVotes . has ( msg . author . id ) && pollOptions . includes ( option ) ) {
90
+ userVotes . set ( msg . author . id , msg . content ) ;
91
+ let voteCount = pollTally . get ( option ) ;
92
+ pollTally . set ( option , ++ voteCount ) ;
93
+ }
94
+ } ) ;
95
+ voteCollector . on ( 'end' , collected => {
96
+ console . log ( "Collected " + collected . size + " votes." ) ;
97
+ resolve ( collected ) ;
98
+ } )
99
+ } ) ;
100
+ }
101
+
102
+ function getPollOptions ( collector ) {
103
+ return new Promise ( ( resolve , reject ) => {
104
+ collector . on ( 'end' , collected => resolve ( collected . map ( m => m . content . toLowerCase ( ) ) ) ) ;
105
+ } ) ;
106
+ }
107
+
108
+ function delay ( time ) {
109
+ return new Promise ( ( resolve , reject ) => {
110
+ setTimeout ( ( ) => {
111
+ resolve ( ) ;
112
+ } , time )
113
+ } )
114
+ }
0 commit comments