-
Notifications
You must be signed in to change notification settings - Fork 6
/
handler.py
354 lines (264 loc) · 11.2 KB
/
handler.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import json
import functools
import discord
import time
import asyncio
import sqlib
import urllib.request
def get_config(key: str):
with open('./data/config.json', 'r', encoding='utf8') as f:
config = json.load(f)
return config[key]
def concat_elements(iterable: list or tuple or dict, space: str=" "):
if iterable is None:
return None
elif len(iterable) == 0:
return ""
return functools.reduce(lambda x, y: f"{x}{space}{y}", iterable)
def get_commands():
with open('./data/commands.json', 'r', encoding='utf8') as f:
return json.load(f)
def get_cmd_by_alias(alias: str):
commands = get_commands()
for cmd in commands:
if alias in commands[cmd]['aliases']:
return cmd
def get_aliases(cmd: str, prefix: str=None):
commands = get_commands()
aliases = commands[cmd]['aliases']
if prefix is not None:
aliases = tuple(map(lambda alias: prefix + alias, aliases))
else:
aliases = tuple(aliases)
return aliases
def get_help_text(cmd: str, prefix):
commands = get_commands()
return concat_elements(commands[cmd]['help'], space="").format(prefix=prefix)
def get_help_embed(alias: str, prefix):
commands = get_commands()
if alias in commands:
cmd = alias
else:
cmd = get_cmd_by_alias(alias)
help_text = get_help_text(cmd, prefix)
embed = discord.Embed(
title=cmd,
description=help_text,
color=int(get_config('color'), 16)
)
embed.set_footer(
text=get_config('default_footer')
)
return embed
def get_all_aliases(prefix=None):
commands = get_commands()
aliases = []
for cmd in commands:
for alias in get_aliases(cmd, prefix=prefix):
aliases.append(alias)
return tuple(aliases)
def alias_in(content: str, cmd: str, prefix=None):
aliases = get_aliases(cmd, prefix=prefix)
return content.lower().startswith(aliases)
def get_cmd_content(content: str):
content = content.split(' ')
if len(content) > 1:
content = concat_elements(content[1:])
return content
else:
return ""
def get_aliases_str(cmd: str, prefix=None):
aliases = get_aliases(cmd, prefix=prefix)
aliases_str = concat_elements(aliases, space=', ')
return aliases_str
def get_aliases_embed(alias: str, prefix=None):
cmd = get_cmd_by_alias(alias)
aliases_str = get_aliases_str(cmd, prefix=prefix)
aliases_embed = discord.Embed(
title=f"`{cmd}` aliases",
description=aliases_str,
color=int(get_config('color'), 16)
)
aliases_embed.set_footer(
text=get_config('default_footer')
)
return aliases_embed
def get_leading_options(options: dict):
options = list(map(lambda o: (o, options[o]), options))
options = sorted(options, key=lambda o: o[1], reverse=True)
highest = options[0][1]
leading_emojis = list(map(lambda o: o[0], filter(lambda o: o[1] == highest, options)))
if len(leading_emojis) == len(options):
leading_emojis = ["`A draw`"]
return concat_elements(leading_emojis, ", "), highest
async def refresh_vote_msg(message: discord.Message, options: dict, duration: int, coro_client: discord.Client,
clock=True, notify=False):
leading_options = get_leading_options(options)
if clock:
clock_emoji = f":clock{duration % 12 if duration % 12 != 0 else 12}:"
else:
clock_emoji = ""
if leading_options[1] == 1:
vote_plural = ""
else:
vote_plural = "s"
if duration == 0:
if len(leading_options[0]) > 1:
winner_plural = "s"
else:
winner_plural = ""
winners = "Final winner{0}: {1[0]} with {1[1]} vote{2}.".format(winner_plural, leading_options, vote_plural)
await message.edit(content=f"The voting is over.\n{winners}")
if notify:
await message.channel.send(f":bell: Voting is over. :bell: \n")
else:
if notify:
notification = "Notification turned on. :bell: \n"
else:
notification = ""
await message.edit(content="The voting is over in {0} minutes. {clock_emoji} \n"
"{notification}"
"Currently leading: {1[0]} with {1[1]} vote{2}."
"".format(duration, leading_options, vote_plural,
clock_emoji=clock_emoji,
notification=notification)
)
return message
async def timer(client: discord.Client, vote_id: str, notify: bool=False):
t_needed = 0
while not client.is_closed():
await asyncio.sleep(60 - t_needed)
t_start = time.time()
vote = sqlib.votes.get(vote_id)
msg_id, options, duration, channel_id = vote
duration -= 1
sqlib.votes.update(msg_id, {"duration": duration})
channel = client.get_channel(int(channel_id))
try:
message = await channel.get_message(msg_id)
except AttributeError:
print("AttributeError")
continue
await refresh_vote_msg(message, json.loads(options), duration, client, notify=notify)
if duration == 0:
break
t_end = time.time()
t_needed = t_end - t_start
def handle_commands(client):
def decorated(func):
@functools.wraps(func)
async def wrapper(message):
if isinstance(message.channel, discord.DMChannel):
if message.author != client.user:
await message.channel.send("Sorry, I can't help you in a private chat.")
return None
client_member = message.guild.get_member(client.user.id)
if not client_member.permissions_in(message.channel).send_messages:
return None
prefix = sqlib.server.get(message.guild.id, 'prefix')
if prefix is None:
prefix = get_config('prefix')
sqlib.server.add_element(message.guild.id, {'prefix': prefix})
else:
prefix = prefix[0]
all_aliases = get_all_aliases(prefix=prefix)
embed_color = int(get_config('color'), 16)
footer = get_config('default_footer')
if message.content.lower().startswith(all_aliases): # checks if message is a command
async with message.channel.typing():
pass
if alias_in(message.content, 'help', prefix=prefix):
content = get_cmd_content(message.content)
if content.lower() in get_all_aliases(prefix=None):
help_embed = get_help_embed(content.lower(), prefix)
else:
commands = get_commands()
help_embed = discord.Embed(
title="Help",
description="All commands and how to use them.",
color=embed_color,
url=get_config("website")
)
for cmd in commands:
help_embed.add_field(
name=cmd,
value=get_help_text(cmd, prefix),
inline=False
)
help_embed.set_footer(
text=footer
)
help_embed.set_thumbnail(url=get_config("thumbnail"))
if message.author.guild_permissions.administrator:
await message.channel.send(embed=help_embed)
else:
try:
await message.author.send(embed=help_embed)
await message.channel.send("Help sent. :white_check_mark:")
except discord.Forbidden:
await message.channel.send("You have to turn on your PM.")
elif alias_in(message.content, 'aliases', prefix=prefix):
content = get_cmd_content(message.content)
if content.lower() in get_all_aliases(prefix):
aliases_embed = get_aliases_embed(content.lower())
else:
commands = get_commands()
aliases_embed = discord.Embed(
title="Aliases",
description="The aliases of all commands.",
color=embed_color
)
for cmd in commands:
aliases_embed.add_field(
name=cmd,
value=get_aliases_str(cmd),
inline=False
)
aliases_embed.set_footer(
text=footer
)
await message.channel.send(embed=aliases_embed)
else:
content = get_cmd_content(message.content)
cmd_without_prefix = message.content.split(' ')[0][
len(prefix):] # splits command from prefix and other content
if content.lower().startswith('help'):
help_embed = get_help_embed(cmd_without_prefix, prefix)
await message.channel.send(embed=help_embed) # sends cmd specific help
elif content.lower().startswith('aliases'):
aliases_embed = get_aliases_embed(cmd_without_prefix)
await message.channel.send(embed=aliases_embed)
else:
return await func(message)
elif client.user in message.mentions:
await message.channel.send(
f"Type `{sqlib.server.get(message.guild.id, 'prefix')[0]}help` to see the command list!"
)
return await func(message)
return None # exits if it's not in command list -> !!! Add new commands to 'commands.json' !!!
return wrapper
return decorated
def post_to_apis(client: discord.Client()):
domains = {
'discordbots.org': get_config('API_TOKEN(discordbots.org)'),
'bots.discord.pw': get_config('API_TOKEN(bots.discord.pw)')
}
for domain in domains:
count_json = json.dumps({
"server_count": len(client.guilds),
"shard_count": client.shard_count,
"shard_id": client.shard_id
})
# Resolve HTTP redirects
api_redirect_url = "https://{0}/api/bots/{1}/stats".format(domain, client.user.id)
# Construct request and post server count
api_req = urllib.request.Request(api_redirect_url)
api_req.add_header(
"Content-Type",
"application/json"
)
api_req.add_header(
"Authorization",
domains[domain]
)
urllib.request.urlopen(api_req, count_json.encode("ascii"))