From def52e9566414aa7f0729dc13c0a01123cdbc4cd Mon Sep 17 00:00:00 2001
From: Giovani Finazzi <53719063+GiovaniFZ@users.noreply.github.com>
Date: Thu, 23 Jan 2025 21:14:44 -0300
Subject: [PATCH] feat: Search for a phone with codename (#31)
---
src/commands/codename.js | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
create mode 100644 src/commands/codename.js
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