Newer
Older
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
import discord
import emoji
DEFAULT_OPTIONS = ["🇦", "🇧", "🇨", "🇩", "🇪", "🇫", "🇬", "🇭", "🇮", "🇯", "🇰", "🇱", "🇲", "🇳", "🇴", "🇵", "🇶",
"🇷"]
DELETE_POLL = "🗑️"
CLOSE_POLL = "🛑"
def get_unique_option(options):
for option in DEFAULT_OPTIONS:
if option not in options:
return option
def get_options(bot, answers):
options = []
for i in range(min(len(answers), len(DEFAULT_OPTIONS))):
option = ""
answer = answers[i].strip()
index = answer.find(" ")
if index > -1:
possible_option = answer[:index]
if emoji.is_emoji(possible_option):
if len(answer[index:].strip()) > 0:
option = possible_option
answers[i] = answer[index:].strip()
elif len(possible_option) > 1:
if (possible_option[0:2] == "<:" or possible_option[0:3] == "<a:") and possible_option[-1] == ">":
splitted_custom_emoji = possible_option.strip("<a:>").split(":")
if len(splitted_custom_emoji) == 2:
id = splitted_custom_emoji[1]
custom_emoji = bot.get_emoji(int(id))
if custom_emoji and len(answer[index:].strip()) > 0:
option = custom_emoji
answers[i] = answer[index:].strip()
if (isinstance(option, str) and len(option) == 0) or option in options or option in [DELETE_POLL,
CLOSE_POLL]:
option = get_unique_option(options)
options.append(option)
return options
class Poll:
def __init__(self, bot, question=None, answers=None, author=None, message=None):
self.bot = bot
self.question = question
self.answers = answers
self.author = author
if message:
self.message = message
self.answers = []
embed = message.embeds[0]
self.author = embed.fields[0].value[3:-1]
self.question = embed.description
for i in range(2, len(embed.fields)):
self.answers.append(f"{embed.fields[i].name} {embed.fields[i].value}")
self.options = get_options(self.bot, self.answers)
async def send_poll(self, channel, result=False, message=None):
option_ctr = 0
title = "Umfrage"
participants = {}
if result:
title += " Ergebnis"
if len(self.answers) > len(DEFAULT_OPTIONS):
await channel.send(
f"Fehler beim Erstellen der Umfrage! Es werden nicht mehr als {len(DEFAULT_OPTIONS)} Optionen unterstützt!")
return
embed = discord.Embed(title=title, description=self.question)
embed.add_field(name="Erstellt von", value=f'<@!{self.author}>', inline=False)
embed.add_field(name="\u200b", value="\u200b", inline=False)
for i in range(0, len(self.answers)):
name = f'{self.options[i]}'
value = f'{self.answers[i]}'
if result:
reaction = self.get_reaction(name)
if reaction:
name += f' : {reaction.count - 1}'
async for user in reaction.users():
if user != self.bot.user:
participants[str(user.id)] = 1
embed.add_field(name=name, value=value, inline=False)
option_ctr += 1
if result:
embed.add_field(name="\u200b", value="\u200b", inline=False)
embed.add_field(name="Anzahl Teilnehmer an der Umfrage", value=f"{len(participants)}", inline=False)
if message:
await message.edit(embed=embed)
else:
message = await channel.send("", embed=embed)
reactions = []
for reaction in message.reactions:
reactions.append(reaction.emoji)
if not result:
await message.clear_reaction("🗑️")
await message.clear_reaction("🛑")
for reaction in reactions:
if reaction not in self.options:
await message.clear_reaction(reaction)
for i in range(0, len(self.answers)):
if self.options[i] not in reactions:
await message.add_reaction(self.options[i])
await message.add_reaction("🗑️")
await message.add_reaction("🛑")
async def close_poll(self):
await self.send_poll(self.message.channel, result=True)
await self.delete_poll()
async def delete_poll(self):
await self.message.delete()
def get_reaction(self, reaction):
if self.message:
reactions = self.message.reactions
for react in reactions:
if react.emoji == reaction:
return react