Advertisement
agunq

index.js

Apr 23rd, 2024 (edited)
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const {
  2.   default: Baileys,
  3.   makeInMemoryStore,
  4.   fetchLatestBaileysVersion,
  5.   useMultiFileAuthState,
  6.   DisconnectReason
  7. } = require('@whiskeysockets/baileys');
  8.  
  9. const { Boom } = require("@hapi/boom");
  10.  
  11. const ytdl = require('ytdl-core');
  12.  
  13. const prefix = "!";
  14.  
  15. async function start() {
  16.   try {
  17.     // Fetch the latest Baileys version and use multi-file auth state
  18.     let { version } = await fetchLatestBaileysVersion();
  19.     let { state, saveCreds } = await useMultiFileAuthState('./session');
  20.  
  21.     // Create a Baileys connection with specified configurations
  22.     const conn = Baileys({
  23.       auth: state,
  24.       printQRInTerminal: true
  25.      
  26.     });
  27.  
  28.  
  29.  
  30.     // Listen for 'creds.update' event and save credentials
  31.     conn.ev.on("creds.update", saveCreds);
  32.  
  33.     // Listen for 'connection.update' event and handle connection updates
  34.     conn.ev.on("connection.update", async (update) => {
  35.       const { lastDisconnect, connection } = update;
  36.    
  37.       // Log connection status
  38.       if (connection) {
  39.         console.log(connection === "connecting" ? "Connecting to the WhatsApp bot..." : `Connection: ${connection}`);
  40.       }
  41.    
  42.       // Handle different connection states
  43.       switch (connection) {
  44.         case "open":
  45.           console.log("Successfully connected to WhatsApp");
  46.           break;
  47.         case "close":
  48.           handleDisconnect(lastDisconnect.error);
  49.           break;
  50.       }
  51.     });
  52.    
  53.     // Function to handle disconnect reasons
  54.     function handleDisconnect(error) {
  55.       const reason = new Boom(error).output.statusCode;
  56.    
  57.       // Handle specific disconnect reasons
  58.       switch (reason) {
  59.         case DisconnectReason.badSession:
  60.           console.log("Bad Session File, Please Delete session and Scan Again");
  61.           conn.logout();
  62.           break;
  63.         case DisconnectReason.connectionClosed:
  64.           console.log("Connection closed, reconnecting...");
  65.           start();
  66.           break;
  67.         case DisconnectReason.connectionLost:
  68.           console.log("Connection Lost from Server, reconnecting...");
  69.           start();
  70.           break;
  71.         case DisconnectReason.connectionReplaced:
  72.           console.log("Connection Replaced, Another New Session Opened, Please Close Current Session First");
  73.           conn.logout();
  74.           break;
  75.         case DisconnectReason.loggedOut:
  76.           console.log("Device Logged Out, Please Delete session and Scan Again.");
  77.           conn.logout();
  78.           break;
  79.         case DisconnectReason.restartRequired:
  80.           console.log("Restart Required, Restarting...");
  81.           start();
  82.           break;
  83.         case DisconnectReason.timedOut:
  84.           console.log("Connection TimedOut, Reconnecting...");
  85.           start();
  86.           break;
  87.         default:
  88.           conn.end(`Unknown DisconnectReason: ${reason}|${error}`);
  89.       }
  90.     }
  91.  
  92.     conn.ev.on("group-participants.update", async (msg) => {
  93.       console.log(msg)
  94.     });
  95.    
  96.     // Listen for 'messages.upsert' event and call the handler function
  97.  
  98.    
  99.     conn.ev.on("messages.upsert", async ({messages, type}) => {
  100.         //console.log(messages[0])
  101.         const noWa = messages[0].key.remoteJid;
  102.         const text = (messages[0]?.message?.extendedTextMessage?.text ??messages[0]?.message?.imageMessage?.caption  ?? messages[0]?.message?.videoMessage?.caption ?? messages[0].message?.ephemeralMessage?.message?.extendedTextMessage?.text ?? messages[0].message?.conversation) || "";
  103.         console.log(noWa, text);
  104.         if (!messages[0].key.fromMe && text[0] === prefix){
  105.  
  106.                 let  [cmd, ...args] = text.slice(1).split(" ");
  107.             args = args.join(" ");
  108.            
  109.             if (cmd == "ping"){
  110.                 await conn.sendMessage(noWa, {text: "Pong"},{quoted: messages[0] });
  111.             }
  112.  
  113.             if (cmd == "ytdl"){
  114.                 const url = await ytdl.getInfo(args)
  115.                 console.log(url.formats[url.formats.length - 1])
  116.                 let url_link = url.formats[url.formats.length - 1].url
  117.                 await conn.sendMessage(noWa, { video: { url:url_link }, mimetype: 'video/mp4' });
  118.             }
  119.         }
  120.  
  121.     });
  122.   } catch (error) {
  123.     console.error(error);
  124.   }
  125. }
  126.  
  127. // Start the application by calling the 'start' function
  128. start();
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement