-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpollAction.js
197 lines (153 loc) · 5.26 KB
/
pollAction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var {
AbstractStrategyBasedChatAction,
AbstractMessageStrategy,
MessageParser
} = require("multi-stream-chatbot/actions")
// !makepoll | poll_question | option_1 | option_2 -> Create a poll (maybe limit to certain users)
// !vote | option -> Vote for an option in the poll
// !endpoll -> Ends the current poll (limit to certain user)
// !viewpoll -> View the poll and current options
class Poll {
constructor(question, options, creator) {
this.question = question
this.options = options
this.creator = creator
this.voters = []
}
hasVoter(voter) {
return this.voters.includes(voter)
}
addVoter(voter) {
this.voters.push(voter)
}
}
class PollOption {
constructor(name) {
this.name = name
this.votes = 0
}
incrementVote() {
this.votes += 1
}
}
class MakePollStrategy extends AbstractMessageStrategy {
constructor(pollAction) {
super()
this.pollAction = pollAction
}
matches(message, ctx) {
return new MessageParser().parseCommand(message) === "!makepoll"
}
async makeMessage(message, ctx) {
if (this.pollAction.getCurrentPoll()) {
return "Sorry, a poll already exists. Please end the poll to create a new one."
}
const commandParts = message.split("|")
const pollQuestion = commandParts[1].trim()
const option1 = commandParts[2].trim().toLowerCase()
const option2 = commandParts[3].trim().toLowerCase()
const username = (ctx.author && ctx.author.displayName) || "(Unknown user)"
const options = [new PollOption(option1), new PollOption(option2)]
const poll = new Poll(pollQuestion, options, username)
this.pollAction.setCurrentPoll(poll)
return `I've created a poll for "${pollQuestion}". Use "!vote | OPTION_NAME" to vote for an option.`
}
}
class ViewPollStrategy extends AbstractMessageStrategy {
constructor(pollAction) {
super()
this.pollAction = pollAction
}
matches(message, ctx) {
return new MessageParser().parseCommand(message) === "!viewpoll"
}
async makeMessage(message, ctx) {
const poll = this.pollAction.getCurrentPoll()
if (!poll) {
return "A poll doesn't exist right now. Try again when there is an active poll."
}
var optionsResults = []
for (const option of poll.options) {
optionsResults.push(`${option.name}=${option.votes}`)
}
const optionResultsStr = optionsResults.join(", ")
return `Poll: ${poll.question} (created by: ${poll.creator}) ${optionResultsStr}`
}
}
class VoteStrategy extends AbstractMessageStrategy {
constructor(pollAction) {
super()
this.pollAction = pollAction
}
matches(message, ctx) {
return new MessageParser().parseCommand(message) === "!vote"
}
async makeMessage(message, ctx) {
const poll = this.pollAction.getCurrentPoll()
if (!poll) {
return "A poll doesn't exist right now. Try again when there is an active poll."
}
const commandParts = message.split("|")
const userOption = commandParts[1].trim().toLowerCase()
const author_id = ctx.author.id
const username = (ctx.author && ctx.author.displayName) || "(Unknown user)"
if (!author_id) {
return
}
if (poll.hasVoter(author_id)) {
return `Sorry, ${username}, you have already voted`
}
var validOption = undefined
for (const option of poll.options) {
if (option.name === userOption) {
validOption = option
}
}
if (!validOption) {
return `Sorry, ${username}, that was not a valid option (${userOption})`
}
validOption.incrementVote()
poll.addVoter(author_id)
return `Thanks ${username}, I have recorded your vote.`
}
}
class ClosePollStrategy extends AbstractMessageStrategy {
constructor(pollAction) {
super()
this.pollAction = pollAction
}
matches(message, ctx) {
return new MessageParser().parseCommand(message) === "!closepoll"
}
async makeMessage(message, ctx) {
const poll = this.pollAction.getCurrentPoll()
if (!poll) {
return "A poll doesn't exist right now. Try again when there is an active poll."
}
var winningResult = undefined
for (const option of poll.options) {
if (!winningResult || winningResult && winningResult.votes < option.votes) {
winningResult = option
}
}
const optionResultsStr = `${winningResult.name} wins with ${winningResult.votes} votes`
return `Poll: ${poll.question} (created by: ${poll.creator}), ${optionResultsStr}`
}
}
class PollAction extends AbstractStrategyBasedChatAction {
registerStrategies() {
return [
new MakePollStrategy(this),
new ViewPollStrategy(this),
new VoteStrategy(this),
new ClosePollStrategy(this)
]
}
getCurrentPoll() {
return this.currentPoll
}
setCurrentPoll(poll) {
this.currentPoll = poll
}
}
module.exports = PollAction