1
1
import discord
2
- from discord .ext .commands import UserInputError
2
+ from discord .ext .commands import UserInputError , CommandError
3
3
4
4
import re
5
5
import string
6
6
import asyncio
7
7
import typing
8
8
from io import BytesIO
9
- from urllib .parse import urlparse
10
9
from datetime import datetime , timedelta
11
10
from traceback import print_exc
12
11
13
12
from core .decorators import async_executor
14
- from core .utils import is_image_url , days
13
+ from core .utils import is_image_url , days , match_user_id , parse_image_url
15
14
from core .objects import Bot , ThreadManagerABC , ThreadABC
16
15
17
16
from colorthief import ColorThief
@@ -24,10 +23,12 @@ def __init__(self, manager: 'ThreadManager',
24
23
recipient : typing .Union [discord .Member , discord .User ],
25
24
channel : typing .Union [discord .DMChannel ,
26
25
discord .TextChannel ]):
26
+ if recipient .bot :
27
+ raise CommandError ('Recipient cannot be a bot.' )
28
+
27
29
self .manager = manager
28
30
self .bot = manager .bot
29
31
self ._id = recipient .id
30
- # TODO: recipient should not be bot
31
32
self ._recipient = recipient
32
33
self ._channel = channel
33
34
self ._ready_event = asyncio .Event ()
@@ -465,21 +466,19 @@ async def _find_from_channel(self, channel):
465
466
"""
466
467
user_id = None
467
468
468
- if channel .topic and 'User ID: ' in channel . topic :
469
- user_id = int ( re . findall ( r'\d+' , channel .topic )[ 0 ] )
469
+ if channel .topic :
470
+ user_id = match_user_id ( channel .topic )
470
471
471
472
# BUG: When discord fails to create channel topic.
472
473
# search through message history
473
474
elif channel .topic is None :
474
- async for message in channel .history (limit = 50 ):
475
+ async for message in channel .history (limit = 100 ):
475
476
if message .embeds :
476
477
embed = message .embeds [0 ]
477
- # TODO: use re.search instead
478
- matches = re .findall (r'User ID: (\d+)' ,
479
- str (embed .footer .text ))
480
- if matches :
481
- user_id = int (matches [0 ])
482
- break
478
+ if embed .footer .text :
479
+ user_id = match_user_id (embed .footer .text )
480
+ if user_id is not None :
481
+ break
483
482
484
483
if user_id is not None :
485
484
if user_id in self .cache :
@@ -536,7 +535,7 @@ async def create(self, recipient, *, creator=None, category=None):
536
535
self .cache [recipient .id ] = thread
537
536
538
537
log_url = await self .bot .api .create_log_entry (recipient , channel ,
539
- creator or recipient ),
538
+ creator or recipient )
540
539
541
540
log_data = await self .bot .api .get_user_logs (recipient .id )
542
541
# await self.get_dominant_color(recipient.avatar_url)
@@ -565,27 +564,17 @@ async def find_or_create(self, recipient):
565
564
return await self .find (recipient = recipient ) or \
566
565
await self .create (recipient )
567
566
568
- @staticmethod
569
- def valid_image_url (url ):
570
- """Checks if a url leads to an image."""
571
- types = ['.png' , '.jpg' , '.gif' , '.jpeg' , '.webp' ]
572
- parsed = urlparse (url )
573
- if any (parsed .path .endswith (i ) for i in types ):
574
- # TODO: Replace this logic with urlsplit/urlunsplit
575
- return url .replace (parsed .query , 'size=128' )
576
- return ''
577
-
578
567
@async_executor ()
579
568
def _do_get_dc (self , image , quality ):
580
569
with BytesIO (image ) as f :
581
570
return ColorThief (f ).get_color (quality = quality )
582
571
583
572
async def get_dominant_color (self , url = None , quality = 10 ):
584
573
"""
585
- Returns the dominant color of an image from a url
574
+ Returns the dominant color of an image from a URL
586
575
(misc)
587
576
"""
588
- url = self . valid_image_url (url )
577
+ url = parse_image_url (url )
589
578
590
579
if not url :
591
580
raise ValueError ('Invalid image url passed.' )
0 commit comments