dev #2
4 changed files with 127 additions and 5 deletions
123
assets/cogs/lyrics.py
Normal file
123
assets/cogs/lyrics.py
Normal file
|
@ -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))
|
|
@ -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_lost": "Hai perso! La parola era:",
|
||||||
"minigames_hangman_won": "Hai vinto! La parola era:",
|
"minigames_hangman_won": "Hai vinto! La parola era:",
|
||||||
"minigames_hangman_already_guessed": "Hai già indovinato",
|
"minigames_hangman_already_guessed": "Hai già indovinato",
|
||||||
|
|
5
bot.py
5
bot.py
|
@ -50,10 +50,9 @@ from modules.version import *
|
||||||
from modules.sentenceprocessing import *
|
from modules.sentenceprocessing import *
|
||||||
from modules.unhandledexception import handle_exception
|
from modules.unhandledexception import handle_exception
|
||||||
from modules.image import gen_meme, gen_demotivator
|
from modules.image import gen_meme, gen_demotivator
|
||||||
# from modules.minigames import guessthenumber, hangman
|
from modules.minigames import guessthenumber, hangman
|
||||||
sys.excepthook = handle_exception
|
sys.excepthook = handle_exception
|
||||||
check_for_update() # Check for updates (from modules/version.py)
|
check_for_update() # Check for updates (from modules/version.py)
|
||||||
|
|
||||||
# Type aliases
|
# Type aliases
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
MessageContext = Union[commands.Context, discord.Interaction]
|
MessageContext = Union[commands.Context, discord.Interaction]
|
||||||
|
@ -114,7 +113,6 @@ async def on_ready() -> None:
|
||||||
logger.info(f"{_('synced_commands')} {len(synced)} {(_('synced_commands2'))}")
|
logger.info(f"{_('synced_commands')} {len(synced)} {(_('synced_commands2'))}")
|
||||||
slash_commands_enabled = True
|
slash_commands_enabled = True
|
||||||
logger.info(f"{(_('started')).format(name=NAME)}")
|
logger.info(f"{(_('started')).format(name=NAME)}")
|
||||||
|
|
||||||
bot.loop.create_task(send_alive_ping_periodically())
|
bot.loop.create_task(send_alive_ping_periodically())
|
||||||
except discord.errors.Forbidden as perm_error:
|
except discord.errors.Forbidden as perm_error:
|
||||||
logger.error(f"Permission error while syncing commands: {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)
|
}.get(status.lower(), discord.Status.online)
|
||||||
await bot.change_presence(status=status, activity=discord.Activity(type=discord.ActivityType.listening, name=f"{song}"))
|
await bot.change_presence(status=status, activity=discord.Activity(type=discord.ActivityType.listening, name=f"{song}"))
|
||||||
launched = True
|
launched = True
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None:
|
async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None:
|
||||||
from modules.unhandledexception import handle_exception
|
from modules.unhandledexception import handle_exception
|
||||||
|
|
|
@ -66,5 +66,7 @@ else:
|
||||||
# Set up Discord bot intents and create bot instance
|
# Set up Discord bot intents and create bot instance
|
||||||
intents: discord.Intents = discord.Intents.default()
|
intents: discord.Intents = discord.Intents.default()
|
||||||
intents.messages = True
|
intents.messages = True
|
||||||
|
intents.presences = True
|
||||||
|
intents.members = True
|
||||||
intents.message_content = 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))
|
bot: commands.Bot = commands.Bot(command_prefix=PREFIX, intents=intents, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False, replied_user=True))
|
Loading…
Add table
Add a link
Reference in a new issue