diff --git a/src/commands/codename.js b/src/commands/codename.js new file mode 100644 index 0000000..c75dcaf --- /dev/null +++ b/src/commands/codename.js @@ -0,0 +1,36 @@ +const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js'); +const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch); +const axios = require('axios'); + +async function searchCodename() { + try { + const url = 'https://raw.githubusercontent.com/Hycon-Devices/official_devices/refs/heads/master/devices.json' + const response = await axios.get(url); + return response.data + } catch(error){ + console.error("Error fetching:", error); + return error; + } +} + +module.exports = (bot) => { + bot.command(['codename'], spamwatchMiddleware, async (ctx) => { + const typedCodename = ctx.message.text.split(" ").slice(1).join(" "); + + if (!typedCodename) { + return ctx.reply("Please provide a codename.", { reply_to_message_id: ctx.message.message_id }); + } + + const requestedPhones = await searchCodename(typedCodename); + const foundPhone = requestedPhones.find((element) => element.codename === typedCodename) + + if(!foundPhone){ + return ctx.reply("No phones were found, please try another codename!") + } + + const {brand, codename, name} = foundPhone; + const message = `Brand: ${brand}\nCodename: ${codename}\nName: ${name}` + + return ctx.reply(message, { reply_to_message_id: ctx.message.message_id, parse_mode: 'HTML' }); + }) +} \ No newline at end of file