-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll.py
186 lines (133 loc) · 5.05 KB
/
poll.py
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from errbot import BotPlugin, CommandError, botcmd
from errbot.utils import drawbar
class Poll(BotPlugin):
active_poll = None
@botcmd
def poll(self, mess, args):
"""List all polls."""
return self.poll_list(mess, args)
@botcmd(syntax="<poll name>")
def poll_new(self, mess, args):
"""Create a new poll."""
title = mess.ctx["title"] if "title" in mess.ctx else args
if not title:
raise CommandError("usage: !poll new <poll_title>")
mess.ctx["current_poll"] = (title, {}, [])
return "Poll created. Use !poll option to add options."
@botcmd(syntax="<poll option>")
def poll_addoption(self, mess, args):
"""Add an option to the currently running poll."""
option = mess.ctx["options"].pop() if "options" in mess.ctx else args
if not option:
raise CommandError("usage: !poll addoption <poll_option>")
title, options, voted = mess.ctx["current_poll"]
if option in options:
raise CommandError(
"Option already exists. Use !poll show to see all options."
)
options[option] = 0
return 'Option "%s" added.' % option
@botcmd
def poll_remove(self, mess, args):
"""Remove a poll."""
title = args
if not title:
raise CommandError("usage: !poll remove <poll_title>")
try:
del self[title]
return "Poll removed."
except KeyError as _:
raise CommandError(
"That poll does not exist. Use !poll list to see all polls."
)
@botcmd
def poll_list(self, mess, args):
"""List all polls."""
if len(self) > 0:
return "All Polls:\n" + "\n".join(
[title + (" *" if title == Poll.active_poll else "") for title in self]
)
raise CommandError("No polls found. Use !poll new to add one.")
@botcmd
def poll_start(self, mess, args):
"""Start a saved poll."""
if Poll.active_poll is not None:
raise CommandError(
'"%s" is currently running, use !poll stop to finish it.'
% Poll.active_poll
)
title = args
if not title:
raise CommandError("usage: !poll start <poll_title>")
if not title in self:
raise CommandError("Poll not found. Use !poll list to see all polls.")
self.reset_poll(title)
Poll.active_poll = title
return self.format_poll(title)
@botcmd
def poll_stop(self, mess, args):
"""Stop the currently running poll."""
result = "Poll finished, final results:\n"
result += self.format_poll(Poll.active_poll)
self.reset_poll(Poll.active_poll)
Poll.active_poll = None
return result
@botcmd
def poll_show(self, mess, args):
"""Show the currently running poll."""
if not Poll.active_poll:
raise CommandError("No active poll. Use !poll start to start a poll.")
return self.format_poll(Poll.active_poll)
@botcmd
def poll_vote(self, mess, args):
"""Vote for the currently running poll."""
if not Poll.active_poll:
raise CommandError("No active poll. Use !poll start to start a poll.")
index = args
if not index:
raise CommandError("usage: !poll vote <option_number>")
if not index.isdigit():
raise CommandError("Please vote using the numerical index of the option.")
poll = self[Poll.active_poll]
options, usernames = poll
index = int(index)
if index > len(options) or index < 1:
raise CommandError(
"Please choose a number between 1 and %d (inclusive)." % len(options)
)
option = list(options.keys())[index - 1] # FIXME: this looks random
if not option in options:
raise CommandError(
"Option not found. Use !poll show to see all options of the current poll."
)
username = mess.frm.person
if username in usernames:
raise CommandError("You have already voted.")
usernames.append(username)
options[option] += 1
self[Poll.active_poll] = poll
return self.format_poll(Poll.active_poll)
def format_poll(self, title):
poll = self[title]
options, usernames = poll
total_votes = sum(options.values())
result = Poll.active_poll + "\n"
index = 1
for option in options:
result += "%s %d. %s (%d votes)\n" % (
drawbar(poll[0][option], total_votes),
index,
option,
poll[0][option],
)
index += 1
return result.strip()
def reset_poll(self, title):
poll = self[title]
options, usernames = poll
for option in options:
options[option] = 0
del usernames[:]
self[title] = poll