Skip to content

Commit

Permalink
[ReTrigger] 2.27.1 Fix handling of uploaded files with discord's new …
Browse files Browse the repository at this point in the history
…CDN rules
  • Loading branch information
TrustyJAID committed Feb 7, 2024
1 parent effe513 commit c11ad0d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TrustyJAID's Cogs for [Red-DiscordBot](https://github.com/Cog-Creators/Red-Disc
| NotSoBot | 2.5.5 | <details><summary>Some working commands from NotSoBot</summary>Magick, trigger and manipulate images with many commands from NotSoSuper's NotSoBot. This cog has a lot of requirements, view the [cog README.md](https://github.com/TrustyJAID/Trusty-cogs/blob/master/notsobot/README.md) for details. </details> | NotSoSuper and TrustyJAID |
| Reddit | 1.2.0 | <details><summary>A cog to post updates from reddit.</summary>Reddit commands for getting updates on specified subreddits.</details> | TrustyJAID |
| Rekt | 1.0.0 | <details><summary>Get REKT</summary>Are you REKT?</details> | TrustyJAID |
| ReTrigger | 2.27.0 | <details><summary>Trigger events via Regular Expressions!</summary>Trigger events based on regex! Check out <https://regex101.com/> and <https://github.com/TrustyJAID/Trusty-cogs/blob/master/retrigger/README.md> for help setting up the cog. Note: This cog can become quite resource heavy. Optional features are available if the requirements are present such as pillow for image resizing and pytesseract to scan images for text (OCR).</details> | TrustyJAID |
| ReTrigger | 2.27.1 | <details><summary>Trigger events via Regular Expressions!</summary>Trigger events based on regex! Check out <https://regex101.com/> and <https://github.com/TrustyJAID/Trusty-cogs/blob/master/retrigger/README.md> for help setting up the cog. Note: This cog can become quite resource heavy. Optional features are available if the requirements are present such as pillow for image resizing and pytesseract to scan images for text (OCR).</details> | TrustyJAID |
| RoleTools | 1.5.11 | <details><summary>Various role related tools.</summary>Various role utility commands. Including Reaction roles, Sticky roles, and Auto role.</details> | TrustyJAID |
| runescape | 1.5.1 | <details><summary>Show your Runescape stats in discord!</summary>A cog to grab Runescape and OSRS stats and profile information.</details> | TrustyJAID |
| ServerStats | 1.8.0 | <details><summary>A plethora of potentially useful commands for any bot owner.</summary>A plethora of potentially useful commands for any bot owner. Includes a way to track the bot joining new servers, find cheaters on global economies, get user avatars and even larger emojis.</details> | TrustyJAID and Preda |
Expand Down
6 changes: 6 additions & 0 deletions retrigger/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ async def is_mod_or_admin(self, member: discord.Member) -> bool:
async def make_guild_folder(self, directory) -> None:
raise NotImplementedError()

@abstractmethod
async def save_attachment_location(
self, attachment: discord.Attachment, guild: discord.Guild
) -> Optional[str]:
raise NotImplementedError()

@abstractmethod
async def save_image_location(self, image_url: str, guild: discord.Guild) -> Optional[str]:
raise NotImplementedError()
Expand Down
5 changes: 2 additions & 3 deletions retrigger/retrigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ReTrigger(
"""

__author__ = ["TrustyJAID"]
__version__ = "2.27.0"
__version__ = "2.27.1"

def __init__(self, bot):
super().__init__()
Expand Down Expand Up @@ -1810,8 +1810,7 @@ async def image(
msg = await self.wait_for_image(ctx)
if not msg or not msg.attachments:
return
image_url = msg.attachments[0].url
filename = await self.save_image_location(image_url, guild)
filename = await self.save_attachment_location(msg.attachments[0], guild)
if not filename:
await ctx.send(_("That is not a valid file link."))
return
Expand Down
19 changes: 15 additions & 4 deletions retrigger/triggerhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
RE_CTX: re.Pattern = re.compile(r"{([^}]+)\}")
RE_POS: re.Pattern = re.compile(r"{((\d+)[^.}]*(\.[^:}]+)?[^}]*)\}")
LINK_REGEX: re.Pattern = re.compile(
r"(http[s]?:\/\/[^\"\']*\.(?:png|jpg|jpeg|gif|mp3|mp4|webp))", flags=re.I
r"(http[s]?:\/\/[^\"\']*\.(?:png|jpg|jpeg|gif|mp3|mp4|webp)).*", flags=re.I
)
IMAGE_REGEX: re.Pattern = re.compile(
r"(?:(?:https?):\/\/)?[\w\/\-?=%.]+\.(?:png|jpg|jpeg|webp)+", flags=re.I
Expand Down Expand Up @@ -108,18 +108,29 @@ async def make_guild_folder(self, directory) -> None:
log.info("Creating guild folder")
directory.mkdir(exist_ok=True, parents=True)

async def save_attachment_location(
self, attachment: discord.Attachment, guild: discord.Guild
) -> Optional[str]:
seed = "".join(random.sample(string.ascii_uppercase + string.digits, k=5))
filename = "{}-{}".format(seed, attachment.filename)
directory = cog_data_path(self).joinpath(str(guild.id))
file_path = cog_data_path(self).joinpath(str(guild.id), str(filename))
await self.make_guild_folder(directory)
await attachment.save(file_path)
return filename

async def save_image_location(self, image_url: str, guild: discord.Guild) -> Optional[str]:
good_image_url = LINK_REGEX.search(image_url)
if not good_image_url:
return None
seed = "".join(random.sample(string.ascii_uppercase + string.digits, k=5))
filename = good_image_url.group(1).split("/")[-1]
filename = "{}-{}".format(seed, filename)
directory = cog_data_path(self) / str(guild.id)
file_path = str(cog_data_path(self)) + f"/{guild.id}/{filename}"
directory = cog_data_path(self).joinpath(str(guild.id))
file_path = cog_data_path(self).joinpath(str(guild.id), str(filename))
await self.make_guild_folder(directory)
async with aiohttp.ClientSession() as session:
async with session.get(good_image_url.group(1)) as resp:
async with session.get(good_image_url.group(0)) as resp:
test = await resp.read()
with open(file_path, "wb") as f:
f.write(test)
Expand Down

0 comments on commit c11ad0d

Please sign in to comment.