|
| 1 | +import { Message } from "../../types/message"; |
| 2 | +import Genius from "genius-lyrics" |
| 3 | +import log from "../components/utils/log"; |
| 4 | +import { request } from "undici"; |
| 5 | + |
| 6 | +export const info = { |
| 7 | + command: "lyrics", |
| 8 | + description: "Get a lyrics from a song.", |
| 9 | + usage: "lyrics <song title>", |
| 10 | + example: "lyrics rchp sick love", |
| 11 | + role: "user", |
| 12 | + cooldown: 5000, |
| 13 | +}; |
| 14 | + |
| 15 | +export default async function (msg: Message) { |
| 16 | + const query = msg.body.replace(/^lyrics\b\s*/i, "").trim(); |
| 17 | + if (query.length === 0) { |
| 18 | + await msg.reply("Please provide a search query."); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const client = new Genius.Client(); |
| 23 | + const origGet = client.request.get.bind(client.request); |
| 24 | + |
| 25 | + client.request.get = async (url: string): Promise<string> => { |
| 26 | + const res = await request(url, { |
| 27 | + headers: { |
| 28 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", |
| 29 | + }, |
| 30 | + }); |
| 31 | + return await res.body.text(); |
| 32 | + }; |
| 33 | + const searches = await client.songs.search(query); |
| 34 | + |
| 35 | + if (!searches || searches.length == 0) { |
| 36 | + await msg.reply("Unable to find resources for the given query."); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const firstSong = searches[0]; |
| 41 | + |
| 42 | + let lyrics: string; |
| 43 | + try { |
| 44 | + lyrics = await firstSong.lyrics(); |
| 45 | + } catch (err: any) { |
| 46 | + await msg.reply("Could not fetch lyrics (possibly blocked by Genius)."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const text = ` |
| 51 | + \`${firstSong.fullTitle}\` |
| 52 | +
|
| 53 | + ${lyrics} |
| 54 | + `; |
| 55 | + await msg.reply(text); |
| 56 | +} |
0 commit comments