Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -897,6 +907,63 @@ 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.Cog.listener()
async def on_message(self, message: discord.Message):
if message.author.bot:
return
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)

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))
Comment thread
Rajrooter marked this conversation as resolved.


# ---------------------------------------------------------------------------
# Events & startup
Expand Down
Loading