From 87c987c16d3798f46014d312f091c3f76d0c062d Mon Sep 17 00:00:00 2001 From: Giovani Finazzi <53719063+GiovaniFZ@users.noreply.github.com> Date: Fri, 2 May 2025 21:08:13 -0300 Subject: [PATCH] ref: replace "ctx.from.language_code" with a function to get the language code and fix ts implementation for "reply_to_message_id" (#51) Co-authored-by: Lucas Gabriel --- src/commands/animal.ts | 120 +++++++++++++++---------------- src/commands/codename.ts | 31 ++++---- src/commands/crew.ts | 17 ++--- src/commands/fun.ts | 9 +-- src/commands/gsmarena.ts | 2 +- src/commands/help.ts | 7 +- src/commands/http.ts | 22 +++--- src/commands/main.ts | 12 ++-- src/commands/modarchive.ts | 16 +++-- src/commands/ponyapi.ts | 58 +++++++-------- src/commands/randompony.ts | 11 +-- src/commands/weather.ts | 12 ++-- src/commands/wiki.ts | 7 +- src/plugins/checklang.ts | 5 +- src/plugins/verifyInput.ts | 10 ++- src/utils/language-code.ts | 9 +++ src/utils/reply-to-message-id.ts | 5 ++ 17 files changed, 193 insertions(+), 160 deletions(-) create mode 100644 src/utils/language-code.ts create mode 100644 src/utils/reply-to-message-id.ts diff --git a/src/commands/animal.ts b/src/commands/animal.ts index 89eecba..2b8352e 100644 --- a/src/commands/animal.ts +++ b/src/commands/animal.ts @@ -4,135 +4,129 @@ import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import axios from 'axios'; import { Context, Telegraf } from 'telegraf'; +import { replyToMessageId } from '../utils/reply-to-message-id'; +import { languageCode } from '../utils/language-code'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); export default (bot: Telegraf) => { bot.command("duck", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const reply_to_message_id = replyToMessageId(ctx); try { const response = await axios(Resources.duckApi); ctx.replyWithPhoto(response.data.url, { caption: "🦆", - // reply_to_message_id works fine, using this for now to avoid errors - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { + const Strings = getStrings(languageCode(ctx)); const message = Strings.duckApiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); return; } }); bot.command("fox", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); + const reply_to_message_id = replyToMessageId(ctx); try { const response = await axios(Resources.foxApi); ctx.replyWithPhoto(response.data.image, { caption: "🦊", - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { const message = Strings.foxApiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); return; } }); bot.command("dog", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); + const reply_to_message_id = replyToMessageId(ctx); try { const response = await axios(Resources.dogApi); ctx.replyWithPhoto(response.data.message, { caption: "🐶", - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { const message = Strings.foxApiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); return; } }); bot.command("cat", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const apiUrl = `${Resources.catApi}?json=true`; const response = await axios.get(apiUrl); const data = response.data; const imageUrl = `${data.url}`; + const reply_to_message_id = replyToMessageId(ctx); try { await ctx.replyWithPhoto(imageUrl, { caption: `🐱`, parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { ctx.reply(Strings.catImgErr, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }; }); bot.command(['soggy', 'soggycat'], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const userInput = ctx.message.text.split(' ')[1]; - - switch (true) { - case (userInput === "2" || userInput === "thumb"): - ctx.replyWithPhoto( - Resources.soggyCat2, { - caption: Resources.soggyCat2, - parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id - }); - break; - - case (userInput === "3" || userInput === "sticker"): - ctx.replyWithSticker( - Resources.soggyCatSticker, { - // @ts-ignore - reply_to_message_id: ctx.message.message_id - }); - break; - - case (userInput === "4" || userInput === "alt"): - ctx.replyWithPhoto( - Resources.soggyCatAlt, { - caption: Resources.soggyCatAlt, - parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id - }); - break; - - default: - ctx.replyWithPhoto( - Resources.soggyCat, { - caption: Resources.soggyCat, - parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id - }); - break; - }; - }); + const userInput = ctx.message.text.split(' ')[1]; + const reply_to_message_id = replyToMessageId(ctx); + + switch (true) { + case (userInput === "2" || userInput === "thumb"): + ctx.replyWithPhoto( + Resources.soggyCat2, { + caption: Resources.soggyCat2, + parse_mode: 'Markdown', + ...({ reply_to_message_id }) + }); + break; + + case (userInput === "3" || userInput === "sticker"): + ctx.replyWithSticker( + Resources.soggyCatSticker, { + // ...({ reply_to_message_id }) // to-do: fix this + }); + break; + + case (userInput === "4" || userInput === "alt"): + ctx.replyWithPhoto( + Resources.soggyCatAlt, { + caption: Resources.soggyCatAlt, + parse_mode: 'Markdown', + ...({ reply_to_message_id }) + }); + break; + + default: + ctx.replyWithPhoto( + Resources.soggyCat, { + caption: Resources.soggyCat, + parse_mode: 'Markdown', + ...({ reply_to_message_id }) + }); + break; + }; + }); } \ No newline at end of file diff --git a/src/commands/codename.ts b/src/commands/codename.ts index 7f4a3e5..ba0e3b4 100644 --- a/src/commands/codename.ts +++ b/src/commands/codename.ts @@ -5,10 +5,19 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import axios from 'axios'; import verifyInput from '../plugins/verifyInput'; import { Context, Telegraf } from 'telegraf'; +import { languageCode } from '../utils/language-code'; +import { replyToMessageId } from '../utils/reply-to-message-id'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); +interface Device { + brand: string; + codename: string; + model: string; +} + async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & { message: { text: string } } }) { + const reply_to_message_id = replyToMessageId(ctx); try { const response = await axios.get(Resources.codenameApi); return response.data @@ -18,8 +27,7 @@ async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & { return ctx.reply(message, { parse_mode: "Markdown", - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } } @@ -27,10 +35,11 @@ async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & { export default (bot: Telegraf) => { bot.command(['codename', 'whatis'], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { const userInput = ctx.message.text.split(" ").slice(1).join(" "); - const Strings = getStrings(ctx.from?.language_code); - const { noCodename } = Strings.codenameCheck - - if(verifyInput(ctx, userInput, noCodename)){ + const Strings = getStrings(languageCode(ctx)); + const { noCodename } = Strings.codenameCheck; + const reply_to_message_id = replyToMessageId(ctx); + + if (verifyInput(ctx, userInput, noCodename)) { return; } @@ -40,13 +49,12 @@ export default (bot: Telegraf) => { if (!phoneSearch) { return ctx.reply(Strings.codenameCheck.notFound, { parse_mode: "Markdown", - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } - + const deviceDetails = jsonRes[phoneSearch]; - const device = deviceDetails.find((item) => item.brand) || deviceDetails[0]; + const device = deviceDetails.find((item: Device) => item.brand) || deviceDetails[0]; const message = Strings.codenameCheck.resultMsg .replace('{brand}', device.brand) .replace('{codename}', userInput) @@ -55,8 +63,7 @@ export default (bot: Telegraf) => { return ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }) } \ No newline at end of file diff --git a/src/commands/crew.ts b/src/commands/crew.ts index c55b774..0614441 100644 --- a/src/commands/crew.ts +++ b/src/commands/crew.ts @@ -5,6 +5,7 @@ import os from 'os'; import { exec } from 'child_process'; import { error } from 'console'; import { Context, Telegraf } from 'telegraf'; +import { languageCode } from '../utils/language-code'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); @@ -54,7 +55,7 @@ function getSystemInfo() { } async function handleAdminCommand(ctx: Context & { message: { text: string } }, action: () => Promise, successMessage: string, errorMessage: string) { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const userId = ctx.from?.id; const adminArray = process.env.botAdmins ? process.env.botAdmins.split(',').map(id => parseInt(id.trim())) : []; if (userId && adminArray.includes(userId)) { @@ -84,7 +85,7 @@ async function handleAdminCommand(ctx: Context & { message: { text: string } }, export default (bot: Telegraf) => { bot.command('getbotstats', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); handleAdminCommand(ctx, async () => { const stats = getSystemInfo(); await ctx.reply(stats, { @@ -96,7 +97,7 @@ export default (bot: Telegraf) => { }); bot.command('getbotcommit', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); handleAdminCommand(ctx, async () => { try { const commitHash = await getGitCommitHash(); @@ -116,7 +117,7 @@ export default (bot: Telegraf) => { }); bot.command('updatebot', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); handleAdminCommand(ctx, async () => { try { const result = await updateBot(); @@ -136,7 +137,7 @@ export default (bot: Telegraf) => { }); bot.command('setbotname', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const botName = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { await ctx.telegram.setMyName(botName); @@ -144,7 +145,7 @@ export default (bot: Telegraf) => { }); bot.command('setbotdesc', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const botDesc = ctx.message.text.split(' ').slice(1).join(' '); handleAdminCommand(ctx, async () => { await ctx.telegram.setMyDescription(botDesc); @@ -152,7 +153,7 @@ export default (bot: Telegraf) => { }); bot.command('botkickme', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); handleAdminCommand(ctx, async () => { if (!ctx.chat) { ctx.reply(Strings.chatNotFound, { @@ -172,7 +173,7 @@ export default (bot: Telegraf) => { }); bot.command('getfile', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const botFile = ctx.message.text.split(' ').slice(1).join(' '); if (!botFile) { diff --git a/src/commands/fun.ts b/src/commands/fun.ts index b1437bf..c18d460 100644 --- a/src/commands/fun.ts +++ b/src/commands/fun.ts @@ -3,11 +3,12 @@ import { getStrings } from '../plugins/checklang'; import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import { Context, Telegraf } from 'telegraf'; +import { languageCode } from '../utils/language-code'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); function sendRandomReply(ctx: Context & { message: { text: string } }, gifUrl: string, textKey: string) { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const randomNumber = Math.floor(Math.random() * 100); const shouldSendGif = randomNumber > 50; @@ -38,7 +39,7 @@ function sendRandomReply(ctx: Context & { message: { text: string } }, gifUrl: s async function handleDiceCommand(ctx: Context & { message: { text: string } }, emoji: string, delay: number) { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); // @ts-ignore const result = await ctx.sendDice({ emoji, reply_to_message_id: ctx.message.message_id }); @@ -55,13 +56,13 @@ async function handleDiceCommand(ctx: Context & { message: { text: string } }, e }, delay); } -function getRandomInt(max) { +function getRandomInt(max: number) { return Math.floor(Math.random() * (max + 1)); } export default (bot: Telegraf) => { bot.command('random', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code); + const Strings = getStrings(languageCode(ctx)); const randomValue = getRandomInt(11); const randomVStr = Strings.randomNum.replace('{number}', randomValue); diff --git a/src/commands/gsmarena.ts b/src/commands/gsmarena.ts index 43a20ed..c44206a 100644 --- a/src/commands/gsmarena.ts +++ b/src/commands/gsmarena.ts @@ -127,7 +127,7 @@ function formatPhone(phone: PhoneDetails) { return `\n\nName: ${formattedPhone.name}\n\n${attributes}\n\n${deviceImage}\n\n${deviceUrl}`; } -async function fetchHtml(url) { +async function fetchHtml(url: string) { try { const response = await axios.get(url, { headers: HEADERS }); return response.data; diff --git a/src/commands/help.ts b/src/commands/help.ts index 95ea804..39191c1 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -1,6 +1,7 @@ import { getStrings } from '../plugins/checklang'; import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; +import { languageCode } from '../utils/language-code'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); @@ -14,7 +15,7 @@ interface MessageOptions { } async function sendHelpMessage(ctx, isEditing) { - const Strings = getStrings(ctx.from.language_code); + const Strings = getStrings(languageCode(ctx)); const botInfo = await ctx.telegram.getMe(); const helpText = Strings.botHelp .replace(/{botName}/g, botInfo.first_name) @@ -56,7 +57,7 @@ export default (bot) => { }); bot.command("about", spamwatchMiddleware, async (ctx) => { - const Strings = getStrings(ctx.from.language_code); + const Strings = getStrings(languageCode(ctx)); const aboutMsg = Strings.botAbout.replace(/{sourceLink}/g, `${process.env.botSource}`); ctx.reply(aboutMsg, { parse_mode: 'Markdown', @@ -67,7 +68,7 @@ export default (bot) => { bot.on('callback_query', async (ctx) => { const callbackData = ctx.callbackQuery.data; - const Strings = getStrings(ctx.from.language_code); + const Strings = getStrings(languageCode(ctx)); const options = { parse_mode: 'Markdown', disable_web_page_preview: true, diff --git a/src/commands/http.ts b/src/commands/http.ts index 8dc6853..53c1359 100644 --- a/src/commands/http.ts +++ b/src/commands/http.ts @@ -5,12 +5,14 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import axios from 'axios'; import verifyInput from '../plugins/verifyInput'; import { Context, Telegraf } from 'telegraf'; +import { languageCode } from '../utils/language-code'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); export default (bot: Telegraf) => { bot.command("http", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const reply_to_message_id = ctx.message.message_id; + const Strings = getStrings(languageCode(ctx)); const userInput = ctx.message.text.split(' ')[1]; const apiUrl = Resources.httpApi; const { invalidCode } = Strings.httpCodes @@ -32,28 +34,26 @@ export default (bot: Telegraf) => { .replace("{description}", codeInfo.description); await ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } else { await ctx.reply(Strings.httpCodes.notFound, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }; } catch (error) { const message = Strings.httpCodes.fetchErr.replace("{error}", error); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }; }); bot.command("httpcat", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const Strings = getStrings(languageCode(ctx)); + const reply_to_message_id = ctx.message.message_id; const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(/\s+/g, ''); const { invalidCode } = Strings.httpCodes @@ -67,14 +67,12 @@ export default (bot: Telegraf) => { await ctx.replyWithPhoto(apiUrl, { caption: `🐱 ${apiUrl}`, parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { ctx.reply(Strings.catImgErr, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } }); diff --git a/src/commands/main.ts b/src/commands/main.ts index 2729173..a5d581c 100644 --- a/src/commands/main.ts +++ b/src/commands/main.ts @@ -1,18 +1,22 @@ import { getStrings } from '../plugins/checklang'; import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; +import { Context, Telegraf } from 'telegraf'; +import { replyToMessageId } from '../utils/reply-to-message-id'; +import { languageCode } from '../utils/language-code'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); -export default (bot: any) => { - bot.start(spamwatchMiddleware, async (ctx: any) => { - const Strings = getStrings(ctx.from.language_code); +export default (bot: Telegraf) => { + bot.start(spamwatchMiddleware, async (ctx: Context) => { + const Strings = getStrings(languageCode(ctx)); const botInfo = await ctx.telegram.getMe(); + const reply_to_message_id = replyToMessageId(ctx) const startMsg = Strings.botWelcome.replace(/{botName}/g, botInfo.first_name); ctx.reply(startMsg, { parse_mode: 'Markdown', - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }); diff --git a/src/commands/modarchive.ts b/src/commands/modarchive.ts index eac76a8..721a517 100644 --- a/src/commands/modarchive.ts +++ b/src/commands/modarchive.ts @@ -5,6 +5,9 @@ import path from 'path'; import { getStrings } from '../plugins/checklang'; import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; +import { languageCode } from '../utils/language-code'; +import { Context, Telegraf } from 'telegraf'; +import { replyToMessageId } from '../utils/reply-to-message-id'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); @@ -46,15 +49,16 @@ async function downloadModule(moduleId: string): Promise { } } -export default (bot) => { +export default (bot: Telegraf) => { bot.command(['modarchive', 'tma'], spamwatchMiddleware, async (ctx) => { - const Strings = getStrings(ctx.from.language_code); - const moduleId = ctx.message.text.split(' ')[1]; + const Strings = getStrings(languageCode(ctx)); + const reply_to_message_id = replyToMessageId(ctx); + const moduleId = ctx.message?.text.split(' ')[1]; if (Number.isNaN(moduleId) || null) { return ctx.reply(Strings.maInvalidModule, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } @@ -65,14 +69,14 @@ export default (bot) => { await ctx.replyWithDocument({ source: filePath }, { caption: fileName, - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); fs.unlinkSync(filePath); } else { ctx.reply(Strings.maDownloadError, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } }); diff --git a/src/commands/ponyapi.ts b/src/commands/ponyapi.ts index 25fc473..2202949 100644 --- a/src/commands/ponyapi.ts +++ b/src/commands/ponyapi.ts @@ -5,6 +5,8 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import axios from 'axios'; import verifyInput from '../plugins/verifyInput'; import { Telegraf, Context } from 'telegraf'; +import { languageCode } from '../utils/language-code'; +import { replyToMessageId } from '../utils/reply-to-message-id'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); @@ -47,24 +49,24 @@ interface Comic { editor: string; } -function capitalizeFirstLetter(string) { - return string.charAt(0).toUpperCase() + string.slice(1); +function capitalizeFirstLetter(letter: string) { + return letter.charAt(0).toUpperCase() + letter.slice(1); } export default (bot: Telegraf) => { bot.command("mlp", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const Strings = getStrings(languageCode(ctx)); + const reply_to_message_id = replyToMessageId(ctx); ctx.reply(Strings.ponyApi.helpDesc, { parse_mode: 'Markdown', - // @ts-ignore - disable_web_page_preview: true, - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id, disable_web_page_preview: true }) }); }); bot.command("mlpchar", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const reply_to_message_id = replyToMessageId(ctx); + const Strings = getStrings(languageCode(ctx) || 'en'); const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+"); const { noCharName } = Strings.ponyApi @@ -118,30 +120,27 @@ export default (bot: Telegraf) => { ctx.replyWithPhoto(charactersArray[0].image[0], { caption: `${result}`, parse_mode: 'Markdown', - // @ts-ignore - disable_web_page_preview: true, - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id, disable_web_page_preview: true }) }); } else { ctx.reply(Strings.ponyApi.noCharFound, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }; } catch (error) { const message = Strings.ponyApi.apiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); }; }); bot.command("mlpep", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const Strings = getStrings(languageCode(ctx) || 'en'); const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+"); + const reply_to_message_id = replyToMessageId(ctx); const { noEpisodeNum } = Strings.ponyApi @@ -156,7 +155,7 @@ export default (bot: Telegraf) => { const episodeArray: Episode[] = []; if (Array.isArray(response.data.data)) { - response.data.data.forEach(episode => { + response.data.data.forEach((episode: Episode) => { episodeArray.push({ id: episode.id, name: episode.name, @@ -189,30 +188,29 @@ export default (bot: Telegraf) => { ctx.replyWithPhoto(episodeArray[0].image, { caption: `${result}`, parse_mode: 'Markdown', - // @ts-ignore - disable_web_page_preview: true, - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id, disable_web_page_preview: true }) }); } else { ctx.reply(Strings.ponyApi.noEpisodeFound, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + + ...({ reply_to_message_id }) }); }; } catch (error) { const message = Strings.ponyApi.apiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + + ...({ reply_to_message_id }) }); }; }); bot.command("mlpcomic", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const Strings = getStrings(languageCode(ctx) || 'en'); const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+"); + const reply_to_message_id = replyToMessageId(ctx); const { noComicName } = Strings.ponyApi @@ -265,23 +263,21 @@ export default (bot: Telegraf) => { ctx.replyWithPhoto(comicArray[0].image, { caption: `${result}`, parse_mode: 'Markdown', - // @ts-ignore - disable_web_page_preview: true, - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id, disable_web_page_preview: true }) }); } else { ctx.reply(Strings.ponyApi.noComicFound, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + + ...({ reply_to_message_id }) }); }; } catch (error) { const message = Strings.ponyApi.apiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + + ...({ reply_to_message_id }) }); }; }); diff --git a/src/commands/randompony.ts b/src/commands/randompony.ts index 3ecf4b4..be91fcf 100644 --- a/src/commands/randompony.ts +++ b/src/commands/randompony.ts @@ -4,13 +4,16 @@ import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import axios from 'axios'; import { Telegraf, Context } from 'telegraf'; +import { languageCode } from '../utils/language-code'; +import { replyToMessageId } from '../utils/reply-to-message-id'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); export default (bot: Telegraf) => { // TODO: this would greatly benefit from a loading message bot.command(["rpony", "randompony", "mlpart"], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => { - const Strings = getStrings(ctx.from?.language_code || 'en'); + const Strings = getStrings(languageCode(ctx)); + const reply_to_message_id = replyToMessageId(ctx); try { const response = await axios(Resources.randomPonyApi); let tags: string[] = []; @@ -26,15 +29,13 @@ export default (bot: Telegraf) => { ctx.replyWithPhoto(response.data.pony.representations.full, { caption: `${response.data.pony.sourceURL}\n\n${tags.length > 0 ? tags.join(', ') : ''}`, parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { const message = Strings.ponyApi.apiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: 'Markdown', - // @ts-ignore - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); return; } diff --git a/src/commands/weather.ts b/src/commands/weather.ts index 6c1a529..f72c343 100644 --- a/src/commands/weather.ts +++ b/src/commands/weather.ts @@ -8,6 +8,7 @@ import { getStrings } from '../plugins/checklang'; import { isOnSpamWatch } from '../spamwatch/spamwatch'; import spamwatchMiddlewareModule from '../spamwatch/Middleware'; import verifyInput from '../plugins/verifyInput'; +import { Context, Telegraf } from 'telegraf'; const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch); @@ -33,8 +34,9 @@ function getLocaleUnit(countryCode: string) { } } -export default (bot) => { +export default (bot: Telegraf) => { bot.command(['clima', 'weather'], spamwatchMiddleware, async (ctx) => { + const reply_to_message_id = ctx.message.message_id; const userLang = ctx.from.language_code || "en-US"; const Strings = getStrings(userLang); const userInput = ctx.message.text.split(' ').slice(1).join(' '); @@ -50,7 +52,7 @@ export default (bot) => { if (!apiKey || apiKey === "InsertYourWeatherDotComApiKeyHere") { return ctx.reply(Strings.weatherStatus.apiKeyErr, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } @@ -69,7 +71,7 @@ export default (bot) => { if (!locationData || !locationData.address) { return ctx.reply(Strings.weatherStatus.invalidLocation, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } @@ -106,13 +108,13 @@ export default (bot) => { ctx.reply(weatherMessage, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } catch (error) { const message = Strings.weatherStatus.apiErr.replace('{error}', error.message); ctx.reply(message, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); } }); diff --git a/src/commands/wiki.ts b/src/commands/wiki.ts index f6ca6d0..bec7df1 100644 --- a/src/commands/wiki.ts +++ b/src/commands/wiki.ts @@ -1,5 +1,7 @@ /* import axios from "axios"; +import { Context, Telegraf } from "telegraf"; +import { replyToMessageId } from "../utils/reply-to-message-id"; function capitalizeFirstLetter(string: string) { return string.charAt(0).toUpperCase() + string.slice(1); @@ -23,7 +25,7 @@ function mediaWikiToMarkdown(input: string) { return input; } -export default (bot) => { +export default (bot: Telegraf) => { bot.command("wiki", async (ctx) => { const userInput = capitalizeFirstLetter(ctx.message.text.split(' ')[1]); const apiUrl = `https://en.wikipedia.org/w/index.php?title=${userInput}&action=raw`; @@ -31,8 +33,9 @@ export default (bot) => { const convertedResponse = response.data.replace(/<\/?div>/g, "").replace(/{{Infobox.*?}}/s, ""); const result = mediaWikiToMarkdown(convertedResponse).slice(0, 2048); + const reply_to_message_id = replyToMessageId(ctx); - ctx.reply(result, { parse_mode: 'Markdown', disable_web_page_preview: true, reply_to_message_id: ctx.message.message_id }); + ctx.reply(result, { parse_mode: 'Markdown', ...({ reply_to_message_id, disable_web_page_preview: true }) }); }); }; */ \ No newline at end of file diff --git a/src/plugins/checklang.ts b/src/plugins/checklang.ts index 650747c..a38445a 100644 --- a/src/plugins/checklang.ts +++ b/src/plugins/checklang.ts @@ -7,7 +7,10 @@ const languageFiles = { 'en-gb': '../locales/english.json' }; -function getStrings(languageCode: string) { +function getStrings(languageCode?: string) { + if (!languageCode) { + return require(languageFiles['en']); + } const filePath: string = languageFiles[languageCode] || languageFiles['en']; try { return require(filePath); diff --git a/src/plugins/verifyInput.ts b/src/plugins/verifyInput.ts index b1355fd..b0f27c0 100644 --- a/src/plugins/verifyInput.ts +++ b/src/plugins/verifyInput.ts @@ -1,8 +1,12 @@ -export default function verifyInput(ctx: any, userInput: string, message: string, verifyNaN = false) { - if (!userInput || (verifyNaN && isNaN(Number(userInput)))) { // not sure why isNaN is used here, but the input should be a number +import { Context } from "telegraf"; +import { replyToMessageId } from "../utils/reply-to-message-id"; + +export default function verifyInput(ctx: Context, userInput: string, message: string, verifyNaN = false) { + const reply_to_message_id = replyToMessageId(ctx); + if (!userInput || (verifyNaN && isNaN(Number(userInput)))) { ctx.reply(message, { parse_mode: "Markdown", - reply_to_message_id: ctx.message.message_id + ...({ reply_to_message_id }) }); return true; } diff --git a/src/utils/language-code.ts b/src/utils/language-code.ts new file mode 100644 index 0000000..027b85b --- /dev/null +++ b/src/utils/language-code.ts @@ -0,0 +1,9 @@ +import { Context } from "telegraf"; + +export const languageCode = (ctx: Context) => { + if(ctx.from) { + return ctx.from.language_code + } else { + return 'en' + } +} \ No newline at end of file diff --git a/src/utils/reply-to-message-id.ts b/src/utils/reply-to-message-id.ts new file mode 100644 index 0000000..7308ea0 --- /dev/null +++ b/src/utils/reply-to-message-id.ts @@ -0,0 +1,5 @@ +import { Context } from "telegraf" + +export const replyToMessageId = (ctx: Context) => { + return ctx.message?.message_id +} \ No newline at end of file