|
| 1 | +import { Message } from "whatsapp-web.js"; |
| 2 | +import axios from "axios"; |
| 3 | +import log from "../components/utils/log"; |
| 4 | + |
| 5 | +export const info = { |
| 6 | + command: "docker", |
| 7 | + description: "Search for Docker repositories or users.", |
| 8 | + usage: "docker <username> | <username/repository>", |
| 9 | + example: "docker mrepol742/project-canis", |
| 10 | + role: "user", |
| 11 | + cooldown: 5000, |
| 12 | +}; |
| 13 | + |
| 14 | +export default async function (msg: Message) { |
| 15 | + const query = msg.body.replace(/^docker\b\s*/i, "").trim(); |
| 16 | + if (query.length === 0) { |
| 17 | + await msg.reply("Please provide a search query."); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + if (query.includes(" ")) { |
| 22 | + await msg.reply( |
| 23 | + "Please provide a username or single repository name without spaces." |
| 24 | + ); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + await axios |
| 29 | + .get(`https://hub.docker.com/v2/repositories/${query}`) |
| 30 | + .then(async (response) => { |
| 31 | + if (response.data.count === 0 || response.data.message) { |
| 32 | + await msg.reply(`No repositories found for "${query}".`); |
| 33 | + return; |
| 34 | + } |
| 35 | + if (!query.includes("/")) { |
| 36 | + // The response is a list of repositories for the user |
| 37 | + const repos = response.data.results; |
| 38 | + if (!repos || repos.length === 0) { |
| 39 | + await msg.reply(`No repositories found for user "${query}".`); |
| 40 | + return; |
| 41 | + } |
| 42 | + let reply = `*${query}*\n\n`; |
| 43 | + reply += repos |
| 44 | + .map( |
| 45 | + (repo: any) => |
| 46 | + `*${repo.name}* |
| 47 | + }\nStars: ${repo.star_count} Pulls: ${repo.pull_count}` |
| 48 | + ) |
| 49 | + .join("\n\n"); |
| 50 | + await msg.reply(reply); |
| 51 | + return; |
| 52 | + } |
| 53 | + const repo = response.data; |
| 54 | + const info = ` |
| 55 | + *${repo.name}* |
| 56 | + ${repo.description || ""} |
| 57 | +
|
| 58 | + - Stars: ${repo.star_count} |
| 59 | + - Pulls: ${repo.pull_count} |
| 60 | + - Last Updated: ${new Date(repo.last_updated).toLocaleDateString()} |
| 61 | + - Link: https://hub.docker.com/r/${repo.namespace}/${repo.name} |
| 62 | + `; |
| 63 | + await msg.reply(info); |
| 64 | + }) |
| 65 | + .catch(async (error) => { |
| 66 | + log.error("docker", `Error fetching data: ${error.message}`); |
| 67 | + await msg.reply( |
| 68 | + `Error fetching data for "${query}". Please try again later.` |
| 69 | + ); |
| 70 | + return; |
| 71 | + }); |
| 72 | +} |
0 commit comments