make it better

This commit is contained in:
Secven 2021-07-16 08:59:34 +00:00
parent 20e06172fe
commit a080ab6085
11 changed files with 41 additions and 44 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ logs
tests
.DS_Store
.tmp
test.js
*.log
npm-debug.log*
yarn-debug.log*

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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(

View File

@ -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__)
})
}

View File

@ -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 () => {

View File

@ -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(
'<code>I can handle from you only one request in 15 seconds.</code>',
true,
{ disable_notification: true }

View File

@ -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 }

1
test.js Normal file
View File

@ -0,0 +1 @@
console.log()