-
Notifications
You must be signed in to change notification settings - Fork 0
/
fun.py
616 lines (573 loc) · 28.4 KB
/
fun.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# -*- coding: utf-8 -*-
from ast import literal_eval
from asyncio import TimeoutError as WaitTimeout
from datetime import datetime as d
from json import loads as json_loads
from random import choice, randint
from typing import List, Union, Optional
from discord import Forbidden, TextChannel, NotFound, User, Message
from discord.ext.commands import Cog, command, Context, cooldown, BucketType, has_permissions, \
CommandInvokeError, CommandError
from praw import Reddit
from requests import request
from enigma.emebds.core import ErrorEmbed, InfoEmbed, SuccessEmbed
from enigma.emebds.errors import TimeoutEmbed
from enigma.emebds.misc import PleaseWaitEmbed
from enigma.settings import in_production, reddit_client_id, reddit_client_secret, reddit_user_agent, rapid_api_key
from enigma.utils.database import create_giveaway, get_giveaway_from_message, delete_giveaway
from enigma.utils.exceptions import DatabaseError
from enigma.utils.strings import number_suffix
class Fun(Cog):
def __init__(self, bot):
self.bot = bot
# How to send images from imigur
# https://stackoverflow.com/questions/57043797/discord-py-getting-random-imgur-images
@has_permissions(manage_guild=True)
@cooldown(1, 30, BucketType.guild)
@command(
name='giveaway',
brief='Initiates a giveaway',
help='Available options:\n'
'- create, start, new;'
' with argument <channel>\n'
'- delete, stop, end;'
' with argument <message ID>\n'
' Message ID could be found under the giveaway message.\n'
'Maximum item\'s length is 30 characters and maximum amount is 25.',
usage='<option> <additional argument> [group result by]',
aliases=['ga'],
enabled=in_production()
)
async def giveaway(self, ctx: Context, option: str = '', arg1: Union[TextChannel, int] = None):
if not option:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Please specify an option'
))
else:
def check(m):
return m.channel == ctx.channel and m.author == ctx.author
if option in ['create', 'start', 'new']:
if not arg1:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Missing channel'
))
elif type(arg1) is not TextChannel:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Bad channel format'
))
else:
perms = arg1.permissions_for(ctx.guild.get_member(self.bot.user.id))
if not perms.read_messages:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: I can\'s see that channel'
))
elif not perms.send_messages:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: I can\'s send messages to that channel'
))
else:
msg = await ctx.send(embed=InfoEmbed(
author=ctx.author,
title=':shopping_bags: Create giveaway',
description=f'Final message will be sent to {arg1.mention}.'
))
things: List[List[str, str]] = []
index = 0
info = await ctx.send(embed=InfoEmbed(
author=ctx.author,
title='Preparing...'
))
# Getting items for giveaway
while True:
index += 1
await info.edit(embed=InfoEmbed(
author=ctx.author,
title=f':name_badge: {index}{number_suffix(index)} item name...',
))
try:
# Get item's name
response_1 = await self.bot.wait_for('message', check=check, timeout=30)
except WaitTimeout:
return await info.edit(embed=TimeoutEmbed(author=ctx.author))
else:
# Ended before adding at least one item
if response_1.content.lower() in ['stop', 'end', 'x']:
try:
await response_1.delete()
except Forbidden:
pass
if len(things) == 0:
return await info.edit(embed=ErrorEmbed(
author=ctx.author,
title=':x: Too few things'
))
break
elif len(response_1.content) > 30:
try:
await response_1.delete()
except Forbidden:
pass
await info.edit(embed=ErrorEmbed(
author=ctx.author,
title=':x: Name\'s too long',
description='Keep it under 30 characters.'
))
return
await info.edit(embed=InfoEmbed(
author=ctx.author,
title=f':1234: {index}{number_suffix(index)} item quantity...'
))
try:
# Get item's quantity
response_2 = await self.bot.wait_for('message', check=check, timeout=10)
except WaitTimeout:
await info.edit(embed=TimeoutEmbed(author=ctx.author))
try:
await response_1.delete()
except Forbidden:
pass
return
else:
# Checking if quantity is a number
try:
q = int(response_2.content)
if q < 1:
raise ValueError
except ValueError:
await info.edit(embed=ErrorEmbed(
author=ctx.author,
title=':x: Bad format, quantity must be a number and higher than 0'
))
try:
await response_1.delete()
await response_2.delete()
except Forbidden:
pass
return
if int(response_2.content) > 25:
try:
await response_1.delete()
except Forbidden:
pass
await info.edit(embed=ErrorEmbed(
author=ctx.author,
title=':x: Too much',
description='Keep quantity under 25.'
))
return
await info.edit(embed=PleaseWaitEmbed(author=ctx.author))
things.append([response_1.content, response_2.content])
await msg.edit(embed=msg.embeds[0].add_field(
name=things[-1][1],
value=things[-1][0]
))
try:
await response_1.delete()
await response_2.delete()
except Forbidden:
pass
await info.delete()
final_em = SuccessEmbed(
author=ctx.author,
title=':white_check_mark: Done!'
).set_footer(
text=f'Created by {ctx.author.display_name}'
)
for thing in things:
final_em.add_field(
name=f'x{thing[1]}',
value=thing[0]
)
await msg.edit(embed=final_em)
# Preparing final giveaway message
final_em.title = ':gift: Giveaway!'
final_em.add_field(
name='\u200b',
value='React with `📝` to participate!',
inline=False
)
# Sending final giveaway message
try:
new_g = await arg1.send(embed=final_em)
except Forbidden:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=f':x: I have no permissions to send message in {arg1.mention}'
))
else:
# Adding message ID to footer
await new_g.edit(embed=new_g.embeds[0].add_field(name='\u200b', value=f'{new_g.id}'))
try:
await new_g.add_reaction(emoji='📝')
except Forbidden:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=f':x: I can\'t add emoji to the message in {arg1.mention}'
))
else:
create_giveaway(new_g.id, ctx.guild.id, data=str(things))
elif option in ['delete', 'stop', 'end']:
# Discord ID has 18 digits
if len(str(arg1)) != 18:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Bad ID format'
))
else:
giveaway = get_giveaway_from_message(arg1)
# Giveaway message ID do not exists in database
if giveaway is None:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Giveaway do not exists'
))
# Users shouldn't end giveaway from another guild
elif giveaway.guild_id != ctx.guild.id:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: This giveaway do not belongs to this guild'
))
else:
info = await ctx.send(embed=PleaseWaitEmbed(author=ctx.author))
# Finding giveaway's message
giveaway_message: Optional[Message] = None
for channel in ctx.guild.text_channels:
try:
giveaway_message = await channel.fetch_message(arg1)
except NotFound:
pass
else:
break
# Message do not exists but giveaway do
if giveaway_message is None:
await info.edit(embed=ErrorEmbed(
author=ctx.author,
title=':x: Message not found',
description='Giveaway exists but its message was probably removed,'
' so I\'m removing the giveaway.'
))
if not delete_giveaway(message_id=arg1):
await self.bot.debug_log(
ctx=ctx, e=DatabaseError(f'Unable to delete giveaway with message ID {arg1}')
)
else:
# Claiming giveaway's reactions
participants = []
for reaction in giveaway_message.reactions:
if reaction.emoji == '📝':
async for user in reaction.users():
if not user.bot:
participants.append(user)
break
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Giveaway reaction not found',
description='Deleting giveaway without winners.'
))
await giveaway_message.edit(
embed=giveaway_message.embeds[0].add_field(
name='\u200b',
value=f'Ended on {d.now()[:16]}'
)
)
if not delete_giveaway(message_id=arg1):
await self.bot.debug_log(
ctx=ctx, e=DatabaseError(f'Unable to delete giveaway with message ID {arg1}')
)
# No one added reaction to giveaway
if not participants:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: No participants found',
description='Deleting giveaway without winners.'
))
if not delete_giveaway(message_id=arg1):
await self.bot.debug_log(
ctx=ctx, e=DatabaseError(f'Unable to delete giveaway with message ID {arg1}')
)
else:
# Prepare giveaway's data
giveaway_data: List[List[str, str]] = literal_eval(giveaway.data)
winners = {}
for item in giveaway_data:
possibles = participants.copy()
item_winners = []
for quantity in range(int(item[1])):
if len(possibles) == 0:
possibles = participants.copy()
random_winner = choice(possibles)
item_winners.append(random_winner)
possibles.remove(random_winner)
winners[item[0]] = item_winners
# noinspection SpellCheckingInspection
win_em = SuccessEmbed(
author=ctx.author,
title=':tada: Winners'
)
await info.edit(embed=InfoEmbed(
author=ctx.author,
title='How you\'d like to group result?',
description='Please type `item` or `user`.'
))
try:
response: Message = await self.bot.wait_for('message', check=check, timeout=10)
except WaitTimeout:
await info.edit(embed=TimeoutEmbed(author=ctx.author))
return
r = response.content.lower()
u = ['u', 'user', 'users']
i = ['i', 'item', 'items']
if r in [*u, *i]:
try:
await response.delete()
except Forbidden:
pass
# Group by users
if r in u:
all_users = set()
for item in winners:
for users in winners[item]:
all_users.add(users)
user_items = {}
for user in all_users:
prizes = []
checked = []
for prize in winners:
for winner_user in winners[prize]:
if winner_user.id == user.id:
prizes.append(prize)
n_prizes = []
for prize in prizes:
if prize not in checked:
n_prizes.append(f'{prize} x{prizes.count(prize)}')
checked.append(prize)
user_items[user] = n_prizes
for user in user_items:
win_em.add_field(
name=user.display_name,
value='- ' + '\n- '.join(user_items[user])
)
# Group by items
elif r in i:
for item in winners:
winners_f = {}
for w in [el for i, el in enumerate(winners[item], 1) if
el not in winners[item][i:]]:
winners_f[w] = winners[item].count(w)
win_em.add_field(
name=item,
value='- ' + '\n- '.join(map(
lambda x: x.mention + str(
((" *x" + str(winners_f[x])) + "*") if winners_f[x] > 1 else ""
),
set(sorted(winners[item], key=lambda x: x.display_name))
))
)
else:
await info.edit(embed=ErrorEmbed(
author=ctx.author,
title=f':x: Unknown option `{r}`'
))
return
await ctx.send(embed=win_em)
try:
await ctx.message.delete()
except Forbidden:
pass
await giveaway_message.edit(
embed=giveaway_message.embeds[0].set_footer(text=f'Ended on {str(d.now())[:16]}')
)
if not delete_giveaway(message_id=arg1):
await self.bot.debug_log(
ctx=ctx, e=DatabaseError(f'Unable to delete giveaway with message ID {arg1}')
)
await info.delete()
else:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Invalid option'
))
@giveaway.error
async def giveaway_error(self, ctx: Context, error: Exception):
if isinstance(error, CommandInvokeError):
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: There was a problem with the giveaway',
description='Probably, there are too many items in the giveaway.'
))
else:
raise CommandError(error)
@cooldown(2, 5, BucketType.guild)
@command(
name='iq',
brief='Check your IQ',
usage='[user]',
enabled=in_production()
)
async def iq(self, ctx: Context, user: User = None):
if not user:
user = ctx.author
if user.id == self.bot.user.id:
iq = 200
elif user.bot is True:
iq = 0
else:
iq = int(user.id) % int(user.discriminator) % 115 + 5
description = ''
if iq == 0:
# noinspection SpellCheckingInspection
description = 'Bots are stuuupiiid.'
elif iq < 10:
description = 'Oh my... I didn\'t think it\'s possible to be that stupid.' \
' *(You\'d be great friends with Paimon)*'
elif iq < 25:
description = 'It\'s sickness, you know?'
elif iq < 40:
description = 'Some potatoes are smarter than you.'
elif iq < 60:
description = 'Roomba is smarter than you LMFAO.'
elif iq == 69:
description = 'Nice.'
elif iq < 70:
description = 'In terms of law - you\'re retarded.\n' \
'> Intellectual disability (ID), also known as general learning disability and mental' \
' retardation (MR), is a generalized neurodevelopmental disorder characterized by' \
' significantly impaired intellectual and adaptive functioning. It is defined by an IQ' \
' under 70, in addition to deficits in two or more adaptive behaviors that affect everyday,' \
' general living.'
elif iq < 90:
description = 'It\'s okay, but still not so smart.'
elif iq < 110:
description = 'You\'re normal, ***b o r i n g***.'
elif iq <= 120:
description = 'Okay, your\'e smart and it\'s maximum value. Cheater...'
elif iq == 200:
description = 'Oh my... It\'s super duper smart!!1!'
safe_name = str(user.display_name). \
replace('*', '\\*'). \
replace('_', '\\_'). \
replace('~', '\\~'). \
replace('>', '\\>')
await ctx.send(embed=SuccessEmbed(
author=ctx.author,
title=f':abacus: {safe_name}\'s IQ is {iq}',
description=description
))
@cooldown(1, 6, BucketType.guild)
@command(
name='meme',
brief='Send a meme',
description='Sends meme from r/dankmemes.',
enabled=in_production()
)
async def meme(self, ctx: Context):
reddit = Reddit(
client_id=reddit_client_id(),
client_secret=reddit_client_secret(),
user_agent=reddit_user_agent()
)
posts = []
# noinspection SpellCheckingInspection
for submission in reddit.subreddit('dankmemes').hot(limit=20):
if not submission.archived:
if submission.score > 50:
if not submission.is_self:
if not submission.over_18:
posts.append(submission)
if posts:
await ctx.send(embed=SuccessEmbed(
author=ctx.author,
title=':black_joker: Meme found'
).set_image(
url=choice(posts).url
))
else:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Meme not found',
description='Just try again'
))
# noinspection SpellCheckingInspection
@command(
name='randomnumber',
brief='Sends random number',
description='You can provide maximun and minimum number to choose.',
usage='[max] [min]',
aliases=['randomnum', 'randnumber', 'randnum'],
enabled=in_production()
)
async def randomnumber(self, ctx: Context, max_: int = 10, min_: int = 1):
nums = list(range(min_, max_ + 1))
if len(nums) == 0:
await ctx.send(embed=ErrorEmbed(
author=ctx.author,
title=':x: Invalid argument(s)',
description='Please remember, that first argument is `max` and the second is `min`.'
))
else:
num = choice(nums)
url = f'https://numbersapi.p.rapidapi.com/{num}/math'
headers = {
'x-rapidapi-host': 'numbersapi.p.rapidapi.com',
'x-rapidapi-key': rapid_api_key()
}
querystring = {"fragment": "false", "json": "false"}
response = request("GET", url, headers=headers, params=querystring)
result: Optional[dict] = json_loads(response.text) if response.status_code == 200 else None
em = SuccessEmbed(
author=ctx.author,
title=f':1234: I\'ve chosen {num}'
)
if result and result['found']:
em.add_field(
name='Funfact',
value=f'{result["text"].capitalize()}.'
)
await ctx.send(embed=em)
@command(
name='choice',
brief='Helps with choosing',
usage='<thing1> <thing2> [thingN]',
enabled=in_production()
)
async def choice(self, ctx: Context, *, things: str):
breakpoint()
em = ErrorEmbed
if not things:
title = ':x: No items specified'
elif len(things) < 2:
title = ':x: Too few thing specified'
else:
title = f':abcd: I\'ve chosen {choice(things)}'
em = SuccessEmbed
await ctx.send(embed=em(
author=ctx.author,
title=title
))
@command(
name='coin',
brief='Tosses a coin',
descriptiom='O Valley of Plenty...',
aliases=['toss'],
enabled=in_production()
)
async def coin(self, ctx: Context):
await ctx.send(embed=SuccessEmbed(
author=ctx.author,
title=':small_red_triangle_down: Tails' if randint(1, 2) == 1 else ':small_red_triangle: Heads'
))
# TODO - Joke
# TODO - Slapping
# TODO - Psycho test
# TODO - Random name generator
# TODO - Random color
# TODO - 8ball
# TODO - How to
# TODO - Curiosities/funfacts
def setup(bot):
bot.add_cog(Fun(bot))