Fitur YouTube

by BiyuOffc
66 Raw
case 'yt':
case 'youtube': {
  const axios = require('axios');
  if (!text) return m.reply(`Contoh penggunaan:
• yt search lofi
• yt channel lofi girl
• yt latest lofi girl
• yt stat https://youtube.com/watch?v=abc123`);

  const subcmd = text.split(' ')[0].toLowerCase();
  const query = text.replace(subcmd, '').trim();
  const apikey = 'AIzaSyBI6P58kEwxWywxh_UeCUpQC7_T5xwieTg';

  if (!['search', 'channel', 'latest', 'stat'].includes(subcmd))
    return m.reply('Subfitur tidak dikenal. Gunakan salah satu: search, channel, latest, stat');

  try {
    if (subcmd === 'search') {
      if (!query) return m.reply('Contoh: yt search lofi chill');
      const { data } = await axios.get(`https://www.googleapis.com/youtube/v3/search`, {
        params: {
          part: 'snippet',
          q: query,
          key: apikey,
          type: 'video',
          maxResults: 30 // lu atur aj bebas
        }
      });

      if (!data.items.length) return m.reply('Video tidak ditemukan.');
      let teks = '*Hasil Pencarian YouTube:*\n\n';
      data.items.forEach(v => {
        teks += `• *${v.snippet.title}*\n`;
        teks += `  Channel: ${v.snippet.channelTitle}\n`;
        teks += `  Link: https://youtube.com/watch?v=${v.id.videoId}\n\n`;
      });
      return m.reply(teks);
    }

    if (subcmd === 'channel') {
      if (!query) return m.reply('Contoh: yt channel lofi girl');
      const search = await axios.get(`https://www.googleapis.com/youtube/v3/search`, {
        params: {
          part: 'snippet',
          q: query,
          type: 'channel',
          key: apikey
        }
      });

      const ch = search.data.items[0];
      if (!ch) return m.reply('Channel tidak ditemukan.');
      const channelId = ch.id.channelId;
      const detail = await axios.get(`https://www.googleapis.com/youtube/v3/channels`, {
        params: {
          part: 'snippet,statistics,brandingSettings',
          id: channelId,
          key: apikey
        }
      });

      const info = detail.data.items[0];
      if (!info) return m.reply('Gagal mengambil detail channel.');
      const bannerUrl = info.brandingSettings?.image?.bannerExternalUrl;
      const cap = `*Channel Info:*
• *Nama:* ${info.snippet.title}
• *Subscriber:* ${info.statistics.subscriberCount}
• *Views:* ${info.statistics.viewCount}
• *Total Video:* ${info.statistics.videoCount}
• *Dibuat:* ${new Date(info.snippet.publishedAt).toLocaleDateString()}
• *Lokasi:* ${info.snippet.country || 'Tidak diketahui'}
• *Link:* https://youtube.com/channel/${channelId}

*Deskripsi:*\n${info.snippet.description?.slice(0, 500) || 'Tidak ada deskripsi.'}`;

      await conn.sendMessage(m.chat, {
        image: { url: info.snippet.thumbnails.high.url },
        caption: cap
      }, { quoted: m });

      if (bannerUrl) await conn.sendMessage(m.chat, {
        image: { url: bannerUrl },
        caption: 'Banner Channel'
      }, { quoted: m });
      return;
    }

    if (subcmd === 'latest') {
      if (!query) return m.reply('Contoh: yt latest lofi girl');
      const search = await axios.get(`https://www.googleapis.com/youtube/v3/search`, {
        params: {
          part: 'snippet',
          q: query,
          type: 'channel',
          key: apikey
        }
      });

      const ch = search.data.items[0];
      if (!ch) return m.reply('Channel tidak ditemukan.');
      const channelId = ch.id.channelId;
      const latest = await axios.get(`https://www.googleapis.com/youtube/v3/search`, {
        params: {
          key: apikey,
          channelId,
          part: 'snippet,id',
          order: 'date',
          maxResults: 1
        }
      });

      const vid = latest.data.items[0];
      if (!vid) return m.reply('Video terbaru tidak ditemukan.');
      const caption = `*Video Terbaru dari ${vid.snippet.channelTitle}:*
• *Judul:* ${vid.snippet.title}
• *Link:* https://youtube.com/watch?v=${vid.id.videoId}`;

      return conn.sendMessage(m.chat, {
        image: { url: vid.snippet.thumbnails.high.url },
        caption
      }, { quoted: m });
    }

    if (subcmd === 'stat') {
      if (!query.includes('youtube.com/watch')) return m.reply('Contoh: yt stat https://youtube.com/watch?v=abc123');
      const videoId = new URL(query).searchParams.get('v');
      const res = await axios.get(`https://www.googleapis.com/youtube/v3/videos`, {
        params: {
          part: 'snippet,statistics,status,contentDetails',
          id: videoId,
          key: apikey
        }
      });

      const video = res.data.items[0];
      if (!video) return m.reply('Video tidak ditemukan.');
      const cap = `*Statistik Video:*
• *Judul:* ${video.snippet.title}
• *Channel:* ${video.snippet.channelTitle}
• *Tayang:* ${new Date(video.snippet.publishedAt).toLocaleDateString()}
• *Views:* ${video.statistics.viewCount}
• *Likes:* ${video.statistics.likeCount}
• *Komentar:* ${video.statistics.commentCount}
• *Kategori ID:* ${video.snippet.categoryId}
• *Status:* ${video.status.privacyStatus}
• *Lisensi:* ${video.status.license}
• *Tags:* ${video.snippet.tags?.slice(0, 5).join(', ') || 'Tidak ada tag'}
• *Link:* https://youtube.com/watch?v=${videoId}

*Deskripsi:* 
${video.snippet.description?.slice(0, 1000) || 'Tidak ada deskripsi.'}`;
      return m.reply(cap);
    }
  } catch (err) {
    console.error(err);
    return m.reply('Gagal mengambil data dari YouTube. Coba lagi nanti.');
  }
}
break