Skip to content

Commit d11ac67

Browse files
committed
feat: improve play and video command
1 parent 3f42f07 commit d11ac67

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

src/commands/play.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default async function play(msg: Message) {
2929
const yt = await Innertube.create({
3030
cache: new UniversalCache(false),
3131
generate_session_locally: true,
32-
player_id: "0004de42"
32+
player_id: "0004de42",
3333
});
3434
const search = await yt.music.search(query, { type: "song" });
3535

@@ -42,7 +42,7 @@ export default async function play(msg: Message) {
4242
// Only allow audios shorter than 10 minutes (600 seconds)
4343
if (audio.length && audio.length.seconds > 600) {
4444
await msg.reply(
45-
"Sorry, only videos shorter than 10 minutes can be downloaded.",
45+
"Sorry, it seems like im not able to get any search results.",
4646
);
4747
return;
4848
}
@@ -62,18 +62,24 @@ export default async function play(msg: Message) {
6262

6363
const tempDir = "./.temp";
6464
await fs.promises.mkdir(tempDir, { recursive: true });
65-
const tempPath = path.join(tempDir, `${audio.id}.mp4`);
65+
const tempPath = path.join(tempDir, `${audio.id}.mp3`);
6666
let writeStream = fs.createWriteStream(tempPath);
6767

6868
for await (const chunk of Utils.streamToIterable(stream)) {
6969
writeStream.write(chunk);
7070
}
7171

72+
await new Promise<void>((resolve, reject) => {
73+
writeStream.end();
74+
writeStream.on("finish", resolve);
75+
writeStream.on("error", reject);
76+
});
77+
7278
await execPromise(
7379
`ffmpeg -y -i "${tempPath}" -vn -ar 44100 -ac 2 -b:a 192k "${tempPath}.mp3"`,
7480
);
7581

76-
const audioBuffer = fs.readFileSync(tempPath + ".mp3");
82+
const audioBuffer = fs.readFileSync(`${tempPath}.mp3`);
7783
const media = new MessageMedia(
7884
"audio/mpeg",
7985
audioBuffer.toString("base64"),
@@ -84,5 +90,8 @@ export default async function play(msg: Message) {
8490
caption: audio.title,
8591
});
8692

87-
Promise.all([fs.promises.unlink(tempPath), fs.promises.unlink(tempPath)]);
93+
Promise.all([
94+
fs.promises.unlink(tempPath),
95+
fs.promises.unlink(`${tempPath}.mp3`),
96+
]);
8897
}

src/commands/video.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ export default async function (msg: Message) {
2727
}
2828

2929
const yt = await Innertube.create({
30-
cache: new UniversalCache(false),
31-
generate_session_locally: true,
32-
player_id: "0004de42",
30+
cache: new UniversalCache(false),
31+
generate_session_locally: true,
32+
player_id: "0004de42",
3333
});
3434
const search = await yt.search(query, { type: "video" });
3535

3636
const video = search.results[0];
3737
if (!video.video_id) {
38-
await msg.reply("Unable to find resources for the given query.");
38+
await msg.reply(
39+
"Sorry, it seems like im not able to get any search results.",
40+
);
3941
return;
4042
}
4143

@@ -69,11 +71,13 @@ export default async function (msg: Message) {
6971
writeStream.write(chunk);
7072
}
7173

72-
await execPromise(
73-
`ffmpeg -y -i "${tempPath}" -c:v copy -c:a copy "${tempPath}.mp4"`,
74-
);
74+
await new Promise<void>((resolve, reject) => {
75+
writeStream.end();
76+
writeStream.on("finish", resolve);
77+
writeStream.on("error", reject);
78+
});
7579

76-
const audioBuffer = fs.readFileSync(tempPath + ".mp4");
80+
const audioBuffer = fs.readFileSync(`${tempPath}`);
7781
const media = new MessageMedia(
7882
"audio/mpeg",
7983
audioBuffer.toString("base64"),
@@ -84,5 +88,5 @@ export default async function (msg: Message) {
8488
caption: `${video.title}`,
8589
});
8690

87-
Promise.all([fs.promises.unlink(tempPath), fs.promises.unlink(tempPath)]);
91+
fs.promises.unlink(tempPath);
8892
}

src/components/events/message.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ export default async function (msg: Message, type: string) {
257257
const text = `
258258
\`${errors[Math.floor(Math.random() * errors.length)]}\`\`
259259
260-
We encountered an error while processing ${key}.
261-
Provider returned an error ${statusMessages[status]}.
260+
We encountered an error while processing ${key}.
261+
Provider returned an error ${statusMessages[status]}.
262262
`;
263263
await msg.reply(text);
264264
return;
@@ -268,7 +268,7 @@ export default async function (msg: Message, type: string) {
268268
const text = `
269269
\`${errors[Math.floor(Math.random() * errors.length)]}\`
270270
271-
We encountered an error while processing ${key}.
271+
We encountered an error while processing ${key}.
272272
`;
273273
await msg.reply(text);
274274
}

0 commit comments

Comments
 (0)