-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmusic.js
159 lines (134 loc) · 4.63 KB
/
music.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require("fs-extra");
const ytdl = require("@distube/ytdl-core");
const yts = require("yt-search");
const axios = require('axios');
module.exports = {
config: {
name: "yt",
version: "1.0",
role: 0,
author: "Luis Lavigne",
cooldowns: 0,
shortDescription: "Download music or video from YouTube.",
longDescription: "",
category: "media",
dependencies: {
"fs-extra": "",
"ytdl-core": "",
"yt-search": "",
"axios": ""
}
},
onStart: async function ({ api, event }) {
},
onChat: async function ({ event, api }) {
const message = event.body.toLowerCase().trim();
if (message.includes("youtube.com") || message.includes("youtu.be")) {
const videoId = extractVideoId(message);
if (videoId) {
await downloadMedia(videoId, false, event, api);
return; // Return early after processing the YouTube link
}
}
if (message.startsWith("yta ")) {
const query = message.slice(4).trim();
await downloadMedia(query, true, event, api);
} else if (message.startsWith("ytv ")) {
const query = message.slice(4).trim();
await downloadMedia(query, false, event, api);
} else if (message.startsWith("ytl ")) {
const query = message.slice(4).trim();
await downloadMediaWithLyrics(query, event, api);
} else {
// Handle unrecognized commands or messages here
}
}
};
async function downloadMedia(query, isAudio, event, api) {
if (!query) {
return api.sendMessage(`Please specify a ${isAudio ? "music" : "video"} name.`, event.threadID);
}
try {
const loadingMessage = await api.sendMessage(`Searching ${isAudio ? "music" : "video"} for "${query}". Please wait...`, event.threadID);
const searchResults = await yts(query);
if (!searchResults.videos.length) {
api.sendMessage(`No ${isAudio ? "music" : "video"} found.`, event.threadID);
api.unsendMessage(loadingMessage.messageID);
return;
}
const media = searchResults.videos[0];
const mediaUrl = media.url;
const stream = ytdl(mediaUrl, { filter: isAudio ? "audioonly" : "videoandaudio" });
const fileName = `${Date.now()}.${isAudio ? "mp3" : "mp4"}`;
const filePath = `./cache/${fileName}`;
stream.pipe(fs.createWriteStream(filePath));
stream.on('end', () => {
api.sendMessage({
body: `Title: ${media.title}`,
attachment: fs.createReadStream(filePath)
}, event.threadID, () => {
fs.unlinkSync(filePath);
api.unsendMessage(loadingMessage.messageID);
});
});
} catch (error) {
console.error('[ERROR]', error);
api.sendMessage('An error occurred while processing the command.', event.threadID);
}
}
async function downloadMediaWithLyrics(query, event, api) {
if (!query) {
return api.sendMessage("Please specify a song title.", event.threadID);
}
try {
const loadingMessage = await api.sendMessage(`Searching for "${query}". Please wait...`, event.threadID);
const searchResults = await yts(query);
if (!searchResults.videos.length) {
api.sendMessage(`No videos found for "${query}".`, event.threadID);
api.unsendMessage(loadingMessage.messageID);
return;
}
const media = searchResults.videos[0];
const mediaUrl = media.url;
const stream = ytdl(mediaUrl, { filter: "audioonly" });
const fileName = `${Date.now()}.mp3`;
const filePath = `./cache/${fileName}`;
stream.pipe(fs.createWriteStream(filePath));
stream.on('end', async () => {
const lyrics = await getLyrics(query);
if (!lyrics) {
api.sendMessage(`No lyrics found for "${query}".`, event.threadID);
api.unsendMessage(loadingMessage.messageID);
return;
}
const replyMessage = {
body: `Title: ${media.title}\n\nLyrics for "${query}":\n${lyrics}`,
attachment: fs.createReadStream(filePath),
};
api.sendMessage(replyMessage, event.threadID, () => {
fs.unlinkSync(filePath);
api.unsendMessage(loadingMessage.messageID);
});
});
} catch (error) {
console.error('[ERROR]', error);
api.sendMessage('An error occurred while processing the command.', event.threadID);
}
}
async function getLyrics(song) {
try {
const response = await axios.get(`https://lyrist.vercel.app/api/${encodeURIComponent(song)}`);
if (response.data && response.data.lyrics) {
return response.data.lyrics;
} else {
return null;
}
} catch (error) {
console.error('[LYRICS ERROR]', error);
return null;
}
}
function extractVideoId(url) {
const match = url.match(/[?&]v=([^&]+)/);
return match && match[1];
}