-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
299 lines (233 loc) · 9.97 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from handler import *
client = discord.AutoShardedClient()
@client.event
async def on_ready():
print("Logged in as:")
print(client.user.name)
print("=============")
all_votes = sqlib.votes.get_all()
pending_votes = list(filter(lambda v: v[2] > 0, all_votes))
for vote in pending_votes:
client.loop.create_task(timer(client, vote[0]))
post_to_apis(client)
@client.event
@handle_commands(client)
async def on_message(message):
prefix = sqlib.server.get(message.guild.id, 'prefix')[0]
if alias_in(message.content, 'vote', prefix=prefix):
content = get_cmd_content(message.content)
title = "Vote/Poll"
options = None
duration = get_config('default_duration')
notify_when_ending = False
async def command_error(reason=" :confused: "):
await message.channel.send("Command error: *{0}*\n"
"Type `{prefix}vote help` to check how it works!"
"".format(reason, prefix=prefix))
if content.count('"') > 1:
subcommands = list(map(lambda x: x.strip(), content.split('"')))
last_item = subcommands.pop() # delete empty item at the end
if "--notify" in last_item or "-N" in last_item:
notify_when_ending = True
skip_next = False
try:
for i in range(len(subcommands)):
if skip_next:
skip_next = False
continue
subcmd = subcommands[i]
nextone = subcommands[i+1]
if any(x in subcmd for x in ["-T", "--title"]):
title = nextone
skip_next = True
elif any(x in subcmd for x in ["-O", "--options"]):
options = nextone
skip_next = True
elif any(x in subcmd for x in ["-D", "--duration"]):
duration = int(nextone)
skip_next = True
if any(x in subcmd for x in ["-N", "--notify"]):
notify_when_ending = True
if options is None:
await command_error("No options set.")
return 0
if duration < 1:
await command_error("Duration has to be at least 1 minute.")
return None
elif duration > 60:
await command_error("max. duration are 60 minutes.")
return None
except ValueError:
await command_error("Duration has to be a number.")
return 0
except Exception:
await command_error("Invalid syntax.")
return 0
else:
content = content.split("|")
if len(content) == 2:
title = content[0]
options = content[1]
else:
options = content[0]
vote_embed = discord.Embed(
title=title,
description="Use the reactions to vote!",
color=int(get_config('color'), 16)
)
vote_embed.set_author(name=message.author.name, icon_url=message.author.avatar_url)
vote_embed.set_footer(text=get_config('default_footer'))
options = options.split(';')
if len(options) > 20:
await command_error("To many options were given. (max. 20)")
return 0
elif len(options) < 2:
await command_error("You have to set at least 2 options. Otherwise this makes no sense . . .")
return 0
emojis = []
for i in range(len(options)): # generates unicode emojis and embed-fields
hex_str = hex(224 + (6 + i))[2:]
reaction = b'\\U0001f1a'.replace(b'a', bytes(hex_str, "utf-8"))
reaction = reaction.decode("unicode-escape")
emojis.append(reaction)
if len(options[i]) == 0:
await command_error("Empty options are not allowed.")
return None
vote_embed.add_field(
name=reaction,
value=options[i],
inline=False
)
msg = await message.channel.send(embed=vote_embed)
emoji_dict = {}
for emoji in emojis:
await msg.add_reaction(emoji)
emoji_dict[emoji] = 0
sqlib.votes.add_element(msg.id, {"options": json.dumps(emoji_dict),
"duration": duration,
"channel": msg.channel.id})
await refresh_vote_msg(msg, emoji_dict, int(duration), client, notify=notify_when_ending)
client.loop.create_task(timer(client, msg.id, notify=notify_when_ending))
elif alias_in(message.content, "yes-no", prefix=prefix):
await message.add_reaction("👍")
await message.add_reaction("👎")
elif alias_in(message.content, "invite", prefix=prefix):
invite = discord.Embed(
title="Invite Me!",
color=int(get_config('color'), 16),
url=get_config('invite_link')
)
invite.set_thumbnail(url=get_config("thumbnail"))
invite.set_footer(text=get_config('default_footer'))
await message.channel.send(embed=invite)
elif alias_in(message.content, "support", prefix=prefix):
await message.channel.send(get_config('support_guild'))
elif alias_in(message.content, "prefix", prefix=prefix):
if not message.author.guild_permissions.administrator:
await message.channel.send("You have to admin to change the prefix.")
return None
content = get_cmd_content(message.content).lower()
if len(content) == 0:
await message.channel.send("Prefix too short.")
elif len(content) > 2:
await message.channel.send("Prefix too long. (max. 2 chars)")
else:
sqlib.server.update(message.guild.id, {'prefix': content})
await message.channel.send(f"Okay, new prefix is `{content}`.")
elif alias_in(message.content, "info", prefix=prefix):
infotext = discord.Embed(
title="VoteIt",
description="About the bot.",
color=int(get_config('color'), 16),
url=get_config('website')
)
infotext.set_author(
name="Linus Bartsch | LiBa01#8817",
url="https://liba001.github.io/",
icon_url="https://avatars0.githubusercontent.com/u/30984789?s=460&v=4"
)
infotext.set_thumbnail(
url=get_config('thumbnail')
)
infotext.add_field(
name="Developer",
value="Name: **Linus Bartsch**\n"
"Discord: **LiBa01#8817**\n"
"GitHub: [LiBa001](https://github.com/LiBa001)\n"
"I'm also at [Discordbots.org](https://discordbots.org/user/269959141508775937).\n"
"I'd be very happy if you'd support me on "
"[**Patreon**](https://www.patreon.com/user?u=8320690). :heart:\n",
inline=True
)
infotext.add_field(
name="Developed in:",
value="Language: **Python3.6**\n"
"Library: **discord.py** (0.16.12)\n"
)
infotext.add_field(
name="Commands",
value="Type `{0}help` to get all commands.\n"
"Join the [Official Support guild](https://discord.gg/z3X3uN4) "
"if you have any questions or suggestions (or just to be one of the cool guys :sunglasses:)."
"".format(prefix)
)
infotext.add_field(
name="Stats",
value="guild count: **{0}**\n"
"Uptime: **{1}** hours, **{2}** minutes\n"
"Member count: **{3}**".format(len(client.guilds), up_hours, up_minutes,
len(list(client.get_all_members())))
)
infotext.set_footer(
text="Special thanks to MaxiHuHe04#8905 who supported me a few times."
)
await message.channel.send(embed=infotext)
elif alias_in(message.content, "donate", prefix=prefix):
await message.channel.send("I'd be very thankful for your support. :heart:\n"
"{0}".format(get_config('patreon')))
async def update_votes(reaction, user):
if user.id != client.user.id:
duration = sqlib.votes.get(reaction.message.id, "duration")
if duration is None: # proves if message is in database
return None
else:
duration = duration[0]
if duration == 0:
return None
options = json.loads(sqlib.votes.get(reaction.message.id, "options")[0])
emoji = reaction.emoji
if emoji in options:
votes = reaction.count - 1
options[emoji] = votes
sqlib.votes.update(reaction.message.id, {"options": json.dumps(options)})
if ":bell:" in reaction.message.content:
notify = True
else:
notify = False
await refresh_vote_msg(reaction.message, options, duration, client, notify=notify)
@client.event
async def on_reaction_add(reaction, user):
await update_votes(reaction, user)
@client.event
async def on_reaction_remove(reaction, user):
await update_votes(reaction, user)
@client.event
async def on_guild_join(guild):
post_to_apis(client)
@client.event
async def on_guild_remove(guild):
post_to_apis(client)
async def uptime_count():
await client.wait_until_ready()
global up_hours
global up_minutes
up_hours = 0
up_minutes = 0
while not client.is_closed:
await asyncio.sleep(60)
up_minutes += 1
if up_minutes == 60:
up_minutes = 0
up_hours += 1
client.loop.create_task(uptime_count())
client.run(get_config('token'))