Advertisement
samiroexpikachu

BD calendar

Apr 13th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2. const fs = require('fs');
  3.  
  4. module.exports = {
  5.   config: {
  6.     name: "bc",
  7.     aliases: ["bdcalendar", "calendar"],
  8.     version: "1.0",
  9.     author: "Samir Œ",
  10.     shortDescription: "Get the calendar for a specific month and year",
  11.     longDescription: "Fetches and sends an image of the calendar for the specified month and year.",
  12.     category: "Utility",
  13.     guide: {
  14.       en: "{pn} [month | year]"
  15.     }
  16.   },
  17.  
  18.   onStart: async function ({ api, event, args }) {
  19.     try {
  20.       let month, year;
  21.       if (args.length > 0) {
  22.         [month, year] = args.join(" ").split("|").map(item => item.trim());
  23.       } else {
  24.         month = new Date().getMonth() + 1;
  25.         year = new Date().getFullYear();
  26.       }
  27.  
  28.       let apiUrl = `https://apis-samir.onrender.com/calendar/${year}/${month}`;
  29.      
  30.       const response = await axios.get(apiUrl, { responseType: 'stream' });
  31.       const path = `calendar-${month}-${year}.jpg`;
  32.       const writer = fs.createWriteStream(path);
  33.  
  34.       response.data.pipe(writer);
  35.  
  36.       await new Promise((resolve, reject) => {
  37.         writer.on('finish', resolve);
  38.         writer.on('error', reject);
  39.       });
  40.  
  41.       await api.sendMessage({
  42.         body: `Here is the calendar for ${month}/${year}:`,
  43.         attachment: fs.createReadStream(path)
  44.       }, event.threadID);
  45.  
  46.       fs.unlinkSync(path);
  47.     } catch (error) {
  48.       console.error("Error retrieving calendar:", error.message);
  49.       api.sendMessage({ body: "Failed to retrieve calendar." }, event.threadID);
  50.     }
  51.   }
  52. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement