11import { MessageMedia } from "whatsapp-web.js" ;
22import { Message } from "../../types/message" ;
33import { getFbVideoInfo } from "fb-downloader-scrapper" ;
4+ import crypto from "crypto" ;
45import log from "../components/utils/log" ;
56import axios from "../components/axios" ;
67import fs from "fs" ;
@@ -14,6 +15,15 @@ export const info = {
1415 cooldown : 5000 ,
1516} ;
1617
18+ const fileExists = async ( filePath : string ) => {
19+ try {
20+ await fs . promises . access ( filePath , fs . constants . F_OK ) ;
21+ return true ;
22+ } catch {
23+ return false ;
24+ }
25+ } ;
26+
1727export default async function ( msg : Message ) {
1828 const query = msg . body . replace ( / ^ f b d l \b \s * / i, "" ) . trim ( ) ;
1929 if ( query . length === 0 ) {
@@ -28,27 +38,41 @@ export default async function (msg: Message) {
2838 return ;
2939 }
3040
31- const result = await getFbVideoInfo ( query ) ;
32- if ( ! result . url )
33- return await msg . reply ( "No video found at the provided URL." ) ;
41+ await msg . react ( "🔍" ) ;
3442
35- const response = await axios . get ( result . hd || result . sd , {
36- responseType : "arraybuffer" ,
37- } ) ;
43+ const result = await getFbVideoInfo ( query ) ;
44+ if ( ! result . url ) {
45+ await Promise . all ( [
46+ msg . reply ( "No video found at the provided URL." ) ,
47+ msg . react ( "" ) ,
48+ ] ) ;
49+ return ;
50+ }
3851
3952 const tempDir = "./.temp" ;
4053 fs . mkdirSync ( tempDir , { recursive : true } ) ;
54+ const tempPath = `${ tempDir } /${ crypto . randomUUID ( ) } .mp4` ;
4155
42- const tempPath = `${ tempDir } /fbdl_${ Date . now ( ) } .mp4` ;
43- fs . writeFileSync ( tempPath , response . data ) ;
56+ if ( await fileExists ( tempPath ) ) {
57+ const media = MessageMedia . fromFilePath ( tempPath ) ;
58+ await Promise . all ( [
59+ msg . reply ( media , undefined , {
60+ caption : result . title ,
61+ } ) ,
62+ msg . react ( "" ) ,
63+ ] ) ;
64+ return ;
65+ }
4466
45- const audioBuffer = fs . readFileSync ( tempPath ) ;
46- const media = new MessageMedia (
47- "audio/mpeg" ,
48- audioBuffer . toString ( "base64" ) ,
49- `${ result . title } .mp4` ,
50- ) ;
67+ await msg . react ( "⬇️" ) ;
5168
52- await msg . reply ( media , msg . from ) ;
53- await fs . promises . unlink ( tempPath ) ;
69+ const response = await axios . get ( result . hd || result . sd , {
70+ responseType : "arraybuffer" ,
71+ } ) ;
72+ fs . writeFileSync ( tempPath , response . data ) ;
73+
74+ const media = MessageMedia . fromFilePath ( tempPath ) ;
75+ await msg . reply ( media , undefined , {
76+ caption : result . title ,
77+ } ) ;
5478}
0 commit comments