fix some lyrics and changes node

This commit is contained in:
Rene 2023-11-22 11:48:05 +09:00
parent 62cd417583
commit b906f77cac
2 changed files with 101 additions and 37 deletions

View File

@ -57,9 +57,10 @@ module.exports = {
secure: true, secure: true,
}, },
{ {
host: "194.233.92.52", host: "lavalink-sg.serenetia.com",
port: 3556, port: 443,
password: "amanechan", password: "amanechan",
secure:true,
}, },
], ],
}, },

View File

@ -5,53 +5,92 @@ const prettyMs = require("pretty-ms");
const { splitBar } = require("string-progressbar"); const { splitBar } = require("string-progressbar");
const lyricsFinder = require("lyrics-finder"); const lyricsFinder = require("lyrics-finder");
const Genius = require("genius-lyrics-api"); const Genius = require("genius-lyrics-api");
const fs = require("fs").promises;
const path = require("path");
function split(text, sep, limit) {
if (typeof sep === 'string') sep = new RegExp(sep, 'g');
let chunks = [],
chunkLength = 0,
lastIndex = 0;
for (const { 0: sepMatch, index = 0 } of text.matchAll(sep)) {
const part = text.slice(lastIndex, index);
// yield chunks if reaching limit
if (chunkLength && chunkLength + sepMatch.length + part.length > limit) {
// remove last separator
chunks.pop();
chunks.push(chunks.join(''));
// reset
chunks = [];
chunkLength = 0;
}
// add part & separator to chunk
chunks.push(part, sepMatch);
chunkLength += part.length + sepMatch.length;
// update last index pointer
lastIndex = index + sepMatch.length;
}
// leftover yield
if (chunks.length) {
// remove last separator
chunks.pop();
chunks.push(chunks.join(''));
}
return chunks;
}
module.exports = class Lyrics extends Command { module.exports = class Lyrics extends Command {
constructor(client) { constructor(client) {
super(client, { super(client, {
name: "lyrics", name: "lyrics",
description: "Show lyrics of the currently playing track", description: "Show lyrics of the specified track or the currently playing track",
category: "MUSIC", category: "MUSIC",
botPermissions: ["SEND_MESSAGES"], botPermissions: ["SEND_MESSAGES", "ATTACH_FILES"],
command: {
enabled: true,
aliases: ["getlyrics"],
},
slashCommand: { slashCommand: {
enabled: true, enabled: true,
options: [
{
name: "title",
type: "STRING",
description: "The title of the track to search for lyrics. If not provided, uses the currently playing track.",
required: false,
},
],
}, },
}); });
} }
/**
* @param {Message} message
* @param {string[]} args
*/
async messageRun(message, args) {
const response = await getLyrics(message);
message.channel.send(response);
}
/** /**
* @param {CommandInteraction} interaction * @param {CommandInteraction} interaction
*/ */
async interactionRun(interaction) { async interactionRun(interaction) {
const response = await getLyrics(interaction); try {
interaction.followUp(response); const title = interaction.options.getString("title");
const response = await getLyrics(interaction, title);
await sendLyrics(interaction, response);
} catch (error) {
console.error(error);
interaction.followUp("An error occurred while fetching the lyrics.");
}
} }
}; };
async function getLyrics({ client, guildId }) { async function getLyrics({ client, guildId }, title) {
const player = client.musicManager.get(guildId); const player = client.musicManager.get(guildId);
if (!player || !player.queue.current) return "🚫 No music is being played!"; if (!player || (!title && !player.queue.current)) return "🚫 No music is being played!";
const track = player.queue.current; const track = title ? { title: title } : player.queue.current;
let lyrics = ""; let lyrics = "";
try { try {
lyrics = await lyricsFinder(track.title, track.author);
if (!lyrics) {
const geniusOptions = { const geniusOptions = {
apiKey: "kvWM3l1MV7z5UM2N2_x3hRneMhhSOTTnMK6NZHzxJxiQ2NzvRM8k3He_zkQyB_EP", apiKey: "kvWM3l1MV7z5UM2N2_x3hRneMhhSOTTnMK6NZHzxJxiQ2NzvRM8k3He_zkQyB_EP",
title: track.title, title: track.title,
@ -66,7 +105,6 @@ async function getLyrics({ client, guildId }) {
} else { } else {
lyrics = "No lyrics found."; lyrics = "No lyrics found.";
} }
}
} catch (error) { } catch (error) {
console.error(error); console.error(error);
lyrics = "An error occurred while fetching the lyrics."; lyrics = "An error occurred while fetching the lyrics.";
@ -74,3 +112,28 @@ async function getLyrics({ client, guildId }) {
return lyrics; return lyrics;
} }
async function sendLyrics(context, lyrics) {
const tempFilePath = path.join(__dirname, "temp_lyrics.txt");
try {
await fs.writeFile(tempFilePath, lyrics, "utf-8");
const fileSize = (await fs.stat(tempFilePath)).size;
if (fileSize > 2000) {
const fileContent = await fs.readFile(tempFilePath, "utf-8");
for (const chunk of split(fileContent, /(\r?\n){2}/g, 2000)) {
context.channel.send(chunk);
}
} else {
context.channel.send(lyrics);
}
} catch (error) {
console.error(error);
context.channel.send("An error occurred while sending the lyrics.");
} finally {
// Cleanup: Delete the temporary file
await fs.unlink(tempFilePath).catch((error) => console.error("Error deleting temporary file:", error));
}
}