Advertisement
samiroexpikachu

Mangadex

Mar 8th, 2024 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2.  
  3.  
  4.  let author1 = "Kim, ";
  5. let author2 = "Gabriel De, ";
  6. let author3 = "Samir Œ";
  7.  
  8. module.exports = {
  9.   config: {
  10.     name: "mangadex",
  11.     aliases: ["mangadex"],
  12.     version: "1.0",
  13.     author: author1 + author2 + author3,
  14.     countDown: 5,
  15.     role: 0,
  16.      longDescription: {
  17.       vi: '',
  18.       en: "Read Manga. this command is created by kim api source code by Gabriel De api is hosted by Samir Œ"
  19.     },
  20.     category: "anime",
  21.     guide: {
  22.       vi: '',
  23.       en: "{pn} <content>"
  24.     }
  25.   },
  26.  
  27.  
  28.  
  29.   onStart: async function ({ api, commandName, event }) {
  30.     return api.sendMessage("Search Manga\n--------------------------\n(Reply to this message)", event.threadID, (error, message) => {
  31.       global.GoatBot.onReply.set(message.messageID, {
  32.         commandName: commandName,
  33.         author: event.senderID,
  34.         messageID: message.messageID,
  35.         type: "search",
  36.         pagetype: false,
  37.         page: 1,
  38.         searchStatus: true
  39.       });
  40.     }, event.messageID);
  41.   },
  42.  
  43.   onReply: async function ({ Reply, api, event, args }) {
  44.     try {
  45.       const { commandName, author, messageID, type } = Reply;
  46.       if (event.senderID != author) {
  47.         return;
  48.       }
  49.       if (type == "search") {
  50.         let currentPage = Reply.page;
  51.         if (Reply.pagetype == true) {
  52.           if (args[0]?.toLowerCase() === "page" && args[1] > 0) {
  53.             currentPage = args[1];
  54.           } else if (args[0]?.toLowerCase() === "select" && args[1] > 0) {
  55.             const index = args[1] - 1;
  56.             const selectedData = Reply.currentPageData[index];
  57.             if (selectedData) {
  58.               api.setMessageReaction('⏳', event.messageID, () => {}, true);
  59.               const response = await axios.get('https://mangadex.onrender.com/manga/mangadex/info/' + selectedData.ID);
  60.               const mangaInfo = response.data;
  61.               const description = "Title: " + mangaInfo.title + "\n\nDescription: " + mangaInfo.description.en + "\n\nGenres: " + mangaInfo.genres.join(", ") + "\nThemes: " + mangaInfo.themes.join(", ") + "\nStatus: " + mangaInfo.status + "\nRelease Date: " + mangaInfo.releaseDate + "\nChapters: " + mangaInfo.chapters.length + "\n\n(Reply to this message the chapter you want to read. Ex: Read/Chapter 2/Done)";
  62.               const imageStream = await global.utils.getStreamFromURL(mangaInfo.image);
  63.               return api.sendMessage({ body: description, attachment: imageStream }, event.threadID, (error, message) => {
  64.                 api.setMessageReaction('', event.messageID, () => {}, true);
  65.                 global.GoatBot.onReply.set(message.messageID, {
  66.                   commandName: commandName,
  67.                   author: author,
  68.                   messageID: message.messageID,
  69.                   type: "read",
  70.                   mangaInfo: mangaInfo,
  71.                   option: false
  72.                 });
  73.               }, event.messageID);
  74.             } else {
  75.               return api.sendMessage("Invalid item number⚠️", event.threadID, event.messageID);
  76.             }
  77.           } else {
  78.             return args[0]?.toLowerCase() == "done" ? api.unsendMessage(messageID) && api.setMessageReaction('✅', event.messageID, () => {}, true) : api.sendMessage("Invalid input!⚠️\nEx: Page 2/Select 2/Done", event.threadID, event.messageID);
  79.           }
  80.         }
  81.  
  82.         let searchData = [];
  83.         let resultData = searchData;
  84.         if (Reply.searchStatus == true) {
  85.           const search = event.body;
  86.           const cleanSearch = search.replace(/[\/\\:]/g, '');
  87.           api.setMessageReaction('⏳', event.messageID, () => {}, true);
  88.           const searchResult = await axios.get('https://mangadex.onrender.com/manga/mangadex/' + cleanSearch);
  89.           const results = searchResult.data.results;
  90.           if (!results.length) {
  91.             return api.sendMessage("No results found!", event.threadID, () => {
  92.               api.setMessageReaction('⚠️', event.messageID, () => {}, true);
  93.             }, event.messageID);
  94.           }
  95.           results.forEach(item => {
  96.             searchData.push({
  97.               ID: item.id,
  98.               description: "Title: " + item.title + "\nDescription: " + item.description + "\nStatus: " + item.status + "\nRelease Date: " + item.releaseDate + "\nContent Rating: " + item.contentRating + "\nLast Volume: " + item.lastVolume + "\nLast Chapter: " + item.lastChapter + "\n\n"
  99.             });
  100.           });
  101.         } else {
  102.           searchData = Reply.resultString;
  103.           resultData = Reply.resultString;
  104.         }
  105.         const totalPages = Math.ceil(resultData.length / 5);
  106.         let resultPage = '';
  107.         let selectedData;
  108.         if (currentPage < 1 || currentPage > totalPages) {
  109.           return api.sendMessage("Page " + currentPage + " does not exist.\nTotal pages: " + totalPages, event.threadID, event.messageID);
  110.         } else {
  111.           selectedData = await paginate(resultData, currentPage, 5);
  112.           selectedData.forEach((data, index) => {
  113.             resultPage += index + 1 + ". " + data.description + "\n";
  114.           });
  115.         }
  116.         await api.unsendMessage(messageID);
  117.         return api.sendMessage("Results:\n--------------------------\n" + resultPage + "Current page " + currentPage + " of " + totalPages + " page/s.\n(Reply to this message. Ex: Page 2/Select 2/Done)", event.threadID, (error, message) => {
  118.           global.GoatBot.onReply.set(message.messageID, {
  119.             commandName: commandName,
  120.             author: author,
  121.             messageID: message.messageID,
  122.             resultString: searchData,
  123.             type: 'search',
  124.             pagetype: true,
  125.             page: currentPage,
  126.             searchStatus: false,
  127.             currentPageData: selectedData
  128.           });
  129.           api.setMessageReaction('', event.messageID, () => {}, true);
  130.         }, event.messageID);
  131.       } else {
  132.         if (type == 'read') {
  133.           let selectedChapter;
  134.           if (Reply.option == false) {
  135.             if (args[0]?.toLowerCase() == "chapter" && args[1] > 0 && Reply.mangaInfo.chapters.length > args[1] - 1) {
  136.               selectedChapter = args[1] - 1;
  137.             } else if (args[0]?.toLowerCase() == 'done') {
  138.               return api.unsendMessage(messageID) && api.setMessageReaction('✅', event.messageID, () => {}, true);
  139.             } else if (args[0]?.toLowerCase() == "read" && Reply.mangaInfo.chapters.length > 0) {
  140.               selectedChapter = 0;
  141.             } else {
  142.               return api.sendMessage("Invalid chapter!⚠️\nEx: Chapter 2/Read/Done", event.threadID, event.messageID);
  143.             }
  144.           } else {
  145.             if (args[0]?.toLowerCase() == "next" && Reply.mangaInfo.chapters.length > Reply.position + 1) {
  146.               selectedChapter = Reply.position + 1;
  147.             } else if (args[0]?.toLowerCase() == "prev" && Reply.position > 0) {
  148.               selectedChapter = Reply.position - 1;
  149.             } else if (args[0]?.toLowerCase() === "chapter" && args[1] > 0 && Reply.mangaInfo.chapters.length > args[1] - 1) {
  150.               selectedChapter = args[1] - 1;
  151.             } else {
  152.               return args[0]?.toLowerCase() == 'done' ? api.unsendMessage(messageID) && api.setMessageReaction('✅', event.messageID, () => {}, true) : api.sendMessage("No chapter available. Ex: Chapter 2/Next/Prev/Done", event.threadID, event.messageID);
  153.             }
  154.           }
  155.           const chapters = Reply.mangaInfo.chapters;
  156.           const reversedChapters = [...chapters].reverse();
  157.           const chapterData = reversedChapters[selectedChapter];
  158.           api.setMessageReaction('⏳', event.messageID, async () => {
  159.             try {
  160.               const response = await axios.get("https://mangadex.onrender.com/manga/mangadex/read/" + chapterData.id);
  161.               const images = response.data.map(item => item.img);
  162.               const imageStreams = await Promise.all(images.map(url => global.utils.getStreamFromURL(url)));
  163.               let chapterInfo = "Title: " + chapterData.title + "\nChapter: " + chapterData.chapterNumber;
  164.               for (let i = 0; i < imageStreams.length; i += 30) {
  165.                 const batchImages = imageStreams.slice(i, i + 30);
  166.                 const messageBody = {
  167.                   body: chapterInfo,
  168.                   attachment: batchImages
  169.                 };
  170.                 const sentMessage = await api.sendMessage(messageBody, event.threadID);
  171.                 global.GoatBot.onReply.set(sentMessage.messageID, {
  172.                   commandName: commandName,
  173.                   author: author,
  174.                   messageID: sentMessage.messageID,
  175.                   type: "read",
  176.                   position: selectedChapter,
  177.                   mangaInfo: Reply.mangaInfo,
  178.                   option: true
  179.                 });
  180.                 chapterInfo = '';
  181.               }
  182.               await api.setMessageReaction('', event.messageID, () => {}, true);
  183.             } catch (error) {
  184.               return api.sendMessage("Something went wrong", event.threadID, event.messageID) && api.setMessageReaction('⚠️', event.messageID, () => {}, true);
  185.             }
  186.           }, true);
  187.         }
  188.       }
  189.     } catch (error) {
  190.       return api.sendMessage("Error: " + error, event.threadID, event.messageID) && api.setMessageReaction('⚠️', event.messageID, () => {}, true);
  191.     }
  192.   }
  193. };
  194. //Don't remove orginal credit in the name of I don't know author name
  195.  
  196. async function paginate(data, currentPage, perPage) {
  197.   const startIndex = (currentPage - 1) * perPage;
  198.   const endIndex = startIndex + perPage;
  199.   return data.slice(startIndex, endIndex);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement