From 762212781f4adc953480dc2a889242f144d18aa3 Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 1/9] Never mention anyone from a fixed link A fixed link is built from the URL a member posted, username included. The author label already gets a space when the username starts with `everyone` or `here`, but the username can contain the mention itself, as in `https://x.com/@everyone/status/1` or `https://x.com/x@here/status/1`: the label of the fixed link then holds it as it is, and nothing told Discord not to read it as a mention. The bot now only allows the mention of the member it replies to, and the messages it sends through a webhook (reply as the original author) none at all. Co-Authored-By: Claude Fable 5.2 --- cogs/link_fix.py | 1 + main.py | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index d5bf6de..b867611 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -279,6 +279,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): diff --git a/main.py b/main.py index c5dab18..daecbe2 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,9 @@ intents = discore.Intents(guild_messages=True, message_content=True, guilds=True) -bot = discore.Bot(help_command=None, intents=intents) +# A fixed link is built from what members post: it must never ping anyone but the member it replies to +allowed_mentions = discore.AllowedMentions(everyone=False, users=False, roles=False) + +bot = discore.Bot(help_command=None, intents=intents, allowed_mentions=allowed_mentions) asyncio.run(bot.tree.set_translator(I18nTranslator())) bot.run() From aeef4a8988d1f84b3dfed7774b7112058302f72c Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 2/9] Ignore the messages sent through the bot's own webhook When the fixed links are sent as a replica of the original author, their author is a webhook and not the bot, so the bot handled its own messages whenever webhooks are not ignored. With a custom website whose fixed link is fixable again (its own domain, or two websites pointing at each other), each fixed link was fixed again, endlessly. Co-Authored-By: Claude Fable 5.2 --- cogs/link_fix.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index b867611..2edc175 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -342,6 +342,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 From 5a3dc1ec9e543214dbe6b61074cdeae41d027d23 Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 3/9] Only accept a real label as the subdomain of a website `[^.]+` accepts any character but a dot, `#` and `/` included: `https://16843009#.bilibili.com/video/BV1` was taken for a BiliBili link whose subdomain is `16843009#`, and answered with `https://16843009#.vxbilibili.com/video/BV1`, which is a link to the host `16843009` labelled as the fixer. Co-Authored-By: Claude Fable 5.2 --- src/websites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websites.py b/src/websites.py index 21a28b7..1cbf065 100644 --- a/src/websites.py +++ b/src/websites.py @@ -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]]: From 73f9bb3feb1470afdbe96fdf4b8b2d7690d2211e Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 4/9] Fix the author link of generic websites MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The link was rebuilt by cutting the original URL at the first occurrence of the username, which is wrong as soon as the username also appears earlier in the URL: for `https://www.instagram.com/instagram/p/…`, the author link was `https://instagram`. The URL is now cut where the route found the username, and written as the original link is (`https`, without `www.`), so the author link of every other URL stays what it was. Co-Authored-By: Claude Fable 5.2 --- src/websites.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/websites.py b/src/websites.py index 1cbf065..ad0d612 100644 --- a/src/websites.py +++ b/src/websites.py @@ -280,9 +280,9 @@ async def get_fixed_url(self) -> 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]: From e08f870f78b1e69a6eea7de20fbb812a6c7b336d Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 5/9] Apply the Instagram view setting The views were declared as `params` while `GenericWebsiteLink` reads `subdomains`, so the setting was saved and displayed but every Instagram link was fixed with the normal view. Co-Authored-By: Claude Fable 5.2 --- src/websites.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websites.py b/src/websites.py index ad0d612..0de5e22 100644 --- a/src/websites.py +++ b/src/websites.py @@ -421,7 +421,7 @@ class InstagramLink(GenericWebsiteLink): "/stories/:username/:id": None, "/:username": None, }) - params = { + subdomains = { InstagramView.NORMAL: '', InstagramView.DIRECT_MEDIA: 'd.', InstagramView.GALLERY: 'g.', From aaa41141712dbbce89ad6b068140edd89a7f3c06 Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 6/9] Match the keywords that start or end with a symbol `\b` only matches between a word character and another kind of character, so a keyword such as `!nofix` was not found at the start of a message or after a space: only when glued to the previous word. A keyword now matches when it stands on its own (not glued to a letter, a digit or an underscore), whatever it is made of. Nothing changes for the keywords made of word characters, such as the default `fxignore`. Co-Authored-By: Claude Fable 5.2 --- cogs/link_fix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index 2edc175..a937b4b 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -361,7 +361,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"(? Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 7/9] Wait for the embed of a fixed link through the raw event `message_edit` is only dispatched for the messages still in the message cache (the last 1000 received, all servers together). When a fixed link has left the cache before Discord adds its embed, the wait times out: the link is then deleted and reported as failed, although it was embedded. `raw_message_edit` is always dispatched. Co-Authored-By: Claude Fable 5.2 --- cogs/link_fix.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index a937b4b..8f76b24 100644 --- a/cogs/link_fix.py +++ b/cogs/link_fix.py @@ -296,10 +296,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 From fb65e5d78377df795b89e93c40d465420cc7e2bf Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 8/9] Check the permission to send messages in threads In a thread, `Send Messages in Threads` is the permission that applies, not `Send Messages`: the links posted in a thread were not fixed when the bot is only granted the former. Co-Authored-By: Claude Fable 5.2 --- cogs/link_fix.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cogs/link_fix.py b/cogs/link_fix.py index 8f76b24..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]]]: From 4a76026c9ade4679926faa9b2c62c9062cb0dce1 Mon Sep 17 00:00:00 2001 From: Seiren <212160429+seirenkr@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:15:46 +0900 Subject: [PATCH 9/9] Don't raise on a handled error when there is no log channel `safe_send_coro` reports the rate limits and the missing permissions it handles to `log.channel`. When that channel is not configured, or not found, the report itself raised (a `TypeError` for the missing setting, an `AttributeError` for the missing channel), turning a handled error into an unhandled one. Co-Authored-By: Claude Fable 5.2 --- src/utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils.py b/src/utils.py index 1c13c5e..8c74fa2 100644 --- a/src/utils.py +++ b/src/utils.py @@ -312,11 +312,12 @@ async def safe_send_coro( _logger.log(log_level, msg.replace('\n', ' '), stack_info=True) - if notify_channel: - bot: discore.Bot = discore.Bot.get() + # The log channel is optional + log_channel = discore.config.log.channel and discore.Bot.get().get_channel(discore.config.log.channel) + if notify_channel and log_channel: traceback_str = "".join(tb.format_tb(e.__traceback__)) + "".join(tb.format_exception_only(type(e), e)) msg_content = f"{msg}\n\n```\n{discore.sanitize(traceback_str, 1990 - len(msg), replace_newline=False)}\n```" - await bot.get_channel(discore.config.log.channel).send(msg_content) + await log_channel.send(msg_content) return False, None