File size: 27,916 Bytes
3419875
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
{
  "id": "0be92e13-c48d-4262-b56a-55081e567cca",
  "title": "sing1.js",
  "content": "const fs = require('fs');\nconst ytdl = require('ytdl-core-enhanced');\nconst { resolve } = require('path');\nconst { createCanvas, loadImage, registerFont } = require('canvas');\nconst axios = require('axios');\nconst path = require('path');\n\nfunction extractVideoId(url) {\n  const patterns = [\n    /(?:youtube\\.com\\/watch\\?v=|youtu\\.be\\/|youtube\\.com\\/embed\\/|youtube\\.com\\/v\\/)([^&\\n?#]+)/,\n    /^([a-zA-Z0-9_-]{11})$/ \n  ];\n  \n  for (const pattern of patterns) {\n    const match = url.match(pattern);\n    if (match) return match[1];\n  }\n  return null;\n}\n\nfunction isYouTubeUrlOrId(input) {\n  return extractVideoId(input) !== null;\n}\n\nfunction durationToSeconds(duration) {\n  if (!duration || duration === \"N/A\") return 0;\n  \n  const parts = duration.split(':').reverse();\n  let seconds = 0;\n  \n  for (let i = 0; i < parts.length; i++) {\n    seconds += parseInt(parts[i]) * Math.pow(60, i);\n  }\n  \n  return seconds;\n}\nfunction filterVideosByDuration(videos, maxDurationMinutes = 23) {\n  const maxSeconds = maxDurationMinutes * 60;\n  \n  return videos.filter(video => {\n    let duration = \"N/A\";\n    \n    if (video.length && video.length.simpleText) {\n      duration = video.length.simpleText;\n    } else if (video.length) {\n      duration = video.length.toString();\n    } else if (video.lengthText) {\n      duration = video.lengthText;\n    } else if (video.lengthSeconds) {\n      const minutes = Math.floor(video.lengthSeconds / 60);\n      const seconds = video.lengthSeconds % 60;\n      duration = `${minutes}:${seconds < 10 ? '0' + seconds : seconds}`;\n    }\n    \n    const durationInSeconds = durationToSeconds(duration);\n    return durationInSeconds > 0 && durationInSeconds <= maxSeconds;\n  });\n}\n\nasync function getCachedBackground() {\n  const backgrounds = [\n    \"http://vanthuankk.io.vn/1752417707835_x4lztg.jpg\",\n    \"http://vanthuankk.io.vn/1752417779463_scpurq.jpg\", \n    \"http://vanthuankk.io.vn/1752417802639_vovq0e.jpg\",\n    \"http://vanthuankk.io.vn/1752417842408_1i3ups.jpg\",\n    \"http://vanthuankk.io.vn/1752417862731_8nxtc6.jpg\"\n  ];\n  \n  const randomBg = backgrounds[Math.floor(Math.random() * backgrounds.length)];\n  const bgFileName = randomBg.split('/').pop();\n  const bgCachePath = `${__dirname}/cache/backgrounds`;\n  \n  if (!fs.existsSync(bgCachePath)) {\n    fs.mkdirSync(bgCachePath, { recursive: true });\n  }\n  \n  const cachedBgPath = path.join(bgCachePath, bgFileName);\n  \n  if (fs.existsSync(cachedBgPath)) {\n    return cachedBgPath;\n  }\n  \n  try {\n    const response = await axios.get(randomBg, { \n      responseType: 'arraybuffer',\n      timeout: 15000 \n    });\n    fs.writeFileSync(cachedBgPath, Buffer.from(response.data));\n    return cachedBgPath;\n  } catch (error) {\n    console.error(\"Error downloading background:\", error);\n    const existingBgs = fs.readdirSync(bgCachePath);\n    if (existingBgs.length > 0) {\n      return path.join(bgCachePath, existingBgs[0]);\n    }\n    throw error;\n  }\n}\n\nfunction formatNumber(num) {\n  if (num >= 1000000000) {\n    return (num / 1000000000).toFixed(1) + 'B';\n  }\n  if (num >= 1000000) {\n    return (num / 1000000).toFixed(1) + 'M';\n  }\n  if (num >= 1000) {\n    return (num / 1000).toFixed(1) + 'K';\n  }\n  return num.toString();\n}\n\nfunction truncateText(text, maxLength) {\n  if (text.length <= maxLength) return text;\n  return text.substring(0, maxLength - 3) + '...';\n}\n\nasync function downloadThumbnail(videoId, savePath) {\n  try {\n    const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/maxresdefault.jpg`;\n    const response = await axios({\n      method: 'GET',\n      url: thumbnailUrl,\n      responseType: 'arraybuffer'\n    });\n    \n    fs.writeFileSync(savePath, response.data);\n    return true;\n  } catch (error) {\n    console.log('Error downloading thumbnail:', error);\n    return false;\n  }\n}\n\nasync function createMusicPlayerCanvas(data, userName, videoId) {\n  const width = 800;\n  const height = 400;\n  const canvas = createCanvas(width, height);\n  const ctx = canvas.getContext('2d');\n\n  try {\n    const backgroundPath = await getCachedBackground();\n    const background = await loadImage(backgroundPath);\n    \n    ctx.drawImage(background, 0, 0, width, height);\n    \n    ctx.fillStyle = 'rgba(0, 0, 0, 0.4)';\n    ctx.fillRect(0, 0, width, height);\n  } catch (error) {\n    console.log('Error loading background:', error);\n    const gradient = ctx.createLinearGradient(0, 0, width, height);\n    gradient.addColorStop(0, '#667eea');\n    gradient.addColorStop(1, '#764ba2');\n    ctx.fillStyle = gradient;\n    ctx.fillRect(0, 0, width, height);\n  }\n\n  const containerX = 30;\n  const containerY = 30;\n  const containerWidth = width - 60;\n  const containerHeight = height - 60;\n  const containerRadius = 25;\n  \n  ctx.fillStyle = 'rgba(255, 255, 255, 0.15)';\n  ctx.beginPath();\n  ctx.moveTo(containerX + containerRadius, containerY);\n  ctx.lineTo(containerX + containerWidth - containerRadius, containerY);\n  ctx.quadraticCurveTo(containerX + containerWidth, containerY, containerX + containerWidth, containerY + containerRadius);\n  ctx.lineTo(containerX + containerWidth, containerY + containerHeight - containerRadius);\n  ctx.quadraticCurveTo(containerX + containerWidth, containerY + containerHeight, containerX + containerWidth - containerRadius, containerY + containerHeight);\n  ctx.lineTo(containerX + containerRadius, containerY + containerHeight);\n  ctx.quadraticCurveTo(containerX, containerY + containerHeight, containerX, containerY + containerHeight - containerRadius);\n  ctx.lineTo(containerX, containerY + containerRadius);\n  ctx.quadraticCurveTo(containerX, containerY, containerX + containerRadius, containerY);\n  ctx.closePath();\n  ctx.fill();\n  \n  ctx.strokeStyle = 'rgba(255, 255, 255, 0.3)';\n  ctx.lineWidth = 2;\n  ctx.stroke();\n  \n  ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';\n  ctx.lineWidth = 1;\n  ctx.stroke();\n\n  const albumSize = 280;\n  const albumX = containerX + 40;\n  const albumY = containerY + (containerHeight - albumSize) / 2;\n  \n  const thumbnailPath = `${__dirname}/cache/thumb-${videoId}.jpg`;\n  const thumbnailExists = await downloadThumbnail(videoId, thumbnailPath);\n  \n  if (thumbnailExists && fs.existsSync(thumbnailPath)) {\n    try {\n      const thumbnail = await loadImage(thumbnailPath);\n      \n      const radius = 20;\n      ctx.save();\n      ctx.beginPath();\n      ctx.moveTo(albumX + radius, albumY);\n      ctx.lineTo(albumX + albumSize - radius, albumY);\n      ctx.quadraticCurveTo(albumX + albumSize, albumY, albumX + albumSize, albumY + radius);\n      ctx.lineTo(albumX + albumSize, albumY + albumSize - radius);\n      ctx.quadraticCurveTo(albumX + albumSize, albumY + albumSize, albumX + albumSize - radius, albumY + albumSize);\n      ctx.lineTo(albumX + radius, albumY + albumSize);\n      ctx.quadraticCurveTo(albumX, albumY + albumSize, albumX, albumY + albumSize - radius);\n      ctx.lineTo(albumX, albumY + radius);\n      ctx.quadraticCurveTo(albumX, albumY, albumX + radius, albumY);\n      ctx.closePath();\n      ctx.clip();\n      \n      ctx.drawImage(thumbnail, albumX, albumY, albumSize, albumSize);\n      ctx.restore();\n      \n      ctx.strokeStyle = 'rgba(255, 255, 255, 0.4)';\n      ctx.lineWidth = 2;\n      ctx.beginPath();\n      ctx.moveTo(albumX + radius, albumY);\n      ctx.lineTo(albumX + albumSize - radius, albumY);\n      ctx.quadraticCurveTo(albumX + albumSize, albumY, albumX + albumSize, albumY + radius);\n      ctx.lineTo(albumX + albumSize, albumY + albumSize - radius);\n      ctx.quadraticCurveTo(albumX + albumSize, albumY + albumSize, albumX + albumSize - radius, albumY + albumSize);\n      ctx.lineTo(albumX + radius, albumY + albumSize);\n      ctx.quadraticCurveTo(albumX, albumY + albumSize, albumX, albumY + albumSize - radius);\n      ctx.lineTo(albumX, albumY + radius);\n      ctx.quadraticCurveTo(albumX, albumY, albumX + radius, albumY);\n      ctx.closePath();\n      ctx.stroke();\n      \n      fs.unlinkSync(thumbnailPath);\n    } catch (error) {\n      console.log('Error loading thumbnail:', error);\n      const radius = 20;\n      ctx.fillStyle = '#e9ecef';\n      ctx.beginPath();\n      ctx.moveTo(albumX + radius, albumY);\n      ctx.lineTo(albumX + albumSize - radius, albumY);\n      ctx.quadraticCurveTo(albumX + albumSize, albumY, albumX + albumSize, albumY + radius);\n      ctx.lineTo(albumX + albumSize, albumY + albumSize - radius);\n      ctx.quadraticCurveTo(albumX + albumSize, albumY + albumSize, albumX + albumSize - radius, albumY + albumSize);\n      ctx.lineTo(albumX + radius, albumY + albumSize);\n      ctx.quadraticCurveTo(albumX, albumY + albumSize, albumX, albumY + albumSize - radius);\n      ctx.lineTo(albumX, albumY + radius);\n      ctx.quadraticCurveTo(albumX, albumY, albumX + radius, albumY);\n      ctx.closePath();\n      ctx.fill();\n      \n      ctx.fillStyle = '#6c757d';\n      ctx.font = '48px Arial';\n      ctx.textAlign = 'center';\n      ctx.fillText('🎵', albumX + albumSize/2, albumY + albumSize/2 + 15);\n    }\n  } else {\n    const radius = 20;\n    ctx.fillStyle = '#e9ecef';\n    ctx.beginPath();\n    ctx.moveTo(albumX + radius, albumY);\n    ctx.lineTo(albumX + albumSize - radius, albumY);\n    ctx.quadraticCurveTo(albumX + albumSize, albumY, albumX + albumSize, albumY + radius);\n    ctx.lineTo(albumX + albumSize, albumY + albumSize - radius);\n    ctx.quadraticCurveTo(albumX + albumSize, albumY + albumSize, albumX + albumSize - radius, albumY + albumSize);\n    ctx.lineTo(albumX + radius, albumY + albumSize);\n    ctx.quadraticCurveTo(albumX, albumY + albumSize, albumX, albumY + albumSize - radius);\n    ctx.lineTo(albumX, albumY + radius);\n    ctx.quadraticCurveTo(albumX, albumY, albumX + radius, albumY);\n    ctx.closePath();\n    ctx.fill();\n    \n    ctx.fillStyle = '#6c757d';\n    ctx.font = '48px Arial';\n    ctx.textAlign = 'center';\n    ctx.fillText('🎵', albumX + albumSize/2, albumY + albumSize/2 + 15);\n  }\n\n  const infoX = albumX + albumSize + 50;\n  const infoWidth = containerWidth - (albumSize + 90);\n  let currentY = containerY + 60;\n\n  ctx.fillStyle = '#ffffff';\n  ctx.shadowColor = 'rgba(0, 0, 0, 0.8)';\n  ctx.shadowBlur = 4;\n  ctx.font = 'bold 32px Arial';\n  ctx.textAlign = 'left';\n  \n  const titleMaxWidth = infoWidth - 80;\n  const words = data.title.split(' ');\n  const lines = [];\n  let currentLine = '';\n  \n  for (let i = 0; i < words.length; i++) {\n    const testLine = currentLine + (currentLine ? ' ' : '') + words[i];\n    const metrics = ctx.measureText(testLine);\n    \n    if (metrics.width > titleMaxWidth && currentLine) {\n      lines.push(currentLine);\n      currentLine = words[i];\n    } else {\n      currentLine = testLine;\n    }\n  }\n  if (currentLine) lines.push(currentLine);\n  \n  if (lines.length > 2) {\n    lines[1] = lines[1] + '...';\n    lines.splice(2);\n  }\n  \n  for (let i = 0; i < lines.length; i++) {\n    ctx.fillText(lines[i], infoX, currentY + (i * 40));\n  }\n  ctx.shadowBlur = 0;\n  \n  currentY += lines.length * 40 + 10;\n\n  ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';\n  ctx.font = '20px Arial';\n  const truncatedAuthor = truncateText(data.author, 35);\n  ctx.fillText(truncatedAuthor, infoX, currentY);\n  currentY += 40;\n\n  ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';\n  ctx.strokeStyle = 'rgba(255, 255, 255, 0.5)';\n  ctx.lineWidth = 2;\n  ctx.beginPath();\n  ctx.arc(infoX + infoWidth - 30, containerY + 70, 20, 0, Math.PI * 2);\n  ctx.fill();\n  ctx.stroke();\n  \n  ctx.fillStyle = '#dc3545';\n  ctx.font = '20px Arial';\n  ctx.textAlign = 'center';\n  ctx.fillText('♥', infoX + infoWidth - 30, containerY + 76);\n\n  const progressY = currentY + 20;\n  const progressWidth = infoWidth - 40;\n  \n  ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';\n  ctx.fillRect(infoX, progressY - 3, progressWidth, 6);\n  \n  const progress = Math.random() * 0.7 + 0.1;\n  ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';\n  ctx.fillRect(infoX, progressY - 3, progressWidth * progress, 6);\n  \n  ctx.fillStyle = '#ffffff';\n  ctx.beginPath();\n  ctx.arc(infoX + (progressWidth * progress), progressY, 8, 0, Math.PI * 2);\n  ctx.fill();\n  \n  currentY += 60;\n\n  const buttonY = currentY;\n  const buttonSize = 40;\n  const buttonSpacing = 70;\n  \n  function drawButtonBg(x, y, size, color = '#ffffff') {\n    ctx.fillStyle = color;\n    ctx.strokeStyle = 'rgba(0, 0, 0, 0.1)';\n    ctx.lineWidth = 1;\n    ctx.beginPath();\n    ctx.arc(x, y, size/2, 0, Math.PI * 2);\n    ctx.fill();\n    ctx.stroke();\n  }\n  \n  drawButtonBg(infoX + 25, buttonY, buttonSize);\n  ctx.fillStyle = '#28a745';\n  ctx.lineWidth = 2;\n  ctx.strokeStyle = '#28a745';\n  ctx.beginPath();\n  ctx.moveTo(infoX + 15, buttonY - 5);\n  ctx.lineTo(infoX + 25, buttonY - 5);\n  ctx.lineTo(infoX + 22, buttonY - 8);\n  ctx.moveTo(infoX + 25, buttonY - 5);\n  ctx.lineTo(infoX + 22, buttonY - 2);\n  ctx.moveTo(infoX + 35, buttonY + 5);\n  ctx.lineTo(infoX + 25, buttonY + 5);\n  ctx.lineTo(infoX + 28, buttonY + 8);\n  ctx.moveTo(infoX + 25, buttonY + 5);\n  ctx.lineTo(infoX + 28, buttonY + 2);\n  ctx.stroke();\n  \n  drawButtonBg(infoX + 25 + buttonSpacing, buttonY, buttonSize);\n  ctx.fillStyle = '#6f42c1';\n  ctx.beginPath();\n  ctx.moveTo(infoX + 25 + buttonSpacing - 8, buttonY);\n  ctx.lineTo(infoX + 25 + buttonSpacing - 2, buttonY - 6);\n  ctx.lineTo(infoX + 25 + buttonSpacing - 2, buttonY + 6);\n  ctx.closePath();\n  ctx.fill();\n  ctx.beginPath();\n  ctx.moveTo(infoX + 25 + buttonSpacing + 2, buttonY);\n  ctx.lineTo(infoX + 25 + buttonSpacing + 8, buttonY - 6);\n  ctx.lineTo(infoX + 25 + buttonSpacing + 8, buttonY + 6);\n  ctx.closePath();\n  ctx.fill();\n  \n  ctx.fillStyle = '#007bff';\n  ctx.beginPath();\n  ctx.arc(infoX + 25 + buttonSpacing * 2, buttonY, 28, 0, Math.PI * 2);\n  ctx.fill();\n  \n  ctx.strokeStyle = 'rgba(0, 0, 0, 0.1)';\n  ctx.lineWidth = 1;\n  ctx.stroke();\n  \n  ctx.fillStyle = '#ffffff';\n  ctx.beginPath();\n  ctx.moveTo(infoX + 25 + buttonSpacing * 2 - 6, buttonY - 10);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 2 - 6, buttonY + 10);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 2 + 10, buttonY);\n  ctx.closePath();\n  ctx.fill();\n  \n  drawButtonBg(infoX + 25 + buttonSpacing * 3, buttonY, buttonSize);\n  ctx.fillStyle = '#6f42c1';\n  ctx.beginPath();\n  ctx.moveTo(infoX + 25 + buttonSpacing * 3 - 8, buttonY - 6);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 3 - 8, buttonY + 6);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 3 - 2, buttonY);\n  ctx.closePath();\n  ctx.fill();\n  ctx.beginPath();\n  ctx.moveTo(infoX + 25 + buttonSpacing * 3 + 2, buttonY - 6);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 3 + 2, buttonY + 6);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 3 + 8, buttonY);\n  ctx.closePath();\n  ctx.fill();\n  \n  drawButtonBg(infoX + 25 + buttonSpacing * 4, buttonY, buttonSize);\n  ctx.fillStyle = '#fd7e14';\n  ctx.strokeStyle = '#fd7e14';\n  ctx.lineWidth = 2;\n  ctx.beginPath();\n  ctx.arc(infoX + 25 + buttonSpacing * 4, buttonY, 8, 0, Math.PI * 1.5);\n  ctx.stroke();\n  ctx.fillStyle = '#fd7e14';\n  ctx.beginPath();\n  ctx.moveTo(infoX + 25 + buttonSpacing * 4 + 8, buttonY - 8);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 4 + 5, buttonY - 5);\n  ctx.lineTo(infoX + 25 + buttonSpacing * 4 + 8, buttonY - 2);\n  ctx.closePath();\n  ctx.fill();\n  \n  currentY += 80;\n\n  return canvas.toBuffer('image/png');\n}\n\n  async function downloadMusicFromYoutube(link, filePath, options = {}) {\n    const timeStart = Date.now();\n\n    if (!link) throw new Error('Thiếu link');\n    if (!filePath) throw new Error('Thiếu đường dẫn file');\n\n    try {\n      await fs.promises.mkdir(path.dirname(filePath), { recursive: true }).catch(() => {});\n      const videoInfo = await ytdl.getInfo(link);\n      const audioFormats = ytdl.filterFormats(videoInfo.formats, 'audioonly');\n\n      let selectedFormat =\n        audioFormats.find(format =>\n          format.quality === 'tiny' &&\n          format.audioBitrate === 128 &&\n          format.hasAudio === true\n        ) ||\n        audioFormats.find(format =>\n          format.audioBitrate === 128 &&\n          format.hasAudio === true\n        ) ||\n        ytdl.chooseFormat(audioFormats, { quality: 'highestaudio' });\n\n      if (!selectedFormat) {\n        throw new Error('Không tìm thấy format audio phù hợp');\n      }\n      const downloadOptions = {\n        format: selectedFormat,\n        multiThread: options.multiThread !== false,\n        maxThreads: options.maxThreads || 4,\n        minSizeForMultiThread: options.minSizeForMultiThread || 2 * 1024 * 1024,\n        requestOptions: {\n          timeout: options.timeout || 120000,\n          maxRetries: 3\n        }\n      };\n      const downloadResult = await new Promise((resolve, reject) => {\n        const stream = ytdl(link, downloadOptions);\n        const writeStream = fs.createWriteStream(filePath);\n\n        let downloadedBytes = 0;\n        let hasError = false;\n\n        stream.on('progress', (chunkLength, downloaded, total) => {\n          downloadedBytes = downloaded;\n        });\n\n        const handleSuccess = () => {\n          if (hasError) return;\n          const duration = (Date.now() - timeStart) / 1000;\n          const avgSpeed = Math.round(downloadedBytes / 1024 / duration);\n          resolve({ downloadedBytes, duration, avgSpeed });\n        };\n\n        const handleError = (error) => {\n          if (hasError) return;\n          hasError = true;\n          stream.destroy();\n          writeStream.destroy();\n          reject(error);\n        };\n\n        stream.on('end', () => writeStream.end());\n        writeStream.on('finish', handleSuccess);\n        stream.on('error', handleError);\n        writeStream.on('error', handleError);\n        setTimeout(() => {\n          if (!hasError) handleError(new Error('Download timeout'));\n        }, downloadOptions.requestOptions.timeout);\n\n        stream.pipe(writeStream);\n      });\n      const fileStats = fs.statSync(filePath);\n      if (fileStats.size === 0) {\n        throw new Error('Downloaded file is empty');\n      }\n      return {\n        title: videoInfo.videoDetails.title,\n        dur: Number(videoInfo.videoDetails.lengthSeconds),\n        sub: Number(videoInfo.videoDetails.author.subscriber_count) || 0,\n        viewCount: Number(videoInfo.videoDetails.viewCount) || 0,\n        author: videoInfo.videoDetails.author.name,\n        timestart: timeStart,\n        filePath: filePath,\n        fileSize: fileStats.size,\n        fileSizeMB: Number((fileStats.size / 1024 / 1024).toFixed(2)),\n        downloadTime: downloadResult.duration,\n        downloadSpeed: downloadResult.avgSpeed,\n        totalTime: (Date.now() - timeStart) / 1000,\n        multiThreadUsed: selectedFormat.contentLength > downloadOptions.minSizeForMultiThread,\n        success: true\n      };\n\n    } catch (error) {\n      if (fs.existsSync(filePath)) {\n        try { fs.unlinkSync(filePath); } catch (e) {}\n      }\n      throw error;\n    }\n  }\n\nmodule.exports.config = {\n  name: \"sing1\",\n  version: \"2.2.0\",\n  hasPermssion: 0,\n  credits: \"D-Jukie, Satoru \",\n  description: \"Phát nhạc thông qua link YouTube, Video ID hoặc từ khoá tìm kiếm\",\n  commandCategory: \"Nhạc\",\n  usages: \"[searchMusic/YouTubeURL/VideoID]\",\n  cooldowns: 0\n}\n\nconst moment = require(\"moment-timezone\");\nvar gio = moment.tz(\"Asia/Ho_Chi_Minh\").format(\"HH:mm:ss || D/MM/YYYY\");\n\nmodule.exports.run = async function ({ api, event, args, Users}) {\n  var thu = moment.tz('Asia/Ho_Chi_Minh').format('dddd');\n  if (thu == 'Sunday') thu = 'Chủ Nhật'\n  if (thu == 'Monday') thu = 'Thứ Hai'\n  if (thu == 'Tuesday') thu = 'Thứ Ba'\n  if (thu == 'Wednesday') thu = 'Thứ Tư'\n  if (thu == \"Thursday\") thu = 'Thứ Năm'\n  if (thu == 'Friday') thu = 'Thứ Sáu'\n  if (thu == 'Saturday') thu = 'Thứ Bảy'\n  let name = await Users.getNameUser(event.senderID);\n  \n  if (args.length == 0 || !args) {\n    api.setMessageReaction(\"❎\", event.messageID, () => { }, true);\n    api.sendMessage('➣ 𝗛𝗮̃𝘆 𝗻𝗵𝗮̣̂𝗽:\\n• 𝗧𝘂̀ 𝗸𝗵𝗼́𝗮 𝘁𝗶̀𝗺 𝗸𝗶𝗲̂́𝗺\\n• 𝗟𝗶𝗻𝗸 𝗬𝗼𝘂𝗧𝘂𝗯𝗲\\n• 𝗜𝗗 𝘃𝗶𝗱𝗲𝗼 𝗬𝗼𝘂𝗧𝘂𝗯𝗲', event.threadID, event.messageID);\n    return;\n  };\n\n  const input = args.join(\" \");\n  var path = `${__dirname}/cache/sing-${event.senderID}.mp3`\n  if (fs.existsSync(path)) { \n    fs.unlinkSync(path)\n  }\n  \n  if (isYouTubeUrlOrId(input)) {\n    try {\n      api.setMessageReaction(\"⌛\", event.messageID, () => { }, true);\n      \n      const videoId = extractVideoId(input);\n      const youtubeUrl = `https://www.youtube.com/watch?v=${videoId}`;\n      var data = await downloadMusicFromYoutube(youtubeUrl, path);\n      \n      if (fs.statSync(path).size > 26214400) {\n        return api.sendMessage('𝐁𝐚̀𝐢 𝐠𝐢̀ 𝐦𝐚̀ 𝐝𝐚̀𝐢 𝐝𝐮̛̃ 𝐯𝐚̣̂𝐲, đ𝐨̂̉𝐢 𝐛𝐚̀𝐢 đ𝐢 😠', event.threadID, () => fs.unlinkSync(path), event.messageID);\n      }\n      \n      var imagePath = `${__dirname}/cache/player-${event.senderID}.png`\n      const canvasBuffer = await createMusicPlayerCanvas(data, name, videoId);\n      fs.writeFileSync(imagePath, canvasBuffer);\n      \n      api.setMessageReaction(\"✅\", event.messageID, () => { }, true);\n      \n      api.sendMessage({ \n        attachment: fs.createReadStream(imagePath)\n      }, event.threadID, () => {\n        fs.unlinkSync(imagePath);\n        setTimeout(() => {\n          api.sendMessage({ \n            attachment: fs.createReadStream(path)\n          }, event.threadID, () => {\n            fs.unlinkSync(path);\n          });\n        }, 100);\n      }, event.messageID);\n      \n    } catch (e) {\n      console.log(e);\n      api.setMessageReaction(\"❎\", event.messageID, () => { }, true);\n      return api.sendMessage('➣ 𝗟𝗼̂̃𝗶 𝗸𝗵𝗶 𝘁𝗮̉𝗶 𝗻𝗵𝗮̣𝗰! 𝗞𝗶𝗲̂̉𝗺 𝘁𝗿𝗮 𝗹𝗮̣𝗶 𝗹𝗶𝗻𝗸/𝗜𝗗', event.threadID, event.messageID);\n    }\n  } else {\n    try {\n      var link = [],\n        msg = \"\",\n        num = 0\n      const Youtube = require('youtube-search-api');\n      api.setMessageReaction(\"⌛\", event.messageID, () => { }, true);\n      var allData = (await Youtube.GetListByKeyword(input, false, 50)).items;\n      var filteredData = filterVideosByDuration(allData, 23);\n      var data = filteredData.slice(0, 8);\n      \n      if (data.length === 0) {\n        api.setMessageReaction(\"❎\", event.messageID, () => { }, true);\n        return api.sendMessage('➣ 𝗞𝗵𝗼̂𝗻𝗴 𝘁𝗶̀𝗺 𝘁𝗵𝗮̂́𝘆 𝘃𝗶𝗱𝗲𝗼 𝗻𝗮̀𝗼 𝗰𝗼́ 𝘁𝗵𝗼̛̀𝗶 𝗹𝘂̛𝗼̛̣𝗻𝗴 ≤ 𝟮𝟯 𝗽𝗵𝘂́𝘁!', event.threadID, event.messageID);\n      }\n      \n      for (let value of data) {\n        link.push(value.id);\n        num = num+=1\n        api.setMessageReaction(\"✅\", event.messageID, () => { }, true);\n\n        let duration = \"N/A\";\n        if (value.length && value.length.simpleText) {\n          duration = value.length.simpleText;\n        } else if (value.length) {\n          duration = value.length.toString();\n        } else if (value.lengthText) {\n          duration = value.lengthText;\n        } else if (value.lengthSeconds) {\n          const minutes = Math.floor(value.lengthSeconds / 60);\n          const seconds = value.lengthSeconds % 60;\n          duration = `${minutes}:${seconds < 10 ? '0' + seconds : seconds}`;\n        }\n        \n        msg += (`➣ Kết quả: ${num} - ${value.title}\\n➣ 𝐓𝐞̂𝐧 𝐤𝐞̂𝐧𝐡: ${value.channelTitle || \"N/A\"}\\n➣ 𝐓𝐡𝐨̛̀𝐢 𝐥𝐮̛𝐨̛̣𝐧𝐠: ${duration}\\n====================\\n`);\n      }\n      var body = `==『 𝙼𝚘̛̀𝚒 𝚋𝚊̣𝚗 𝚘𝚛𝚍𝚎𝚛 𝚖𝚎𝚗𝚞 』==\\n====================\\n${msg}➝ 𝙼𝚘̛̀𝚒 ${name} 𝚝𝚛𝚊̉ 𝚕𝚘̛̀𝚒 𝚝𝚒𝚗 𝚗𝚑𝚊̆́𝚗 𝚗𝚊̀𝚢 𝚔𝚎̀𝚖 𝚜𝚘̂́ 𝚝𝚑𝚞̛́ 𝚝𝚞̛̣ 𝚖𝚊̀ 𝚋𝚊̣𝚗 𝚖𝚞𝚘̂́𝚗 𝚗𝚐𝚑𝚎 𝚋𝚘𝚝 𝚜𝚎̃ 𝚘𝚛𝚍𝚎𝚛 𝚌𝚑𝚘 𝚋𝚊̣𝚗`\n      \n      return api.sendMessage({\n        body: body\n      }, event.threadID, (error, info) => global.client.handleReply.push({\n        type: 'reply',\n        name: this.config.name,\n        messageID: info.messageID,\n        author: event.senderID,\n        link\n      }), event.messageID);\n      \n    } catch(e) {\n      console.log(e)\n      api.setMessageReaction(\"❎\", event.messageID, () => { }, true);\n      return api.sendMessage('➣ 𝗟𝗼̂̃𝗶 𝗸𝗵𝗶 𝘁𝗶̀𝗺 𝗸𝗶𝗲̂́𝗺!', event.threadID, event.messageID);\n    } \n  }\n}\n\nmodule.exports.handleReply = async function ({ api, event, handleReply, Users }) {\n  api.setMessageReaction(\"⌛\", event.messageID, () => { }, true);\n  const moment = require(\"moment-timezone\");\n  var gio = moment.tz(\"Asia/Ho_Chi_Minh\").format(\"HH:mm:ss || D/MM/YYYY\");\n  var thu = moment.tz('Asia/Ho_Chi_Minh').format('dddd');\n  if (thu == 'Sunday') thu = 'Chủ Nhật'\n  if (thu == 'Monday') thu = 'Thứ Hai'\n  if (thu == 'Tuesday') thu = 'Thứ Ba'\n  if (thu == 'Wednesday') thu = 'Thứ Tư'\n  if (thu == \"Thursday\") thu = 'Thứ Năm'\n  if (thu == 'Friday') thu = 'Thứ Sáu'\n  if (thu == 'Saturday') thu = 'Thứ Bảy'\n  let name = await Users.getNameUser(event.senderID);\n  \n  const { createReadStream, unlinkSync, statSync } = require(\"fs-extra\")\n  try {\n    var path = `${__dirname}/cache/sing-${event.senderID}.mp3`\n    var imagePath = `${__dirname}/cache/player-${event.senderID}.png`\n    \n    const videoId = handleReply.link[event.body - 1];\n    var data = await downloadMusicFromYoutube('https://www.youtube.com/watch?v=' + videoId, path);\n    \n    if (fs.statSync(path).size > 26214400) {\n      return api.sendMessage('𝐁𝐚̀𝐢 𝐠𝐢̀ 𝐦𝐚̀ 𝐝𝐚̀𝐢 𝐝𝐮̛̃ 𝐯𝐚̣̂𝐲, đ𝐨̂̉𝐢 𝐛𝐚̀𝐢 đ𝐢 😠', event.threadID, () => fs.unlinkSync(path), event.messageID);\n    }\n    \n    const canvasBuffer = await createMusicPlayerCanvas(data, name, videoId);\n    fs.writeFileSync(imagePath, canvasBuffer);\n    \n    api.unsendMessage(handleReply.messageID);\n    api.setMessageReaction(\"✅\", event.messageID, () => { }, true);\n    api.sendMessage({ \n      attachment: fs.createReadStream(imagePath)\n    }, event.threadID, () => {\n      fs.unlinkSync(imagePath);\n      setTimeout(() => {\n        api.sendMessage({ \n          attachment: fs.createReadStream(path)\n        }, event.threadID, () => {\n          fs.unlinkSync(path);\n        });\n      }, 100);\n    }, event.messageID);\n  }\n  catch (e) { \n    console.log(e);\n    return api.sendMessage('Có lỗi xảy ra khi tạo player', event.threadID, event.messageID);\n  }\n}\n\nmodule.exports.convertHMS = function(value) {\n  const sec = parseInt(value, 10); \n  let hours = Math.floor(sec / 3600);\n  let minutes = Math.floor((sec - (hours * 3600)) / 60);\n  let seconds = sec - (hours * 3600) - (minutes * 60); \n  if (hours   < 10) {hours   = \"0\"+hours;}\n  if (minutes < 10) {minutes = \"0\"+minutes;}\n  if (seconds < 10) {seconds = \"0\"+seconds;}\n  return (hours != '00' ? hours +':': '') + minutes+':'+seconds;\n}",
  "language": "javascript",
  "createdAt": 1758552636095,
  "updatedAt": 1758552636095
}