diff --git a/cogs/link_fix.py b/cogs/link_fix.py index d5bf6de..e834e0d 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -132,8 +132,9 @@ async def fix_embeds( channel = original_message.channel permissions = channel.permissions_for(original_message.guild.me) - if (not (permissions.send_messages and permissions.embed_links) - or (isinstance(channel, discore.Thread) and (channel.locked or channel.archived))): + is_thread = isinstance(channel, discore.Thread) + can_send = permissions.send_messages_in_threads if is_thread else permissions.send_messages + if not (can_send and permissions.embed_links) or (is_thread and (channel.locked or channel.archived)): return async def render_and_send() -> tuple[list[tuple[str, list[WebsiteLink]]], dict[discore.Message, list[WebsiteLink]]]: @@ -279,6 +280,7 @@ async def webhook_send( 'username': original_message.author.display_name, 'avatar_url': original_message.author.display_avatar.url, 'silent': silent, + 'allowed_mentions': discore.AllowedMentions.none(), 'wait': True } if isinstance(original_message.channel, discore.Thread): @@ -295,10 +297,11 @@ async def wait_for_embed(message: discore.Message) -> bool: if message.embeds: return True - def check(before: discore.Message, after: discore.Message) -> bool: - return after.id == message.id and len(after.embeds) > len(before.embeds) + def check(payload: discore.RawMessageUpdateEvent) -> bool: + return payload.message_id == message.id and bool(payload.data.get('embeds')) try: - await discore.Bot.get().wait_for('message_edit', check=check, timeout=6) + # The raw event, as `message_edit` is only dispatched for the messages still in the cache (the last 1000) + await discore.Bot.get().wait_for('raw_message_edit', check=check, timeout=6) except asyncio.TimeoutError: return True if message.embeds else False return True @@ -341,6 +344,7 @@ async def on_message(self, message: discore.Message) -> None: if ( message.author == message.guild.me + or message.application_id == self.bot.application_id or not message.content or not message.channel or not message.guild @@ -359,7 +363,7 @@ async def on_message(self, message: discore.Message) -> None: if not links: return if any( - re.search(rf"\b{re.escape(k)}\b", message.content) for k in guild.keywords + re.search(rf"(? tuple[str | None, str | None]: async def get_author_url(self) -> tuple[str | None, str | None]: if not self.match.groupdict().get('username'): return None, None - username = self.match["username"] - user_link = (await self.get_original_url())[0].split(username)[0] + username - return user_link, username + # Cut where the route found the username, as the same text can appear earlier (`instagram.com/instagram`) + author_url = self.url[:self.match.end('username')] + return re.sub(r'^https?://(?:www\.)?', 'https://', author_url, flags=re.IGNORECASE), self.match['username'] @call_if_valid async def get_original_url(self) -> tuple[str | None, str | None]: @@ -321,7 +321,7 @@ def generate_regex(domain_names: str|list[str], route: str, params: list[str] | query_string_param_regexes = [rf"(?:(?=(?:\?|.*&){param}=(?P<{param}>[^&#]+)))?" for param in params] query_string_regex = r"/?(?:" + ''.join(query_string_param_regexes) + r"\?[^#]+)?\??" - return re.compile(r"https?://(?:(?P[^.]+)\.)?" + domain_regex + route_regex + query_string_regex + r"(?:#(?P.+)?)?", re.IGNORECASE) + return re.compile(r"https?://(?:(?P[\w-]+)\.)?" + domain_regex + route_regex + query_string_regex + r"(?:#(?P.+)?)?", re.IGNORECASE) def generate_routes(domain_names: str|list[str], routes: dict[str, list[str] | None]) -> dict[str, re.Pattern[str]]: @@ -421,7 +421,7 @@ class InstagramLink(GenericWebsiteLink): "/stories/:username/:id": None, "/:username": None, }) - params = { + subdomains = { InstagramView.NORMAL: '', InstagramView.DIRECT_MEDIA: 'd.', InstagramView.GALLERY: 'g.',