diff options
| author | salmonik <raisersoft@proton.me> | 2026-03-18 15:29:51 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-03-18 15:29:51 +0100 |
| commit | c0053ca98912901b5d387d58fbc57f9b7da91f38 (patch) | |
| tree | 14fe7a7beae1ca2745333dcd7032a6ced28de0f7 /cogs/search.py | |
| parent | fix tts (diff) | |
| download | senator-sperma-c0053ca98912901b5d387d58fbc57f9b7da91f38.tar.gz senator-sperma-c0053ca98912901b5d387d58fbc57f9b7da91f38.zip | |
voice doesn't work due to DAVE, but we got copyparty search
Diffstat (limited to 'cogs/search.py')
| -rw-r--r-- | cogs/search.py | 73 |
1 files changed, 73 insertions, 0 deletions
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 @@ | |||
| 1 | # This file is part of Senator Sperma | ||
| 2 | # Licensed under the GPL-3.0 License | ||
| 3 | # Copyright (C) 2026 Salmonik @ salmonik@tuta.io | ||
| 4 | # https://codeberg.org/salmonik/senator-sperma | ||
| 5 | |||
| 6 | import urllib.parse | ||
| 7 | |||
| 8 | import discord | ||
| 9 | from discord.ext import commands | ||
| 10 | import logging | ||
| 11 | from discord import AutocompleteContext | ||
| 12 | from cogs.utils import file_search as search_utils | ||
| 13 | |||
| 14 | logger = logging.getLogger(__name__) | ||
| 15 | |||
| 16 | class Search(commands.Cog): | ||
| 17 | def __init__(self, bot): | ||
| 18 | self.bot = bot | ||
| 19 | self._cached_results = {} | ||
| 20 | |||
| 21 | async def file_autocomplete(self, ctx: AutocompleteContext): | ||
| 22 | """Autocomplete function for file search.""" | ||
| 23 | query = ctx.value.lower() if ctx.value else "" | ||
| 24 | results = await search_utils.UniversalSearcher.search_copyparty_autocomplete(query, max_results=25) | ||
| 25 | |||
| 26 | user_id = ctx.interaction.user.id | ||
| 27 | self._cached_results[user_id] = {urllib.parse.unquote(url.split('/')[-1]): url for url in results} | ||
| 28 | |||
| 29 | choices = [] | ||
| 30 | for url in results[:25]: | ||
| 31 | filename = urllib.parse.unquote(url.split('/')[-1]) | ||
| 32 | display_name = filename[:99] + "…" if len(filename) > 100 else filename | ||
| 33 | choices.append(discord.OptionChoice(name=display_name, value=filename)) | ||
| 34 | return choices | ||
| 35 | |||
| 36 | @commands.slash_command( | ||
| 37 | name="search", | ||
| 38 | description="wyszukaj pliku" | ||
| 39 | ) | ||
| 40 | @discord.option( | ||
| 41 | name="filename", | ||
| 42 | description="Search for a file to send", | ||
| 43 | autocomplete=file_autocomplete | ||
| 44 | ) | ||
| 45 | async def search(self, ctx, filename: str): | ||
| 46 | """Send a file via URL from autocomplete selection.""" | ||
| 47 | if not filename: | ||
| 48 | await ctx.reply("Please select a file from the autocomplete options.") | ||
| 49 | return | ||
| 50 | |||
| 51 | await ctx.defer() | ||
| 52 | logger.info(f"Search command called by {ctx.author} for file: {filename}") | ||
| 53 | |||
| 54 | cached = self._cached_results.get(ctx.author.id) | ||
| 55 | if not cached: | ||
| 56 | await ctx.send_followup("Search results expired. Please search again.") | ||
| 57 | return | ||
| 58 | |||
| 59 | matching_url = cached.get(filename) | ||
| 60 | if not matching_url: | ||
| 61 | await ctx.send_followup(f"File '{filename}' not found.") | ||
| 62 | return | ||
| 63 | |||
| 64 | try: | ||
| 65 | await ctx.send_followup(matching_url) | ||
| 66 | logger.info(f"Successfully sent URL: {matching_url}") | ||
| 67 | del self._cached_results[ctx.author.id] | ||
| 68 | except Exception as e: | ||
| 69 | logger.error(f"Error sending URL {matching_url}: {str(e)}") | ||
| 70 | await ctx.send_followup(str(e)) | ||
| 71 | |||
| 72 | def setup(bot): | ||
| 73 | bot.add_cog(Search(bot)) | ||
