From 7d6c5aae513fd3d1955ceae4207c9718aa25ac18 Mon Sep 17 00:00:00 2001 From: WhatDidYouExpect <89535984+WhatDidYouExpect@users.noreply.github.com> Date: Tue, 22 Jul 2025 00:19:55 +0200 Subject: [PATCH] ts fuckass minigames file will be the death of me --- assets/cogs/lyrics.py | 123 +++++++++++++++++++++++++++++++++++++++++ assets/locales/it.json | 2 +- bot.py | 5 +- modules/globalvars.py | 2 + 4 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 assets/cogs/lyrics.py diff --git a/assets/cogs/lyrics.py b/assets/cogs/lyrics.py new file mode 100644 index 0000000..db999b4 --- /dev/null +++ b/assets/cogs/lyrics.py @@ -0,0 +1,123 @@ +import discord +from discord.ext import commands +from discord import app_commands +import aiohttp +import re + +class Lyrics(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @app_commands.command(name="lyrics", description="Get lyrics for a song") + @app_commands.describe( + artist="Name of the artist", + song="Title of the song", + language="Target language code (optional)" + ) + @app_commands.choices(language=[ + app_commands.Choice(name="Bulgarian", value="bg"), + app_commands.Choice(name="Czech", value="cs"), + app_commands.Choice(name="Danish", value="da"), + app_commands.Choice(name="German", value="de"), + app_commands.Choice(name="Greek", value="el"), + app_commands.Choice(name="English", value="en"), + app_commands.Choice(name="Spanish", value="es"), + app_commands.Choice(name="Estonian", value="et"), + app_commands.Choice(name="Finnish", value="fi"), + app_commands.Choice(name="French", value="fr"), + app_commands.Choice(name="Irish", value="ga"), + app_commands.Choice(name="Croatian", value="hr"), + app_commands.Choice(name="Hungarian", value="hu"), + app_commands.Choice(name="Italian", value="it"), + app_commands.Choice(name="Lithuanian", value="lt"), + app_commands.Choice(name="Latvian", value="lv"), + app_commands.Choice(name="Maltese", value="mt"), + app_commands.Choice(name="Dutch", value="nl"), + app_commands.Choice(name="Polish", value="pl"), + app_commands.Choice(name="Portuguese", value="pt"), + app_commands.Choice(name="Romanian", value="ro"), + app_commands.Choice(name="Slovak", value="sk"), + app_commands.Choice(name="Slovene", value="sl"), + app_commands.Choice(name="Swedish", value="sv"), + ]) + async def lyrics(self, interaction: discord.Interaction, artist: str = None, song: str = None, language: app_commands.Choice[str] = None): + await interaction.response.defer() + if not artist or not song: + member = interaction.guild.get_member(interaction.user.id) + if not member: + member = await interaction.guild.fetch_member(interaction.user.id) + act_artist, act_song = await self.get_artist_song_from_presence(member) + if act_artist and act_song: + artist = artist or act_artist + song = song or act_song + else: + await interaction.followup.send("No artist or song provided and couldn't find it from your current activity.") + return + + lyrics = await self.fetch_lyrics(artist, song) + if not lyrics: + await interaction.followup.send(f"Could not find lyrics for **{artist} - {song}**") + return + + if language: + translated = await self.translate_text(lyrics, language.value) + if translated: + lyrics = translated + + if len(lyrics) > 1900: + lyrics = lyrics[:1900] + "\n\n[...lyrics truncated...]" + + embed = discord.Embed( + title=f"{artist} - {song}", + description=lyrics, + color=discord.Color.blue() + ) + embed.set_footer(text=f"Requested by {interaction.user}", icon_url=interaction.user.display_avatar.url) + await interaction.followup.send(embed=embed) + + async def get_artist_song_from_presence(self, member: discord.Member): + for activity in member.activities: + if isinstance(activity, discord.Spotify): + return activity.artist, activity.title + return None, None + + + async def fetch_lyrics(self, artist, song): + artist_q = artist.replace(' ', '+').lower() + song_q = song.replace(' ', '+').lower() + + url = f"https://lrclib.net/api/get?artist_name={artist_q}&track_name={song_q}" + print(url) + + async with aiohttp.ClientSession() as session: + try: + async with session.get(url) as resp: + if resp.status != 200: + return None + data = await resp.json() + return data.get('plainLyrics') + except Exception: + return None + + async def translate_text(self, text: str, target_lang: str) -> str | None: + translate_url = "https://translate.googleapis.com/translate_a/single" + params = { + "client": "gtx", + "sl": "auto", + "tl": target_lang, + "dt": "t", + "q": text + } + async with aiohttp.ClientSession() as session: + try: + async with session.get(translate_url, params=params) as resp: + if resp.status != 200: + return None + result = await resp.json() + translated_chunks = [item[0] for item in result[0] if item[0]] + return ''.join(translated_chunks) + except Exception: + return None + +async def setup(bot): + await bot.add_cog(Lyrics(bot)) diff --git a/assets/locales/it.json b/assets/locales/it.json index 972088d..4f97806 100644 --- a/assets/locales/it.json +++ b/assets/locales/it.json @@ -1,5 +1,5 @@ { - "minigames_hangman_game": "Parola: {display_word}\nErrori: {wrong_guesses}/{max_wrong}", + "minigames_hangman_game": "Parola: {display_word()}\nErrori: {wrong_guesses}/{max_wrong}", "minigames_hangman_lost": "Hai perso! La parola era:", "minigames_hangman_won": "Hai vinto! La parola era:", "minigames_hangman_already_guessed": "Hai giĆ  indovinato", diff --git a/bot.py b/bot.py index 12d3d87..bbee94d 100644 --- a/bot.py +++ b/bot.py @@ -50,10 +50,9 @@ from modules.version import * from modules.sentenceprocessing import * from modules.unhandledexception import handle_exception from modules.image import gen_meme, gen_demotivator -# from modules.minigames import guessthenumber, hangman +from modules.minigames import guessthenumber, hangman sys.excepthook = handle_exception check_for_update() # Check for updates (from modules/version.py) - # Type aliases T = TypeVar('T') MessageContext = Union[commands.Context, discord.Interaction] @@ -114,7 +113,6 @@ async def on_ready() -> None: logger.info(f"{_('synced_commands')} {len(synced)} {(_('synced_commands2'))}") slash_commands_enabled = True logger.info(f"{(_('started')).format(name=NAME)}") - bot.loop.create_task(send_alive_ping_periodically()) except discord.errors.Forbidden as perm_error: logger.error(f"Permission error while syncing commands: {perm_error}") @@ -136,7 +134,6 @@ async def on_ready() -> None: }.get(status.lower(), discord.Status.online) await bot.change_presence(status=status, activity=discord.Activity(type=discord.ActivityType.listening, name=f"{song}")) launched = True - @bot.event async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None: from modules.unhandledexception import handle_exception diff --git a/modules/globalvars.py b/modules/globalvars.py index 5c20f1d..ac37ed4 100644 --- a/modules/globalvars.py +++ b/modules/globalvars.py @@ -66,5 +66,7 @@ else: # Set up Discord bot intents and create bot instance intents: discord.Intents = discord.Intents.default() intents.messages = True +intents.presences = True +intents.members = True intents.message_content = True bot: commands.Bot = commands.Bot(command_prefix=PREFIX, intents=intents, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False, replied_user=True)) \ No newline at end of file