Skip to content
16 changes: 10 additions & 6 deletions cogs/link_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]:
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"(?<!\w){re.escape(k)}(?!\w)", message.content) for k in guild.keywords
) != guild.keywords_use_allow_list:
return
if not TextChannel.find_get_enabled(message.channel, guild):
Expand Down
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 4 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/websites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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<subdomain>[^.]+)\.)?" + domain_regex + route_regex + query_string_regex + r"(?:#(?P<anchor>.+)?)?", re.IGNORECASE)
return re.compile(r"https?://(?:(?P<subdomain>[\w-]+)\.)?" + domain_regex + route_regex + query_string_regex + r"(?:#(?P<anchor>.+)?)?", re.IGNORECASE)


def generate_routes(domain_names: str|list[str], routes: dict[str, list[str] | None]) -> dict[str, re.Pattern[str]]:
Expand Down Expand Up @@ -421,7 +421,7 @@ class InstagramLink(GenericWebsiteLink):
"/stories/:username/:id": None,
"/:username": None,
})
params = {
subdomains = {
InstagramView.NORMAL: '',
InstagramView.DIRECT_MEDIA: 'd.',
InstagramView.GALLERY: 'g.',
Expand Down
Loading