|
| 1 | +import { Message, MessageMedia } from "whatsapp-web.js"; |
| 2 | +import axios from "axios"; |
| 3 | +import log from "../components/utils/log"; |
| 4 | +import fs from "fs/promises"; |
| 5 | + |
| 6 | +export const info = { |
| 7 | + command: "meme", |
| 8 | + description: "Fetch a random meme image.", |
| 9 | + usage: "meme", |
| 10 | + example: "meme", |
| 11 | + role: "user", |
| 12 | + cooldown: 5000, |
| 13 | +}; |
| 14 | + |
| 15 | +export default async function (msg: Message) { |
| 16 | + if (!/^meme$/i.test(msg.body)) return; |
| 17 | + |
| 18 | + await axios |
| 19 | + .get("https://api.popcat.xyz/meme") |
| 20 | + .then(async (response) => { |
| 21 | + await axios |
| 22 | + .get(response.data.content.image, { |
| 23 | + responseType: "arraybuffer", |
| 24 | + }) |
| 25 | + .then(async (response) => { |
| 26 | + const tempDir = "./.temp"; |
| 27 | + await fs.mkdir(tempDir, { recursive: true }); |
| 28 | + |
| 29 | + const tempPath = `${tempDir}/meme_${Date.now()}.png`; |
| 30 | + await fs.writeFile(tempPath, response.data); |
| 31 | + |
| 32 | + const media = MessageMedia.fromFilePath(tempPath); |
| 33 | + await msg.reply(media); |
| 34 | + await fs.unlink(tempPath); |
| 35 | + }) |
| 36 | + .catch(async (error) => { |
| 37 | + log.error("meme", `Error fetching image: ${error.message}`); |
| 38 | + await msg.reply("Error fetching image. Please try again later."); |
| 39 | + }); |
| 40 | + }) |
| 41 | + .catch(async (error) => { |
| 42 | + log.error("meme", `Error fetching data: ${error.message}`); |
| 43 | + await msg.reply("Error fetching data. Please try again later."); |
| 44 | + }); |
| 45 | +} |
0 commit comments