From 0f71cc546c6bbf0c3b3ff312ae6ee9ea57da6e0c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 10 Feb 2023 19:22:26 +0100 Subject: [PATCH] Add lyrics --- package-lock.json | 1 + package.json | 1 + src/commands/music/lyrics.ts | 86 ++++++++++++++++++++++++++++++++++++ src/locales/fr.json | 8 +++- src/modules/player.ts | 16 +++++++ src/utils/client.ts | 3 ++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/commands/music/lyrics.ts create mode 100644 src/modules/player.ts diff --git a/package-lock.json b/package-lock.json index b8ef190..798137c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.1", "license": "GPL-3.0-only", "dependencies": { + "@discord-player/extractor": "^4.0.0", "@discordjs/opus": "^0.8.0", "@discordjs/rest": "^1.1.0", "@types/sqlite3": "^3.1.8", diff --git a/package.json b/package.json index 89c65a2..433b692 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "author": "La confrérie du Kassoulait", "license": "GPL-3.0-only", "dependencies": { + "@discord-player/extractor": "^4.0.0", "@discordjs/opus": "^0.8.0", "@discordjs/rest": "^1.1.0", "@types/sqlite3": "^3.1.8", diff --git a/src/commands/music/lyrics.ts b/src/commands/music/lyrics.ts new file mode 100644 index 0000000..f7886c4 --- /dev/null +++ b/src/commands/music/lyrics.ts @@ -0,0 +1,86 @@ +import { SlashCommandBuilder } from "@discordjs/builders"; +import { ChatInputCommandInteraction, Client, EmbedBuilder } from "discord.js"; +import { getLocale, getLocalizations } from "../../utils/locales"; +import { getFilename } from "../../utils/misc"; + +export default { + scope: () => [], + + data: (client: Client) => { + const filename = getFilename(__filename); + const loc_default = client.locales.get(client.config.default_lang); + if (!loc_default) { + return; + } + + // TODO: Add subcommand "romanized" appending "romanized" to searchs + return ( + new SlashCommandBuilder() + .setName(filename.toLowerCase()) + .setDescription(loc_default.get(`c_${filename}_desc`) ?? "") + .setNameLocalizations(getLocalizations(client, `c_${filename}_name`, true)) + .setDescriptionLocalizations(getLocalizations(client, `c_${filename}_desc`)) + + // Command option + .addStringOption((option) => + option + .setName(loc_default.get(`c_${filename}_opt1_name`)?.toLowerCase() ?? "") + .setDescription(loc_default.get(`c_${filename}_opt1_desc`) ?? "") + .setNameLocalizations(getLocalizations(client, `c_${filename}_opt1_name`, true)) + .setDescriptionLocalizations(getLocalizations(client, `c_${filename}_opt1_desc`)) + ) + ); + }, + + interaction: async (interaction: ChatInputCommandInteraction, client: Client) => { + const loc_default = client.locales.get(client.config.default_lang); + const filename = getFilename(__filename); + + const loc = getLocale(client, interaction.locale); + + const request = interaction.options.getString( + loc_default?.get(`c_${filename}_opt1_name`) as string + ); + + let data; + if (request) { + data = await client.player.lyrics.search(request); + } + + const queue = client.player.queues.get(interaction.guildId ?? ""); + if (queue) { + data = await client.player.lyrics.search( + (queue.current?.author + " " ?? "") + queue.current?.title + ); + } + + if (data) { + if (data.lyrics.length > 4096 * 10) { + // TODO: Pretty embed + return await interaction.reply(loc.get("c_lyrics2") + " -> " + data.url); + } + // TODO: Take care of the desc char limit + const embeds = []; + + { + const embed = new EmbedBuilder(); + embed + .setTitle(data.title) + .setURL(data.url) + .setAuthor({ + name: data?.artist.name ?? "\u200b", + iconURL: data?.artist.image, + url: data?.artist.url, + }) + .setDescription(data.lyrics) + .setThumbnail(data.thumbnail); + + embeds.push(embed); + } + + return await interaction.reply({ embeds }); + } + + return await interaction.reply(loc.get("c_lyrics1")); + }, +}; diff --git a/src/locales/fr.json b/src/locales/fr.json index b41c429..04d7c71 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -108,5 +108,11 @@ "c_skip_opt1_desc": "ID de la chanson que vous voulez écouter", "c_skip1": "Passe la chanson", "c_skip2": "Le bot ne joue rien en ce moment.", - "c_skip3": "Passe à la chanson" + "c_skip3": "Passe à la chanson", + "c_lyrics_name": "paroles", + "c_lyrics_desc": "Affiche les paroles d'une chanson", + "c_lyrics_opt1_name": "chanson", + "c_lyrics_opt1_desc": "Chanson recherchée", + "c_lyrics1": "Le bot ne joue rien en ce moment et aucune chanson n'est renseignée.", + "c_lyrics2": "Paroles trop longues pour être affichées." } diff --git a/src/modules/player.ts b/src/modules/player.ts new file mode 100644 index 0000000..2440fb1 --- /dev/null +++ b/src/modules/player.ts @@ -0,0 +1,16 @@ +import { LyricsData } from "@discord-player/extractor"; +import { Client } from "genius-lyrics"; + +type LyricsClient = { + search: (query: string) => Promise; + client: Client; +}; + +export {}; + +declare module "discord-player" { + export interface Player { + /** Lyrics client */ + lyrics: LyricsClient; + } +} diff --git a/src/utils/client.ts b/src/utils/client.ts index a84308f..c480f31 100644 --- a/src/utils/client.ts +++ b/src/utils/client.ts @@ -4,6 +4,7 @@ import { loadLocales } from "./locales"; import "../modules/client"; import { Database } from "sqlite3"; import { Player } from "discord-player"; +import { lyricsExtractor } from "@discord-player/extractor"; /** Creation of the client and definition of its properties. */ export default async () => { @@ -43,6 +44,8 @@ export default async () => { }, }); + client.player.lyrics = lyricsExtractor(); + console.log("Translations progression :"); client.locales = await loadLocales(client.config.default_lang);