From c0053ca98912901b5d387d58fbc57f9b7da91f38 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 18 Mar 2026 15:29:51 +0100 Subject: voice doesn't work due to DAVE, but we got copyparty search --- cogs/search.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 cogs/search.py (limited to 'cogs/search.py') diff --git a/cogs/search.py b/cogs/search.py new file mode 100644 index 0000000..2f8373b --- /dev/null +++ b/cogs/search.py @@ -0,0 +1,73 @@ +# This file is part of Senator Sperma +# Licensed under the GPL-3.0 License +# Copyright (C) 2026 Salmonik @ salmonik@tuta.io +# https://codeberg.org/salmonik/senator-sperma + +import urllib.parse + +import discord +from discord.ext import commands +import logging +from discord import AutocompleteContext +from cogs.utils import file_search as search_utils + +logger = logging.getLogger(__name__) + +class Search(commands.Cog): + def __init__(self, bot): + self.bot = bot + self._cached_results = {} + + async def file_autocomplete(self, ctx: AutocompleteContext): + """Autocomplete function for file search.""" + query = ctx.value.lower() if ctx.value else "" + results = await search_utils.UniversalSearcher.search_copyparty_autocomplete(query, max_results=25) + + user_id = ctx.interaction.user.id + self._cached_results[user_id] = {urllib.parse.unquote(url.split('/')[-1]): url for url in results} + + choices = [] + for url in results[:25]: + filename = urllib.parse.unquote(url.split('/')[-1]) + display_name = filename[:99] + "…" if len(filename) > 100 else filename + choices.append(discord.OptionChoice(name=display_name, value=filename)) + return choices + + @commands.slash_command( + name="search", + description="wyszukaj pliku" + ) + @discord.option( + name="filename", + description="Search for a file to send", + autocomplete=file_autocomplete + ) + async def search(self, ctx, filename: str): + """Send a file via URL from autocomplete selection.""" + if not filename: + await ctx.reply("Please select a file from the autocomplete options.") + return + + await ctx.defer() + logger.info(f"Search command called by {ctx.author} for file: {filename}") + + cached = self._cached_results.get(ctx.author.id) + if not cached: + await ctx.send_followup("Search results expired. Please search again.") + return + + matching_url = cached.get(filename) + if not matching_url: + await ctx.send_followup(f"File '{filename}' not found.") + return + + try: + await ctx.send_followup(matching_url) + logger.info(f"Successfully sent URL: {matching_url}") + del self._cached_results[ctx.author.id] + except Exception as e: + logger.error(f"Error sending URL {matching_url}: {str(e)}") + await ctx.send_followup(str(e)) + +def setup(bot): + bot.add_cog(Search(bot)) -- cgit v1.2.3