async def clearurl_hndlr(event): if event.message.via_bot is not None: # Don't handle inline messages return if event.message.entities: to_send = [] for entity in event.message.entities: url = None if isinstance(entity, MessageEntityTextUrl): url = entity.url elif isinstance(entity, MessageEntityUrl): url = event.message.text[entity.offset:entity.offset + entity.length] if url is not None: to_send.append(clear_url(url)) to_send_txt = "\n\n".join(i for i in to_send) await event.reply(f"🧹 Cleaned URLs: " f"\n{to_send_txt}", link_preview=False) else: chat = await event.get_chat() if isinstance( chat, User ): # don't disturb the group, only throw error at private chat await event.reply( "The message did not contain any links for me to clean!") raise events.StopPropagation
def intercept(info: interceptor.Request) -> None: if not info.request_url.scheme().startswith('http'): return new_url = QUrl(clear_url(info.request_url.url())) if new_host := redirects.get(new_url.host()): new_url.setHost(new_host)
async def handler(event): builder = event.builder input_urls = [] if '\n' in event.text: input_urls.extend(event.text.split('\n')) if ' ' in event.text: input_urls.extend(event.text.split(' ')) if input_urls: result = "\n".join(clear_url(link) for link in input_urls) else: result = "No URLs found" await event.answer( [builder.article('🧹 Cleaned URLs', text=result, link_preview=False)])
async def on_message(self, message): if message.author == client.user: permissions = message.channel.guild.me.permissions_in( message.channel) # Suppress embeds for bot messages to avoid visual clutter if permissions.manage_messages: await message.edit(suppress=True) # Add :wastebasket: emoji for easy deletion if necessary if permissions.add_reactions and permissions.read_message_history: await message.add_reaction('🗑') # Extract links and clean urls = re.findall('(?P<url>https?://[^\s]+)', message.content) cleaned = [] for url in urls: if clear_url(url) != url: cleaned.append(clear_url(url)) # Send message and add reactions if cleaned: text = 'It appears that you have sent one or more links with tracking parameters. Below are the same links with those fields removed:\n' + '\n'.join( cleaned) await message.reply(text, mention_author=False)
async def handler(event): builder = event.builder input_urls = [] if "\n" in event.text: input_urls.extend(event.text.split("\n")) # if no new line found, (i.e) just one link if len(event.text) and not input_urls: input_urls = [event.text] if input_urls: result = "\n".join(clear_url(link) for link in input_urls) else: result = "No URLs found" await event.answer( [builder.article("🧹 Cleaned URLs", text=result, link_preview=False)])
async def clearurl_hndlr(event): if event.message.via_bot is not None: # Don't handle inline messages by other bots return if event.message.entities: to_send = [] input_urls = set() for input_url in url_re.finditer(event.message.text): input_urls.add(input_url.group()) # Get url from formatted entities for entity in event.message.entities: if isinstance(entity, MessageEntityTextUrl): input_urls.add(entity.url) if input_urls: for url in input_urls: clean_url = clear_url(url) if url != clean_url: to_send.append(clean_url) if to_send: to_send_txt = "\n\n".join(i for i in to_send) try: await event.reply(f"🧹 Cleaned URLs: " f"\n{to_send_txt}", link_preview=False) except ChatWriteForbiddenError: # bot could send to the user (who added this bot to group), since we are not storing any details, # the only way to handle is to ignore pass except SlowModeWaitError: # FIXME schedule the msg to be sent after wait period pass else: chat = await event.get_chat() if isinstance( chat, User ): # don't disturb the group, only show at private chat await event.reply("No unclean links found!") else: chat = await event.get_chat() if isinstance( chat, User ): # don't disturb the group, only throw error at private chat await event.reply( "The message did not contain any links for me to clean!") raise events.StopPropagation