-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (115 loc) ยท 4.81 KB
/
main.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
import discord
import dotenv
import os
import emoji
bot = discord.Bot()
dotenv.load_dotenv()
token = os.getenv('POLL_BOT_TOKEN')
embed_color = 0xefb034
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
@bot.slash_command(description='Create simple poll')
async def simple_poll(ctx, question, choice_a=None, choice_b=None, choice_c=None, choice_d=None, choice_e=None,
choice_f=None, choice_g=None, choice_h=None, choice_i=None, choice_j=None, choice_k=None,
choice_l=None, choice_m=None, choice_n=None, choice_o=None, choice_p=None, choice_q=None,
choice_r=None, choice_s=None, choice_t=None):
choices = [choice_a, choice_b, choice_c, choice_d, choice_e, choice_f, choice_g, choice_h, choice_i, choice_j,
choice_k, choice_l, choice_m, choice_n, choice_o, choice_p, choice_q, choice_r, choice_s, choice_t]
choices = [i for i in choices if i]
choices_emojis = []
# letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
# 'u', 'v', 'w', 'x', 'y', 'z']
regional_indicators = ['๐ฆ', '๐ง', '๐จ', '๐ฉ', '๐ช', '๐ซ', '๐ฌ', '๐ญ', '๐ฎ', '๐ฏ', '๐ฐ', '๐ฑ', '๐ฒ', '๐ณ', '๐ด',
'๐ต', '๐ถ', '๐ท', '๐ธ', '๐น']
# '๐บ', '๐ป', '๐ผ', '๐ฝ', '๐พ', '๐ฟ']
desc_str = ''
text = '๐** ' + question + '**'
if choices:
for i in range(0, len(choices)):
e_dict = emoji.emoji_list(choices[i])
# print(e_dict)
e = ''
try:
e = e_dict[0]['emoji']
except IndexError:
pass
if e:
choices_emojis.append(e[0])
desc_str_i = choices[i]
if desc_str_i[0] == e[0]:
desc_str = desc_str + e[0] + 'แ
' + desc_str_i[1:] + '\n'
else:
desc_str = desc_str + e[0] + 'แ
' + desc_str_i + '\n'
else:
# print(i)
choices_emojis.append(regional_indicators[i])
desc_str = desc_str + regional_indicators[i] + 'แ
' + choices[i] + '\n'
embed = discord.Embed(description=desc_str, color=embed_color)
interaction = await ctx.respond(text, embed=embed)
for e0 in choices_emojis:
msg = await interaction.original_response()
# print(emoji)
await msg.add_reaction(e0)
else:
interaction = await ctx.respond(text)
msg = await interaction.original_response()
await msg.add_reaction('๐')
await msg.add_reaction('๐')
@bot.message_command(name='Close poll')
async def poll_results(ctx: discord.ApplicationContext, message: discord.Message):
if message.author.id != bot.user.id:
await ctx.respond("Oops.. It seems it's not my message :(", ephemeral=True)
return
if message.embeds[0].footer:
await ctx.respond("This poll is already closed", ephemeral=True)
return
all_reactions = message.reactions
reactions = []
for reaction in all_reactions:
async for i in reaction.users():
if i.id == bot.user.id:
reactions.append(reaction)
continue
print(reactions)
reactions.sort(key=lambda x: x.count, reverse=True)
print(reactions)
def squares(n):
return ''.join([':blue_square:' for _ in range(n)])
info = {}
text = message.embeds[0].description
for line in text.splitlines(False):
ind = line.find('แ
')
key = line[:ind]
value = line[ind + 1:]
print(f'[{key}], [{value}]')
info[key] = value
max_reactions = reactions[0].count - 1
texts = []
for i in reactions:
if max_reactions != 0:
num = round((i.count - 1) * 20 / max_reactions)
else:
num = 0
e = str(i.emoji)
print(num, e)
choice = info[e]
text = squares(num) + '\n' + e + 'แ
' + choice + 'แ
' + str(i.count - 1)
texts.append(text)
desc = '\n\n'.join(texts)
embed = discord.Embed(title="Results:", description=desc, color=embed_color)
embed.set_footer(text='Poll closed')
await message.edit(embed=embed)
await ctx.respond("Done results", ephemeral=True)
@bot.event
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
channel = bot.get_channel(payload.channel_id)
pre_message = channel.get_partial_message(payload.message_id)
message = await pre_message.fetch()
if message.author.id != bot.user.id:
return
if message.embeds[0].footer:
member = bot.get_guild(payload.guild_id).get_member(payload.user_id)
await message.remove_reaction(payload.emoji, member)
if __name__ == '__main__':
bot.run(token)