forked from mahmud-aura/HINATA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.js
More file actions
123 lines (104 loc) · 5.68 KB
/
Copy pathaudio.js
File metadata and controls
123 lines (104 loc) · 5.68 KB
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
const axios = require("axios");
const fs = require('fs-extra');
const path = require('path');
const baseApiUrl = async () => {
const base = await axios.get(`https://raw.githubusercontent.com/mahmudx7/HINATA/main/baseApiUrl.json`);
return base.data.mahmud;
};
module.exports = {
config: {
name: "audio",
version: "1.7",
author: "MahMUD",
countDown: 5,
role: 0,
description: {
en: "Download any song directly from YouTube",
bn: "যেকোনো গান সরাসরি ডাউনলোড করুন",
vi: "Tải bất kỳ bài hát nào trực tiếp từ YouTube"
},
category: "music",
guide: {
en: ' {pn} <song name>\n Example: {pn} stay justin bieber',
bn: ' {pn} <গানের নাম>\n উদাহরণ: {pn} tui chinli na amay',
vi: ' {pn} <tên bài hát>\n Ví dụ: {pn} see you again'
}
},
langs: {
bn: {
error: "❌ An error occurred: contact MahMUD to help %1",
noResult: "⭕ | দুঃখিত বেবি, \"%1\" এর জন্য কিছু খুঁজে পাইনি।",
success: "✅ | এই নাও তোমার গান: %1"
},
en: {
error: "❌ An error occurred: contact MahMUD to help %1",
noResult: "⭕ | Sorry baby, I couldn't find anything for \"%1\"",
success: "✅ | Here is your song: %1"
},
vi: {
error: "❌ Đã xảy ra lỗi: liên hệ MahMUD để được hỗ trợ %1",
noResult: "⭕ | Xin lỗi bé, không tìm thấy kết quả cho \"%1\"",
success: "✅ | Đây là bài hát của bạn: %1"
}
},
onStart: async function ({ api, args, message, event, getLang }) {
const authorName = String.fromCharCode(77, 97, 104, 77, 85, 68);
if (this.config.author !== authorName) {
return api.sendMessage("You are not authorized to change the author name.", event.threadID, event.messageID);
}
const { threadID, messageID } = event;
const input = args.join(" ");
if (!input) return api.sendMessage("• Baby, please provide a song name.", threadID, messageID);
try {
const apiUrl = await baseApiUrl();
api.setMessageReaction("⏳", messageID, () => { }, true);
const res = await axios.get(`${apiUrl}/api/ytb/search?q=${encodeURIComponent(input)}`);
const results = res.data.results;
if (!results || results.length === 0) {
api.setMessageReaction("❌", messageID, () => { }, true);
return api.sendMessage(getLang("noResult", input), threadID, messageID);
}
const videoID = results[0].id;
const title = results[0].title;
api.setMessageReaction("⌛", messageID, () => { }, true);
await handleDownload(api, threadID, messageID, videoID, apiUrl, title, getLang);
} catch (e) {
api.setMessageReaction("❌", messageID, () => { }, true);
return api.sendMessage(getLang("error", e.message), threadID, messageID);
}
}
};
async function handleDownload(api, threadID, messageID, videoID, apiUrl, title, getLang) {
const cacheDir = path.join(__dirname, 'cache');
if (!fs.existsSync(cacheDir)) fs.mkdirSync(cacheDir, { recursive: true });
const filePath = path.join(cacheDir, `music_${Date.now()}.mp3`);
try {
const res = await axios.get(`${apiUrl}/api/ytb/get?id=${videoID}&type=audio`);
const { downloadLink } = res.data.data;
const response = await axios({
url: downloadLink,
method: 'GET',
responseType: 'stream',
headers: { 'User-Agent': 'Mozilla/5.0' }
});
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
writer.on('finish', () => {
api.sendMessage({
body: getLang("success", title),
attachment: fs.createReadStream(filePath)
}, threadID, (err) => {
if (err) api.sendMessage(getLang("error", "File too large!"), threadID, messageID);
api.setMessageReaction("🪽", messageID, () => { }, true);
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
}, messageID);
});
writer.on('error', (e) => {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
api.sendMessage(getLang("error", e.message), threadID, messageID);
});
} catch (e) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
api.sendMessage(getLang("error", "Download failed!"), threadID, messageID);
}
}