From edff41f3468996dad5e66d34ac8ab1f5a238751f Mon Sep 17 00:00:00 2001 From: salmonik Date: Thu, 15 Jan 2026 14:37:22 +0100 Subject: minor improvements --- cogs/ai.py | 11 +++++++---- cogs/compression.py | 5 +++-- cogs/music/play.py | 12 +++++++----- cogs/music/play_utils.py | 7 ++++++- cogs/web.py | 5 ++++- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/cogs/ai.py b/cogs/ai.py index a3c8201..f527c5b 100644 --- a/cogs/ai.py +++ b/cogs/ai.py @@ -6,7 +6,7 @@ import logging import requests import json from gtts import gTTS -from .music.play_utils import PlayUtils +from cogs.music.play_utils import PlayUtils import io logger = logging.getLogger(__name__) @@ -22,8 +22,8 @@ class AI(commands.Cog): async def senator( self, ctx: discord.ApplicationContext, - text=discord.Option(str, "message", required=True), - tts=discord.Option(bool, "face?", default=False) + text: str = discord.Option(str, "message", required=True), + tts: str = discord.Option(bool, "face?", default=False) ): await ctx.defer() @@ -38,7 +38,10 @@ class AI(commands.Cog): "messages": [ { "role": "user", - "content": "You are Senator Steaven Sperma Armstrong from Metal Gear Rising Vengeance. You are racist and you hate other minorities. Respond in plain text, no ** or "". If the message is in Polish, respond in Polish. Respond in 100 words. Respond accordingly to this message: " + text + "content": "You are Senator Steaven Sperma Armstrong from Metal Gear Rising Vengeance. \ + You are racist and you hate other minorities. Respond in plain text, no ** or "". \ + If the message is in Polish, respond in Polish. Respond in 100 words. \ + Respond accordingly to this message: " + text } ], diff --git a/cogs/compression.py b/cogs/compression.py index 1905d74..79f79a0 100644 --- a/cogs/compression.py +++ b/cogs/compression.py @@ -2,7 +2,7 @@ import mimetypes import os import subprocess from typing import List, Optional - +from os import PathLike import discord from discord.ext import commands import logging @@ -29,8 +29,9 @@ class Compression(commands.Cog): async def _compress_attachment(self, attachment: discord.Attachment) -> Optional[discord.File]: """Compress a single attachment and return the compressed file""" + file_path = PathLike[str] try: - file_path = os.path.join(self.temp_dir, attachment.filename) + file_path: PathLike[str] | str = os.path.join(self.temp_dir, attachment.filename) output_path = os.path.join(self.temp_dir, f"better_{attachment.filename}") # Save the original file diff --git a/cogs/music/play.py b/cogs/music/play.py index 2c5b641..d77d772 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py @@ -1,11 +1,12 @@ import os import discord import mutagen -from typing import List, Tuple, Optional, Callable, Awaitable, Union +from typing import List, Tuple, Callable, Awaitable +from os import PathLike -from . import play_utils +from cogs.music import play_utils from discord.ext import commands -from discord.commands import Option, OptionChoice +from discord.commands import OptionChoice import logging logger = logging.getLogger(__name__) @@ -16,7 +17,8 @@ class Play(commands.Cog): self.temp_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "temp") self.path_mapping = {} # Maps display names to full paths - async def _create_search_embed(self, candidates: List[str]) -> Tuple[discord.Embed, discord.ui.View]: + @staticmethod + async def _create_search_embed(candidates: List[str]) -> Tuple[discord.Embed, discord.ui.View]: """Create an embed and view for search results.""" embed = discord.Embed( title="Wyniki wyszukiwania", @@ -204,7 +206,7 @@ class Play(commands.Cog): @discord.slash_command(name="grajcuj_plik", description="grajcuj plik") async def play_file(self, ctx: discord.ApplicationContext, attachment: discord.Attachment = discord.Option(discord.Attachment, "co zagrać??", required=True)): await ctx.defer() - file_path = os.path.join(self.temp_dir, attachment.filename) + file_path: PathLike[str] | str = os.path.join(self.temp_dir, attachment.filename) await attachment.save(file_path) await play_utils.PlayUtils(self.bot).play(ctx, file_path) diff --git a/cogs/music/play_utils.py b/cogs/music/play_utils.py index 0614157..044bd1d 100644 --- a/cogs/music/play_utils.py +++ b/cogs/music/play_utils.py @@ -113,7 +113,9 @@ class Utils: # Boost score based on word ratio score = score * (0.7 + (0.3 * word_ratio)) - + + score = int(score) + # Ensure score is within bounds return min(100, max(0, score)) @@ -410,6 +412,9 @@ class PlayUtils: async def search_local_autocomplete(cls, query: str = None, max_results: int = 10): """Wrapper for autocomplete searches that returns more results and allows empty query.""" # For autocomplete, we want to show more options and allow empty queries + if not query: + return [] + return await cls.search_local(query, max_results=max_results, autocomplete=True) diff --git a/cogs/web.py b/cogs/web.py index 9431e84..224df82 100644 --- a/cogs/web.py +++ b/cogs/web.py @@ -24,7 +24,8 @@ class Web(commands.Cog): ): await ctx.defer(ephemeral=True) headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} - url = f"https://duckduckgo.com/html/?q={query}" + encoded_query = urllib.parse.quote_plus(query) + url = f"https://duckduckgo.com/html/?q={encoded_query}" response = requests.get(url, headers=headers) logger.info(f"Query: {query}") @@ -47,8 +48,10 @@ class Web(commands.Cog): title = yt.title except Exception: pass + embed.add_field(name=f"Result {i}", value=f"[{title}]({link.get('href')})", inline=False) button = discord.ui.Button(label=f"Send Result {i}") + async def button_callback(interaction: discord.Interaction, link=link): await interaction.response.send_message(link.get('href')) button.callback = button_callback -- cgit v1.2.3