-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
converter.py
92 lines (85 loc) · 3.69 KB
/
converter.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
import re
from typing import List, Pattern, Union
import discord
import unidecode
from discord.ext.commands.converter import Converter
from discord.ext.commands.errors import BadArgument
from redbot.core import commands
IMAGE_LINKS: Pattern = re.compile(
r"(https?:\/\/[^\"\'\s]*\.(?:png|jpg|jpeg|gif|png|svg)(\?size=[0-9]*)?)", flags=re.I
)
EMOJI_REGEX: Pattern = re.compile(r"(<(a)?:[a-zA-Z0-9\_]+:([0-9]+)>)")
MENTION_REGEX: Pattern = re.compile(r"<@!?([0-9]+)>")
ID_REGEX: Pattern = re.compile(r"[0-9]{17,}")
class ImageFinder(Converter):
"""
This is a class to convert notsobots image searching capabilities
into a more general converter class
"""
async def convert(
self, ctx: commands.Context, argument: str
) -> List[Union[discord.Asset, str]]:
attachments = ctx.message.attachments
mentions = MENTION_REGEX.finditer(argument)
matches = IMAGE_LINKS.finditer(argument)
emojis = EMOJI_REGEX.finditer(argument)
ids = ID_REGEX.finditer(argument)
urls = []
if matches:
for match in matches:
urls.append(match.group(1))
if emojis:
for emoji in emojis:
p_emoji = discord.PartialEmoji.from_str(emoji.group(1))
urls.append(p_emoji)
if mentions:
for mention in mentions:
user = ctx.guild.get_member(int(mention.group(1)))
if user is None:
continue
if user.display_avatar.is_animated():
urls.append(user.display_avatar.replace(format="gif", size=1024))
else:
urls.append(user.display_avatar.replace(format="png", size=1024))
if not urls and ids:
for possible_id in ids:
user = ctx.guild.get_member(int(possible_id.group(0)))
if user is None:
continue
if user.display_avatar.is_animated():
urls.append(user.display_avatar.replace(format="gif", size=1024))
else:
urls.append(user.display_avatar.replace(format="png", size=1024))
if attachments:
for attachment in attachments:
urls.append(attachment.url)
if not urls:
for m in ctx.guild.members:
if argument.lower() in unidecode.unidecode(m.display_name.lower()):
# display_name so we can get the nick of the user first
# without being NoneType and then check username if that matches
# what we're expecting
urls.append(m.display_avatar.replace(format="png", size=1024))
continue
if argument.lower() in unidecode.unidecode(m.name.lower()):
urls.append(m.display_avatar.replace(format="png", size=1024))
continue
if not urls:
raise BadArgument("No images provided.")
return urls
async def search_for_images(
self, ctx: commands.Context
) -> List[Union[discord.Asset, discord.Attachment, str]]:
urls = []
if not ctx.channel.permissions_for(ctx.me).read_message_history:
raise BadArgument("I require read message history perms to find images.")
async for message in ctx.channel.history(limit=10):
if message.attachments:
for attachment in message.attachments:
urls.append(attachment)
match = IMAGE_LINKS.match(message.content)
if match:
urls.append(match.group(1))
if not urls:
raise BadArgument("No Images found in recent history.")
return urls