@@ -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}
0 commit comments