| const axios = require('axios'); | |
| module.exports.config = { | |
| name: "monitor", | |
| version: "1.0.0", | |
| hasPermission: 0, | |
| description: "Monitor website uptime, search for monitored URLs, and list all monitored URLs", | |
| usePrefix: true, | |
| credits: "Jonell Magallanes", | |
| cooldowns: 6, | |
| commandCategory: "System", | |
| }; | |
| module.exports.run = async function ({ api, event, args }) { | |
| const baseUrl = "https://ccprojectapis.ddns.net"; | |
| if (!args[0]) { | |
| return api.sendMessage("Please specify a command: `add`, `list`, or `search`.", event.threadID, event.messageID); | |
| } | |
| const wha = await api.sendMessage("Loading.....", event.threadID, event.messageID); | |
| const command = args[0]; | |
| const urlOrSearch = args[1]; | |
| if (command === 'add') { | |
| if (!urlOrSearch) { | |
| return api.sendMessage("Please provide a URL to add to the monitor list.", event.threadID, event.messageID); | |
| } | |
| try { | |
| const response = await axios.get(`${baseUrl}/uptime?url=${encodeURIComponent(urlOrSearch)}`); | |
| const data = response.data; | |
| if (data.message === "Website added successfully") { | |
| api.editMessage(`๐ ๐ผ๐ป๐ถ๐๐ผ๐ฟ๐ฒ๐ฑ ๐จ๐ฟ๐น ๐๐ฑ๐ฑ๐ฒ๐ฑ\nโโโโโโโโโโโโโโโโโโ\nMonitor added successfully for URL: ${data.url}`, wha.messageID, event.threadID, event.messageID); | |
| } else if (data.message === "URL is already in the list") { | |
| api.editMessage(`๐๐น๐ฟ๐ฒ๐ฎ๐ฑ๐ ๐จ๐ฝ๐๐ถ๐บ๐ฒ๐ฑ ๐จ๐ฅ๐\nโโโโโโโโโโโโโโโโโโ\nURL is already in the database: ${data.url}`, wha.messageID, event.threadID, event.messageID); | |
| } else { | |
| api.editMessage("Unknown response from the server.", wha.messageID, event.threadID, event.messageID); | |
| } | |
| } catch (error) { | |
| api.sendMessage("Error adding monitor.", event.threadID, event.messageID); | |
| } | |
| } else if (command === 'search') { | |
| if (!urlOrSearch) { | |
| return api.sendMessage("Please provide a URL to search for in the monitor list.", event.threadID, event.messageID); | |
| } | |
| try { | |
| const response = await axios.get(`${baseUrl}/search?url=${encodeURIComponent(urlOrSearch)}`); | |
| const data = response.data; | |
| if (data.length === 0) { | |
| return api.editMessage(`No results found for: ${urlOrSearch}`, wha.messageID, event.threadID, event.messageID); | |
| } | |
| let message = `๐ฆ๐ฒ๐ฎ๐ฟ๐ฐ๐ต ๐จ๐ฟ๐น ๐ ๐ผ๐ป๐ถ๐๐ผ๐ฟ๐ฒ๐ฑ\nโโโโโโโโโโโโโโโโโโ\n๐ Search:${urlOrSearch}\n`; | |
| data.forEach(item => { | |
| const status = interpretStatus(item.status); | |
| message += `๐ URL: ${item.url}\n๐ Status: ${status}\nDuration: ${item.duration}ms\nโฑ๏ธLast Checked: ${new Date(item.lastChecked)}\nโโโโโโโโโโโโโโโโโโ\n`; | |
| }); | |
| api.editMessage(message, wha.messageID, event.threadID, event.messageID); | |
| } catch (error) { | |
| api.sendMessage("Error searching for the URL.", event.threadID, event.messageID); | |
| } | |
| } else if (command === 'list') { | |
| try { | |
| const response = await axios.get(`${baseUrl}/list`); | |
| const data = response.data; | |
| let message = "๐ ๐ผ๐ป๐ถ๐๐ผ๐ฟ ๐จ๐ฝ๐๐ถ๐บ๐ฒ๐ฑ ๐๐ถ๐๐\nโโโโโโโโโโโโโโโโโโ\n"; | |
| data.forEach(item => { | |
| const status = interpretStatus(item.status); | |
| message += `๐ URL: ${item.url}\n๐ Status: ${status}\nDuration: ${item.duration}ms\nโฑ๏ธ Last Checked: ${new Date(item.lastChecked)}\nโโโโโโโโโโโโโโโโโโ\n`; | |
| }); | |
| api.editMessage(message, wha.messageID, event.threadID, event.messageID); | |
| } catch (error) { | |
| api.sendMessage("Error fetching monitor list.", event.threadID, event.messageID); | |
| } | |
| } else { | |
| api.sendMessage("Invalid command. Use `add`, `list`, or `search`.", event.threadID, event.messageID); | |
| } | |
| }; | |
| function interpretStatus(statusEmoji) { | |
| switch (statusEmoji) { | |
| case '๐ต': | |
| return "Up (200 OK)"; | |
| case 'โซ': | |
| return "Forbidden or Bad Gateway"; | |
| case '๐ด': | |
| return "Down"; | |
| default: | |
| return "Unknown Status"; | |
| } | |
| } | |