-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
146 lines (127 loc) · 4.52 KB
/
utils.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
const path = require('path');
const fs = require('fs');
const ytdl = require("ytdl-core");
function cleanupSongsUtil(guildId) {
console.log(`Cleaning up songs for guild ID ${guildId}`);
const guildDirectory = path.join(__dirname, `temp_${guildId}`);
if (fs.existsSync(guildDirectory)) {
fs.readdir(guildDirectory, (err, files) => {
if (err) {
console.error(`Error reading directory for cleanup: ${guildDirectory}`, err);
return;
}
files.forEach(file => {
const filePath = path.join(guildDirectory, file);
if (fs.existsSync(filePath)) {
fs.unlink(filePath, err => {
if (err) {
console.error(`Error deleting file ${filePath}:`, err);
} else {
console.log(`Deleted song file: ${filePath}`);
}
});
}
});
// Remove the directory as well
fs.rm(guildDirectory, {recursive: true}, err => {
if (err) {
console.error(`Error deleting directory ${guildDirectory}:`, err);
} else {
console.log(`Deleted directory: ${guildDirectory}`);
}
});
});
} else {
console.log(`No directory found for guild ID ${guildId}, no cleanup necessary.`);
}
}
function createTempFileForGuildUtil(guildId) {
const guildDirectory = path.join(__dirname, `temp_${guildId}`);
if (!fs.existsSync(guildDirectory)) {
fs.mkdirSync(guildDirectory, {recursive: true});
}
// Create a unique file name
const fileName = `song_${Date.now()}.mp3`;
const filePath = path.join(guildDirectory, fileName);
return filePath;
}
const { exec } = require('youtube-dl-exec');
function downloadSongUtil(url, path) {
return new Promise((resolve, reject) => {
console.log("Downloading song:", url, "to path:", path);
exec(url, {
output: path,
extractAudio: true,
audioFormat: 'mp3',
audioQuality: 0,
noCheckCertificates: true,
preferFreeFormats: true,
youtubeSkipDashManifest: true,
referer: 'https://www.youtube.com/',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
}).then(() => {
console.log('Download completed successfully');
resolve();
}).catch((error) => {
console.error('Error downloading:', error);
reject(error);
});
});
}
function deleteSongFileUtil(filePath) {
setTimeout(() => {
fs.unlink(filePath, (err) => {
if (err) {
console.error("Error deleting song file:", err);
} else {
console.log(`Deleted song file: ${filePath}`);
}
});
}, 15000);
}
function invalidSongURL(songUrl) {
return (!songUrl || !ytdl.validateURL(songUrl));
}
function stringifyQueueWithIndex(queue) {
let queueString = "";
queue.getQueue().forEach((item, index) => {
queueString += `${index}: ${item.song.name} (Added by ${item.addedBy})\n`;
});
return queueString;
}
function splitMessage(message, maxLength) {
const messageParts = [];
let currentPart = "";
message.split("\n").forEach((line) => {
if (line.length > maxLength) {
// If the line itself exceeds the max length, split it into smaller parts
const lineParts = line.match(new RegExp(`.{1,${maxLength}}`, 'g'));
lineParts.forEach((part) => {
if (currentPart.length + part.length + 1 <= maxLength) {
currentPart += part + "\n";
} else {
messageParts.push(currentPart.trim());
currentPart = part + "\n";
}
});
} else if (currentPart.length + line.length + 1 <= maxLength) {
currentPart += line + "\n";
} else {
messageParts.push(currentPart.trim());
currentPart = line + "\n";
}
});
if (currentPart.length > 0) {
messageParts.push(currentPart.trim());
}
return messageParts;
}
module.exports = {
cleanupSongsUtil,
createTempFileForGuildUtil,
downloadSongUtil,
deleteSongFileUtil,
invalidSongURL,
splitMessage,
stringifyQueueWithIndex,
};