File size: 4,075 Bytes
4ad8ff9
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
{
  "id": "a1ce0156-99b3-404f-b502-716049a76256",
  "title": "yyy",
  "content": "\nconst axios = require('axios');\nconst fs = require('fs');\nconst pathLib = require('path');\n\nconst BASE_URL = 'https://twannote.vercel.app'; \n\nfunction detectLanguageByExt(filePath) {\n  const ext = pathLib.extname(filePath).slice(1).toLowerCase();\n  const map = {\n    js: 'javascript',\n    mjs: 'javascript',\n    cjs: 'javascript',\n    ts: 'typescript',\n    py: 'python',\n    json: 'json',\n    html: 'html',\n    htm: 'html',\n    css: 'css',\n    md: 'markdown',\n    sh: 'bash',\n    bash: 'bash',\n    txt: 'text'\n  };\n  return map[ext] || 'text';\n}\n\nasync function createNoteOnAPI(filePath) {\n  const content = fs.readFileSync(filePath, 'utf8');\n  const title = pathLib.basename(filePath);\n  const language = detectLanguageByExt(filePath);\n\n  const res = await axios.post(`${BASE_URL}/api/code`, {\n    title,\n    content,\n    language\n  }, { timeout: 20000 });\n\n  const id = res.data?.id;\n  if (!id) throw new Error('API trả về không có id');\n\n  const rawUrl = `${BASE_URL}/code/${id}/raw`;\n  const viewUrl = `${BASE_URL}/api/code/${id}`;   // JSON view\n  const pageUrl = `${BASE_URL}/code/${id}`;       // nếu có trang UI /code/:id\n\n  return { id, rawUrl, viewUrl, pageUrl };\n}\n\nthis.zuckbotconfig = {\n  name: 'note',\n  version: '2.3.1.3-api',\n  author: \"Nguyễn Thanh Mài\",\n  role: 2,\n  info: `${BASE_URL}/code/:id`,\n  Category: 'Admin',\n  cd: 3,\n  shadowPrefix: false,\n  image: []\n};\n\nthis.onRun = async function (o) {\n  const name = this.zuckbotconfig.name;\n  const maybeUrl = o.event?.messageReply?.args?.[0] || o.args[1];\n  let targetPath = `${__dirname}/${o.args[0]}`;\n  const send = msg => new Promise(r => o.api.sendMessage(msg, o.event.threadID, (err, res) => r(res), o.event.messageID));\n\n  try {\n    if (/^https?:\\/\\//i.test(maybeUrl)) {\n      return send(`🔗 URL: ${maybeUrl}\\n📄 File đích: ${targetPath}\\n\\nThả cảm xúc để **xác nhận** thay thế nội dung file từ URL này.`).then(res => {\n        res = {\n          ...res,\n          name,\n          path: targetPath,\n          o,\n          url: maybeUrl,\n          action: 'confirm_replace_content_from_url',\n        };\n        global.zuckbot.onReaction.push(res);\n      });\n    }\n\n    if (!fs.existsSync(targetPath)) {\n      return send(`❎ Đường dẫn file không tồn tại để upload: ${targetPath}`);\n    }\n\n    const { id, rawUrl, viewUrl, pageUrl } = await createNoteOnAPI(targetPath);\n\n    return send(\n      `📝 Đã tạo note trên API\\n` +\n      `• ID: ${id}\\n` +\n      `• Raw: ${rawUrl}\\n` +\n      `• JSON: ${viewUrl}\\n` +\n      `• Page: ${pageUrl}\\n` +\n      `────────────────\\n` +\n      `📄 File nguồn: ${targetPath}\\n\\n` +\n      `📌 Thả cảm xúc để **tải nội dung từ Raw về và ghi đè** file nguồn.`\n    ).then(res => {\n      res = {\n        ...res,\n        name,\n        path: targetPath,\n        o,\n        url: rawUrl,\n        action: 'confirm_replace_content_from_url',\n      };\n      global.zuckbot.onReaction.push(res);\n    });\n\n  } catch (e) {\n    console.error(e);\n    send(e.toString());\n  }\n};\n\nthis.onReaction = async function (o) {\n  const _ = o.onReaction;\n  const send = msg => new Promise(r => o.api.sendMessage(msg, o.event.threadID, (err, res) => r(res), o.event.messageID));\n\n  try {\n    if (o.event.userID != _.o.event.senderID) return;\n\n    switch (_.action) {\n      case 'confirm_replace_content_from_url': {\n        const content = (await axios.get(_.url, { responseType: 'text', timeout: 20000 })).data;\n        fs.writeFileSync(_.path, content);\n        send(`✅ Đã ghi đè nội dung file\\n\\n🔗 File: ${_.path}`);\n      } break;\n\n      default:\n        break;\n    }\n  } catch (e) {\n    console.error(e);\n    send(e.toString());\n  }\n};",
  "language": "javascript",
  "createdAt": 1755523115816,
  "updatedAt": 1755523115816
}