From 954a1df7a092d222741503920d30e701e0528e36 Mon Sep 17 00:00:00 2001 From: Raj Aryan Date: Tue, 3 Feb 2026 19:58:33 +0530 Subject: [PATCH] Add diagnostics for link handling --- main.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 3038262..8051849 100644 --- a/main.py +++ b/main.py @@ -195,9 +195,19 @@ async def link_preview(url: str) -> str: async def download_bytes(url: str) -> Optional[bytes]: try: + parsed = urlparse(url) + if parsed.scheme not in {"http", "https"}: + return None async with aiohttp.ClientSession() as session: async with session.get(url, timeout=aiohttp.ClientTimeout(total=30)) as resp: if resp.status == 200: + content_length = resp.headers.get("Content-Length") + if content_length: + try: + if int(content_length) > MAX_DOWNLOAD_BYTES: + return None + except ValueError: + pass content_type = resp.headers.get('Content-Type', '') if (not content_type) or any(ct in content_type for ct in ALLOWED_CONTENT_TYPES): data = await resp.read() @@ -389,7 +399,7 @@ def export_links_pdf_placeholder(): async def shorten_link(url: str) -> Optional[str]: try: safe_url = urllib.parse.quote(url, safe=":/?#[]@!$&'()*+,;=") - api = f"http://tinyurl.com/api-create.php?url={safe_url}" + api = f"https://tinyurl.com/api-create.php?url={safe_url}" async with aiohttp.ClientSession() as session: async with session.get(api, timeout=8) as resp: if resp.status == 200: @@ -897,6 +907,70 @@ async def handle_summarize_preview_ctx(self, interaction, message): # Placeholder: Implement summarization logic here await interaction.response.send_message("📝 Summarization not implemented yet.", ephemeral=True) + @commands.command(name="ping") + async def ping_command(self, ctx: commands.Context): + await safe_send(ctx, content="🏓 Pong! Bot is responding.") + + @commands.Cog.listener() + async def on_message(self, message: discord.Message): + if message.author.bot: + return + try: + await self.bot.process_commands(message) + if not message.content: + return + links = re.findall(URL_REGEX, message.content) + if not links: + return + filtered = [] + for link in links: + if not is_valid_url(link): + continue + if is_media_url(link): + continue + filtered.append(link) + if not filtered: + return + for link in filtered: + await self._handle_link(message, link) + except Exception as e: + logger.error(f"on_message failed: {e}", exc_info=True) + + async def _handle_link(self, message: discord.Message, link: str): + verdict, reason = get_link_verdict() + preview = await link_preview(link) + embed = make_verdict_embed(link, verdict, reason, preview) + entry = { + "url": link, + "timestamp": datetime.datetime.utcnow().isoformat(), + "author": str(message.author), + "user_id": message.author.id, + "guild_id": message.guild.id if message.guild else None, + "archived": False, + "expires_at": None, + } + pending_id = await asyncio.to_thread(storage.add_pending_link, entry) + view = LinkActionView(link, message.author.id, message, pending_id, self, ai_verdict=verdict) + prompt_msg = await safe_send(message.channel, embed=embed, view=view) + if prompt_msg: + view.message = prompt_msg + self.pending_links[prompt_msg.id] = pending_id + await asyncio.to_thread(storage.update_pending_with_bot_msg_id, pending_id, prompt_msg.id) + if message.guild: + self.guild_pending_counts[message.guild.id] = self.guild_pending_counts.get(message.guild.id, 0) + 1 + + @commands.command(name="pendinglinks") + async def pending_links_command(self, ctx: commands.Context): + links = await asyncio.to_thread(storage.get_pending_links_for_user, ctx.author.id) + if not links: + await safe_send(ctx, content="✅ No pending links.") + return + lines = [] + for idx, entry in enumerate(links, start=1): + url = entry.get("url", "unknown") + lines.append(f"{idx}. {url}") + await safe_send(ctx, content="🕒 **Your pending links:**\n" + "\n".join(lines)) + # --------------------------------------------------------------------------- # Events & startup @@ -926,6 +1000,8 @@ async def on_ready(): ╚═══════════════════════════════════════════════╝ """ logger.info(ready_banner) + if not bot.intents.message_content: + logger.warning("Message content intent is disabled; link detection will not work.") @bot.event