@@ -2,13 +2,8 @@ import { MessageMedia } from "whatsapp-web.js";
22import { Message } from "../../types/message" ;
33import fs from "fs" ;
44import path from "path" ;
5- // fallback import for compatibility
6- // youtubei.js currently does not support ESM directly
7- const { Innertube, UniversalCache, Utils } = require ( "youtubei.js" ) ;
8- import { exec } from "child_process" ;
9- import util from "util" ;
10-
11- const execPromise = util . promisify ( exec ) ;
5+ import { Innertube , UniversalCache , Utils } from "youtubei.js" ;
6+ import log from "../components/utils/log" ;
127
138export const info = {
149 command : "video" ,
@@ -19,6 +14,20 @@ export const info = {
1914 cooldown : 5000 ,
2015} ;
2116
17+ async function search ( yt : Innertube , query : string ) {
18+ log . info ( "Video" , `Searching for ${ query } ` ) ;
19+ const results = await yt . search ( query , { type : "video" } ) ;
20+ const video : any = results . results ?. [ 0 ] ;
21+
22+ if ( video ?. video_id ) return video ;
23+
24+ const didYouMean : any = results . results ?. [ 0 ] ;
25+ if ( ! didYouMean ) return undefined ;
26+
27+ log . info ( "Video" , `Did you mean ${ didYouMean . corrected_query . text } ` ) ;
28+ return await search ( yt , didYouMean . corrected_query . text ) ;
29+ }
30+
2231export default async function ( msg : Message ) {
2332 const query = msg . body . replace ( / ^ v i d e o \b \s * / i, "" ) . trim ( ) ;
2433 if ( ! query ) {
@@ -31,20 +40,17 @@ export default async function (msg: Message) {
3140 generate_session_locally : true ,
3241 player_id : "0004de42" ,
3342 } ) ;
34- const search = await yt . search ( query , { type : "video" } ) ;
3543
36- const video = search . results [ 0 ] ;
37- if ( ! video . video_id ) {
38- await msg . reply (
39- "Sorry, it seems like im not able to get any search results." ,
40- ) ;
44+ const video : any = await search ( yt , query ) ;
45+ if ( ! video ) {
46+ await msg . reply ( `No youtube video found for "${ query } ".` ) ;
4147 return ;
4248 }
4349
44- // Only allow videos shorter than 10 minutes (600 seconds)
45- if ( video . length && video . length . seconds > 600 ) {
50+ // Only allow audios shorter than 20 minutes (1200 seconds)
51+ if ( video . duration && video . duration . seconds > 1200 ) {
4652 await msg . reply (
47- "Sorry, only videos shorter than 10 minutes can be downloaded ." ,
53+ "Opps, the video is quite long we can only process max of 20 minutes ." ,
4854 ) ;
4955 return ;
5056 }
@@ -58,7 +64,10 @@ export default async function (msg: Message) {
5864 } ) ;
5965
6066 if ( ! stream ) {
61- await msg . reply ( "Failed to download the video stream." ) ;
67+ await Promise . all ( [
68+ msg . reply ( "Failed to download the video." ) ,
69+ msg . react ( "" ) ,
70+ ] ) ;
6271 return ;
6372 }
6473
@@ -79,15 +88,9 @@ export default async function (msg: Message) {
7988 writeStream . on ( "error" , reject ) ;
8089 } ) ;
8190
82- const audioBuffer = fs . readFileSync ( `${ tempPath } ` ) ;
83- const media = new MessageMedia (
84- "audio/mpeg" ,
85- audioBuffer . toString ( "base64" ) ,
86- `${ video . title } .mp4` ,
87- ) ;
88-
91+ const media = MessageMedia . fromFilePath ( tempPath ) ;
8992 await msg . reply ( media , undefined , {
90- caption : ` ${ video . title } ` ,
93+ caption : video . title . text ,
9194 } ) ;
9295
9396 fs . promises . unlink ( tempPath ) ;
0 commit comments