const axios = require('axios'); const fs = require('fs'); module.exports = { config: { name: "mlhero", aliases: [], version: "1.0", author: "Jmlabaco", countDown: 5, role: 0, shortDescription: "Search for a hero in MLBB.", longDescription: "Search for a hero in Mobile Legends: Bang Bang by name.", category: "box chat", guide: "To use this command, type mlhero .", isPrefix: "both", }, onStart: async function ({ api, event, args }) { if (args.length === 0) { return api.sendMessage("Please provide the name of the hero.", event.threadID); } const heroName = args.join(" "); const apiUrl = `https://apis-samir.onrender.com/mobile-legends/hero/${encodeURIComponent(heroName)}`; try { const response = await axios.get(apiUrl); const heroData = response.data; // Download and save the hero image const imageUrl = heroData.hero_img; const imageName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); const imagePath = __dirname + "/cache/" + imageName; const imageWriter = fs.createWriteStream(imagePath); const imageResponse = await axios.get(imageUrl, { responseType: 'stream' }); imageResponse.data.pipe(imageWriter); // Once image is saved, prepare hero information message imageWriter.on('finish', () => { let heroInfoMessage = `**Hero Information:**\n`; heroInfoMessage += `Name: ${heroName}\n`; heroInfoMessage += `Release Date: ${heroData.release}\n`; heroInfoMessage += `Role: ${heroData.role}\n`; heroInfoMessage += `Specialty: ${heroData.specialty}\n`; heroInfoMessage += `Lane: ${heroData.lane}\n`; heroInfoMessage += `Price: ${heroData.price}\n`; heroInfoMessage += `**Gameplay Info:**\n`; heroInfoMessage += `Durability: ${heroData.gameplay_info.durability}\n`; heroInfoMessage += `Offense: ${heroData.gameplay_info.offense}\n`; heroInfoMessage += `Control Effect: ${heroData.gameplay_info.control_effect}\n`; heroInfoMessage += `Difficulty: ${heroData.gameplay_info.difficulty}\n`; heroInfoMessage += `**Story Info:**\n`; heroInfoMessage += `Origin: ${heroData.story_info_list.Origin}\n`; heroInfoMessage += `Gender: ${heroData.story_info_list.Gender}\n`; heroInfoMessage += `Species: ${heroData.story_info_list.Species}\n`; heroInfoMessage += `Occupation: ${heroData.story_info_list.Occupation}\n`; heroInfoMessage += `Fields of Study: ${heroData.story_info_list["Fields of study"]}\n`; heroInfoMessage += `Abilities: ${heroData.story_info_list.Abilities}\n`; heroInfoMessage += `Website: ${heroData.story_info_list.Website}\n`; heroInfoMessage += `Traits: ${heroData.story_info_list.Traits}\n`; heroInfoMessage += `Likes: ${heroData.story_info_list.Likes}\n`; // Send hero information message with the attached image api.sendMessage({ body: heroInfoMessage, attachment: fs.createReadStream(imagePath) }, event.threadID, () => { // After sending message with attachment, delete the image file fs.unlink(imagePath, (err) => { if (err) { console.error("Error deleting image file:", err); } }); }); }); } catch (error) { console.error("Error fetching or processing hero data:", error); api.sendMessage("An error occurred while fetching hero data.", event.threadID); } } };