-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolls.py
71 lines (60 loc) · 2.92 KB
/
polls.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
import discord
from discord.ext import commands
class QuickPoll:
""""""
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True)
async def quickpoll(self, ctx, question, *options: str):
if len(options) <= 1:
await self.bot.say('You need more than one option to make a poll!')
return
if len(options) > 10:
await self.bot.say('You cannot make a poll for more than 10 things!')
return
if len(options) == 2 and options[0] == 'yes' and options[1] == 'no':
reactions = ['✅', '❌']
else:
reactions = ['1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣', '🔟']
description = []
avatar = ctx.message.author.avatar_url
for x, option in enumerate(options):
description += '\n {} {}'.format(reactions[x], option)
embed = discord.Embed(title=question, description=''.join(description))
react_message = await self.bot.say(embed=embed)
for reaction in reactions[:len(options)]:
await self.bot.add_reaction(react_message, reaction)
author = ctx.message.author.name
embed.set_author(name=author, icon_url=avatar)
embed.set_footer(text='Poll ID: {}'.format(react_message.id))
await self.bot.edit_message(react_message, embed=embed)
await self.bot.delete_message(ctx.message)
@commands.command(pass_context=True)
async def tally(self, ctx, id):
poll_message = await self.bot.get_message(ctx.message.channel, id)
if not poll_message.embeds:
return
embed = poll_message.embeds[0]
if poll_message.author != ctx.message.server.me:
return
if not embed['footer']['text'].startswith('Poll ID:'):
return
unformatted_options = [x.strip() for x in embed['description'].split('\n')]
opt_dict = {x[:2]: x[3:] for x in unformatted_options} if unformatted_options[0][0] == '1' \
else {x[:1]: x[2:] for x in unformatted_options}
# check if we're using numbers for the poll, or x/checkmark, parse accordingly
voters = [ctx.message.server.me.id] # add the bot's ID to the list of voters to exclude it's votes
tally = {x: 0 for x in opt_dict.keys()}
for reaction in poll_message.reactions:
if reaction.emoji in opt_dict.keys():
reactors = await self.bot.get_reaction_users(reaction)
for reactor in reactors:
if reactor.id not in voters:
tally[reaction.emoji] += 1
voters.append(reactor.id)
output = 'Results of the poll for "{}":\n'.format(embed['title']) + \
'\n'.join(['{}: {}'.format(opt_dict[key], tally[key]) for key in tally.keys()])
await self.bot.say(output)
def setup(bot):
bot.add_cog(QuickPoll(bot))
print('loaded quickpolls')