Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
949 B
1
Indexable
Never
const fs = require("fs");
require("dotenv").config();
const downloadFolderName = process.env.PATH_FILES;

const handleMedia = async (msg) => {
  const media = await msg.downloadMedia();

  let fileName = media.filename;
  if (!fileName) {
    const timestamp = Date.now();
    const fileExtension = getFileExtension(media.mimetype);
    fileName = `${timestamp}.${fileExtension}`;
  }

  fs.writeFileSync(`${downloadFolderName}/${fileName}`, media.data, "base64");
  console.log(`Downloaded ${fileName}`);

  // ChatGPT with "msg.body"

  fs.renameSync(
    `${downloadFolderName}/${fileName}`,
    `${downloadFolderName}/ib#@_${fileName}`
  );
  console.log(`Renamed ib#@_${fileName}`);
  msg.react("👍");
};

const getFileExtension = (mimetype) => {
  const pattern = /([^\/;]+)(?:;|$)/;
  const extensionMatch = mimetype.match(pattern);

  return extensionMatch[1];
};

module.exports = {
  handleMedia,
};
Leave a Comment