From a080ab608595af9f5d48ecf03ae006feae1fb1be Mon Sep 17 00:00:00 2001 From: Secven Date: Fri, 16 Jul 2021 08:59:34 +0000 Subject: [PATCH] make it better --- .gitignore | 1 + src/app.js | 2 +- src/config/config.js | 4 ++-- src/config/telegraf.js | 11 ++++------- src/handlers/jokeTask.js | 19 ++++--------------- src/handlers/langTask.js | 8 +++----- src/middle/report.js | 2 -- src/middle/updaterUser.js | 12 +++++------- src/route/eyeofgod.js | 6 +++--- src/util/service.js | 19 +++++++++++++++++-- test.js | 1 + 11 files changed, 41 insertions(+), 44 deletions(-) create mode 100644 test.js diff --git a/.gitignore b/.gitignore index 0165c61..bde76a3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ logs tests .DS_Store .tmp +test.js *.log npm-debug.log* yarn-debug.log* diff --git a/src/app.js b/src/app.js index eb8605f..10b1df4 100644 --- a/src/app.js +++ b/src/app.js @@ -65,7 +65,7 @@ class App { const handlerUpdater = ctx => { if (clusterNumber >= _workers.length) clusterNumber = 0 - const worker = _workers[Number(clusterNumber)] + const worker = _workers[+clusterNumber] if (worker) { clusterNumber += 1 worker.send(ctx.update) diff --git a/src/config/config.js b/src/config/config.js index 16f76dd..89159c6 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -14,7 +14,7 @@ config.consignOpts = { } config.limitSecurity = { - window: 1000 * 25, + window: 20e3, limit: 8, onLimitExceeded: ({ reply }) => reply('Please wait a second', { @@ -23,7 +23,7 @@ config.limitSecurity = { } config.cbQueryOpts = { - window: 1000 * 13, + window: 15e3, limit: 3, onLimitExceeded: ({ answerCbQuery }) => answerCbQuery('Please wait a second', true) diff --git a/src/config/telegraf.js b/src/config/telegraf.js index a47422f..a5b246c 100644 --- a/src/config/telegraf.js +++ b/src/config/telegraf.js @@ -24,12 +24,10 @@ const bot = new Telegraf(process.env.BOT_TOKEN, { const Route = new Router(({ callbackQuery }) => { if (!callbackQuery.data) return - const parts = callbackQuery.data.split('|') + const [route, value] = callbackQuery.data.split('|') return { - route: parts[0], - state: { - value: parts[1] - } + route, + state: { value } } }) @@ -59,9 +57,8 @@ bot.context = { Composer } -bot.use(Composer.mount('callback_query', rateLimit(config.cbQueryOpts))) -bot.use(Composer.mount('inline_query', rateLimit(config.cbQueryOpts))) bot.use(rateLimit(config.limitSecurity)) +bot.use(Composer.mount('callback_query', rateLimit(config.cbQueryOpts))) bot.use(session()) bot.use(new I18n(config.i18nOpts)) bot.use(updaterUser) diff --git a/src/handlers/jokeTask.js b/src/handlers/jokeTask.js index 92b860c..0a5f282 100644 --- a/src/handlers/jokeTask.js +++ b/src/handlers/jokeTask.js @@ -1,9 +1,9 @@ -const fetch = require('node-fetch') +const { SERVICE } = require('../util/service') class JokeTask { async getRandomAnimation({ Extra, replyWithAnimation }) { - const { videoURL: sendGif, description: caption } = await getRandomGif() - console.log(await getRandomGif()) + const { videoURL: sendGif, description: caption } = + await SERVICE.getJokeDev() return replyWithAnimation(sendGif, { caption, ...Extra.markup(m => @@ -22,7 +22,7 @@ class JokeTask { const moar = Extra.markup(m => m.inlineKeyboard([m.callbackButton('MOAR', 'joke|moar')]) ) - const { videoURL: media, description: caption } = await getRandomGif() + const { videoURL: media, description: caption } = await SERVICE.getJokeDev() return await editMessageMedia( { type: 'animation', @@ -33,15 +33,4 @@ class JokeTask { } } -const getRandomGif = async () => { - const rand = Math.floor(Math.random() * 100) - return await fetch(`https://developerslife.ru/top/${rand}?json=true`) - .then(res => res.json()) - .then(data => { - const index = Number(Math.floor(Math.random() * data.result.length)) - return data.result[Number(index)] - }) - .catch(err => console.error(err.message())) -} - module.exports = new JokeTask() diff --git a/src/handlers/langTask.js b/src/handlers/langTask.js index 9e80789..d9f3ba5 100644 --- a/src/handlers/langTask.js +++ b/src/handlers/langTask.js @@ -1,7 +1,7 @@ const { readdirSync, path } = require('../config/config') class LangTask { - updaterLang = async ({ i18n, Extra, markup, replyWithHTML }) => { + updaterLang = async ({ i18n, Extra, Markup, replyWithHTML }) => { const locales = {} const markupArr = [] const __PATH__ = path.resolve(__dirname, '../locales') @@ -13,10 +13,8 @@ class LangTask { Object.keys(locales).forEach(value => { let name = locales[value.toString()].name - if (value === i18n.languageCode) { - name = `✅ ${name}` - } - return markupArr.push(markup.callbackButton(name, `set_lang|${value}`)) + if (value === i18n.languageCode) name = `✅ ${name}` + return markupArr.push(Markup.callbackButton(name, `set_lang|${value}`)) }) return await replyWithHTML( diff --git a/src/middle/report.js b/src/middle/report.js index ab48188..5fca169 100644 --- a/src/middle/report.js +++ b/src/middle/report.js @@ -10,7 +10,6 @@ module.exports = async ({ telegram, updateType, i18n }, next) => { stack: escapeHtml(err.stack), botName: `@${username}` } - await telegram .sendMessage( process.env.ADMIN_ID, @@ -21,6 +20,5 @@ module.exports = async ({ telegram, updateType, i18n }, next) => { } ) .catch(noop) - console.log(__TEMPLATE__) }) } diff --git a/src/middle/updaterUser.js b/src/middle/updaterUser.js index ffe929f..7ccd338 100644 --- a/src/middle/updaterUser.js +++ b/src/middle/updaterUser.js @@ -1,22 +1,20 @@ const { escapeHtml } = require('../util/escapeHTML') module.exports = async ({ db, from, i18n, session }, next) => { - // eslint-disable-next-line camelcase - const { id, first_name, username } = from + const { id, first_name: firstName, username } = from if (!id) return await next() await db.User.findOne({ user_id: id }) .then(root => { if (!root) { root = new db.User() root.user_id = id - // eslint-disable-next-line camelcase - root.first_name = escapeHtml(first_name) ?? 'Unknown' + root.first_name = escapeHtml(firstName) ?? 'Unknown' root.username = username ?? 'Unknown' } - // eslint-disable-next-line camelcase - root.first_name = escapeHtml(first_name) ?? 'Unknown' + + root.first_name = escapeHtml(firstName) ?? 'Unknown' root.username = username - // eslint-disable-next-line camelcase + session.root = root i18n.locale(session.root.settings.lang) return next().then(async () => { diff --git a/src/route/eyeofgod.js b/src/route/eyeofgod.js index fa1e6f0..d7e978d 100644 --- a/src/route/eyeofgod.js +++ b/src/route/eyeofgod.js @@ -2,10 +2,10 @@ module.exports = bot => { const { eyeofgodTask } = bot.handlers const expect = bot.rateLimit({ - window: 1000 * 20, + window: 20e3, limit: 1, - onLimitExceeded: ({ reply }) => - reply( + onLimitExceeded: ({ replyWithHTML }) => + replyWithHTML( 'I can handle from you only one request in 15 seconds.', true, { disable_notification: true } diff --git a/src/util/service.js b/src/util/service.js index 831e9fe..5f5d2b2 100644 --- a/src/util/service.js +++ b/src/util/service.js @@ -1,5 +1,19 @@ const fetch = require('node-fetch') +const getJokeDev = async () => { + return Promise.resolve( + await fetch( + `https://developerslife.ru/top/${+~~(Math.random() * 10e1)}?json=true` + ) + ) + .then(res => res.json()) + .then(data => { + const index = +~~(Math.random() * data.result.length) + return data.result[+index] + }) + .catch(err => console.error(err.message())) +} + const getInfoOperator = async (num = '79521305638') => { // eslint-disable-next-line no-async-promise-executor return new Promise(async resolve => { @@ -13,7 +27,7 @@ const getInfoOperator = async (num = '79521305638') => { 'Content-Type': 'application/json' }, compress: true, - body: JSON.stringify([num]) + body: JSON.stringify([+num]) }) .then(res => { if (res.statusCode === 401) @@ -29,7 +43,8 @@ const getInfoOperator = async (num = '79521305638') => { } const SERVICE = { - getInfoOperator: getInfoOperator + getInfoOperator: getInfoOperator, + getJokeDev: getJokeDev } module.exports = { SERVICE } diff --git a/test.js b/test.js new file mode 100644 index 0000000..0f4e35a --- /dev/null +++ b/test.js @@ -0,0 +1 @@ +console.log()