mirror of
https://github.com/MinazukiAmane/Tinasha-Bot.git
synced 2025-03-15 20:55:58 +08:00
fix some lyrics and changes node
This commit is contained in:
parent
62cd417583
commit
b906f77cac
@ -57,9 +57,10 @@ module.exports = {
|
||||
secure: true,
|
||||
},
|
||||
{
|
||||
host: "194.233.92.52",
|
||||
port: 3556,
|
||||
host: "lavalink-sg.serenetia.com",
|
||||
port: 443,
|
||||
password: "amanechan",
|
||||
secure:true,
|
||||
},
|
||||
],
|
||||
},
|
||||
|
@ -5,53 +5,92 @@ const prettyMs = require("pretty-ms");
|
||||
const { splitBar } = require("string-progressbar");
|
||||
const lyricsFinder = require("lyrics-finder");
|
||||
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 {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: "lyrics",
|
||||
description: "Show lyrics of the currently playing track",
|
||||
description: "Show lyrics of the specified track or the currently playing track",
|
||||
category: "MUSIC",
|
||||
botPermissions: ["SEND_MESSAGES"],
|
||||
command: {
|
||||
enabled: true,
|
||||
aliases: ["getlyrics"],
|
||||
},
|
||||
botPermissions: ["SEND_MESSAGES", "ATTACH_FILES"],
|
||||
slashCommand: {
|
||||
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
|
||||
*/
|
||||
async interactionRun(interaction) {
|
||||
const response = await getLyrics(interaction);
|
||||
interaction.followUp(response);
|
||||
try {
|
||||
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);
|
||||
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 = "";
|
||||
try {
|
||||
lyrics = await lyricsFinder(track.title, track.author);
|
||||
|
||||
if (!lyrics) {
|
||||
const geniusOptions = {
|
||||
apiKey: "kvWM3l1MV7z5UM2N2_x3hRneMhhSOTTnMK6NZHzxJxiQ2NzvRM8k3He_zkQyB_EP",
|
||||
title: track.title,
|
||||
@ -66,7 +105,6 @@ async function getLyrics({ client, guildId }) {
|
||||
} else {
|
||||
lyrics = "No lyrics found.";
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
lyrics = "An error occurred while fetching the lyrics.";
|
||||
@ -74,3 +112,28 @@ async function getLyrics({ client, guildId }) {
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user