ref: create separated verifyUserInput function

This commit is contained in:
GiovaniFZ 2025-03-09 12:46:47 -03:00
parent 4c02ebc265
commit 9535c69dbc
No known key found for this signature in database
GPG key ID: 63DD92181B575322
4 changed files with 30 additions and 41 deletions

View file

@ -3,6 +3,7 @@ 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');
const { verifyInput } = require('../plugins/verifyInput.js');
async function getDeviceList() {
try {
@ -23,12 +24,10 @@ module.exports = (bot) => {
bot.command(['codename', 'whatis'], spamwatchMiddleware, async (ctx) => {
const userInput = ctx.message.text.split(" ").slice(1).join(" ");
const Strings = getStrings(ctx.from.language_code);
if (!userInput) {
ctx.reply(Strings.codenameCheck.noCodename, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
});
const { noCodename } = Strings.codenameCheck
if(verifyInput(ctx, userInput, noCodename)){
return;
}
const jsonRes = await getDeviceList()

View file

@ -3,19 +3,18 @@ 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');
const { verifyInput } = require('../plugins/verifyInput.js');
module.exports = (bot) => {
bot.command("http", spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const userInput = ctx.message.text.split(' ')[1];
const apiUrl = Resources.httpApi;
const { invalidCode } = Strings.httpCodes
if (!userInput || isNaN(userInput)) {
return ctx.reply(Strings.httpCodes.invalidCode, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
};
if (verifyInput(ctx, userInput, invalidCode, true)) {
return;
}
try {
const response = await axios.get(apiUrl);
@ -50,12 +49,10 @@ module.exports = (bot) => {
bot.command("httpcat", spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(/\s+/g, '');
if (!userInput || isNaN(userInput)) {
return ctx.reply(Strings.httpCodes.invalidCode, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
const { invalidCode } = Strings.httpCodes
if (verifyInput(ctx, userInput, invalidCode, true)) {
return;
}
const apiUrl = `${Resources.httpCatApi}${userInput}`;

View file

@ -3,6 +3,7 @@ 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");
const { verifyInput } = require('../plugins/verifyInput.js');
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
@ -22,14 +23,11 @@ module.exports = (bot) => {
bot.command("mlpchar", spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+");
const { noCharName } = Strings.ponyApi
if (!userInput) {
ctx.reply(Strings.ponyApi.noCharName, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
if (verifyInput(ctx, userInput, noCharName)) {
return;
};
}
const capitalizedInput = capitalizeFirstLetter(userInput);
const apiUrl = `${Resources.ponyApi}/character/${capitalizedInput}`;
@ -98,13 +96,11 @@ module.exports = (bot) => {
const Strings = getStrings(ctx.from.language_code);
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+");
if (!userInput) {
ctx.reply(Strings.ponyApi.noEpisodeNum, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
const { noEpisodeNum } = Strings.ponyApi
if (verifyInput(ctx, userInput, noEpisodeNum, true)) {
return;
};
}
const apiUrl = `${Resources.ponyApi}/episode/by-overall/${userInput}`;
@ -167,11 +163,9 @@ module.exports = (bot) => {
const Strings = getStrings(ctx.from.language_code);
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+");
if (!userInput) {
ctx.reply(Strings.ponyApi.noComicName, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
const { noComicName } = Strings.ponyApi
if (verifyInput(ctx, userInput, noComicName)) {
return;
};

View file

@ -6,6 +6,7 @@ const Resources = require('../props/resources.json');
const axios = require('axios');
const { getStrings } = require('../plugins/checklang.js');
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
const { verifyInput } = require('../plugins/verifyInput.js');
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
const statusEmojis = {
@ -35,12 +36,10 @@ module.exports = (bot) => {
const userLang = ctx.from.language_code || "en-US";
const Strings = getStrings(userLang);
const userInput = ctx.message.text.split(' ').slice(1).join(' ');
const { provideLocation } = Strings.weatherStatus
if (!userInput) {
return ctx.reply(Strings.weatherStatus.provideLocation, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
});
if (verifyInput(ctx, userInput, provideLocation)) {
return;
}
const location = userInput;
@ -95,7 +94,7 @@ module.exports = (bot) => {
.replace('{windSpeed}', windSpeed)
.replace('{speedUnit}', speedUnit);
ctx.reply(weatherMessage, {
ctx.reply(weatherMessage, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
});