Add lyrics

This commit is contained in:
Mylloon 2023-02-10 19:22:26 +01:00
parent 6f89ed1856
commit 0f71cc546c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 114 additions and 1 deletions

1
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "0.0.1", "version": "0.0.1",
"license": "GPL-3.0-only", "license": "GPL-3.0-only",
"dependencies": { "dependencies": {
"@discord-player/extractor": "^4.0.0",
"@discordjs/opus": "^0.8.0", "@discordjs/opus": "^0.8.0",
"@discordjs/rest": "^1.1.0", "@discordjs/rest": "^1.1.0",
"@types/sqlite3": "^3.1.8", "@types/sqlite3": "^3.1.8",

View file

@ -16,6 +16,7 @@
"author": "La confrérie du Kassoulait", "author": "La confrérie du Kassoulait",
"license": "GPL-3.0-only", "license": "GPL-3.0-only",
"dependencies": { "dependencies": {
"@discord-player/extractor": "^4.0.0",
"@discordjs/opus": "^0.8.0", "@discordjs/opus": "^0.8.0",
"@discordjs/rest": "^1.1.0", "@discordjs/rest": "^1.1.0",
"@types/sqlite3": "^3.1.8", "@types/sqlite3": "^3.1.8",

View file

@ -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"));
},
};

View file

@ -108,5 +108,11 @@
"c_skip_opt1_desc": "ID de la chanson que vous voulez écouter", "c_skip_opt1_desc": "ID de la chanson que vous voulez écouter",
"c_skip1": "Passe la chanson", "c_skip1": "Passe la chanson",
"c_skip2": "Le bot ne joue rien en ce moment.", "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."
} }

16
src/modules/player.ts Normal file
View file

@ -0,0 +1,16 @@
import { LyricsData } from "@discord-player/extractor";
import { Client } from "genius-lyrics";
type LyricsClient = {
search: (query: string) => Promise<LyricsData | null>;
client: Client;
};
export {};
declare module "discord-player" {
export interface Player {
/** Lyrics client */
lyrics: LyricsClient;
}
}

View file

@ -4,6 +4,7 @@ import { loadLocales } from "./locales";
import "../modules/client"; import "../modules/client";
import { Database } from "sqlite3"; import { Database } from "sqlite3";
import { Player } from "discord-player"; import { Player } from "discord-player";
import { lyricsExtractor } from "@discord-player/extractor";
/** Creation of the client and definition of its properties. */ /** Creation of the client and definition of its properties. */
export default async () => { export default async () => {
@ -43,6 +44,8 @@ export default async () => {
}, },
}); });
client.player.lyrics = lyricsExtractor();
console.log("Translations progression :"); console.log("Translations progression :");
client.locales = await loadLocales(client.config.default_lang); client.locales = await loadLocales(client.config.default_lang);