Remake of codename check

This commit is contained in:
Luquinhas 2025-01-23 22:09:05 -03:00
parent 552970e7aa
commit 09ddd96572
No known key found for this signature in database
GPG key ID: D9B075FC6DC93985
3 changed files with 61 additions and 26 deletions

View file

@ -1,36 +1,57 @@
const { getStrings } = require('../plugins/checklang.js');
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;
}
async function getDeviceList() {
try {
const response = await axios.get('https://raw.githubusercontent.com/androidtrackers/certified-android-devices/master/by_device.json');
return response.data
} catch (error) {
const message = Strings.codenameCheck.apiErr
.replace('{error}', error.message);
return ctx.reply(message, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
});
}
}
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)
bot.command(['codename', 'whatis'], spamwatchMiddleware, async (ctx) => {
const userInput = ctx.message.text.split(" ").slice(1).join(" ");
const Strings = getStrings(ctx.from.language_code);
if(!foundPhone){
return ctx.reply("No phones were found, please try another codename!")
}
if (!userInput) {
ctx.reply(Strings.codenameCheck.noCodename, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
});
}
const {brand, codename, name} = foundPhone;
const message = `<b>Brand:</b> <code>${brand}</code>\n<b>Codename:</b> <code>${codename}</code>\n<b>Name:</b> <code>${name}</code>`
const jsonRes = await getDeviceList()
const phoneSearch = Object.keys(jsonRes).find((codename) => codename === userInput);
return ctx.reply(message, { reply_to_message_id: ctx.message.message_id, parse_mode: 'HTML' });
})
if (!phoneSearch) {
return ctx.reply(Strings.codenameCheck.notFound, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
});
}
const deviceDetails = jsonRes[phoneSearch];
const device = deviceDetails.find((item) => item.brand) || deviceDetails[0];
const { brand = "Unknown", name = "Unknown", model = "Unknown" } = device;
const message = Strings.codenameCheck.resultMsg
.replace('{brand}', device.brand)
.replace('{codename}', userInput)
.replace('{model}', device.model)
.replace('{name}', device.name);
console.log(message)
return ctx.reply(message, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
})
}