update and fix
This commit is contained in:
parent
6aa498e0b3
commit
230e89379f
|
@ -4,6 +4,7 @@ logs
|
|||
.DS_Store
|
||||
.tmp
|
||||
*.log
|
||||
tests
|
||||
.env
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
|
|
|
@ -16,10 +16,9 @@ config.consignOpts = {
|
|||
|
||||
config.limitSecurity = {
|
||||
window: ms('15s'),
|
||||
limit: 6,
|
||||
onLimitExceeded: ({ message, i18n, replyWithHTML }) =>
|
||||
replyWithHTML(i18n.t('default.rateLimit'), {
|
||||
reply_to_message_id: message.message_id,
|
||||
limit: 8,
|
||||
onLimitExceeded: ({ replyWithHTML }) =>
|
||||
replyWithHTML('<code>❗️Please wait a second (5s)</code>', {
|
||||
disable_notification: true
|
||||
})
|
||||
}
|
||||
|
@ -31,17 +30,19 @@ config.i18nOpts = {
|
|||
}
|
||||
|
||||
config.settings = {
|
||||
setCommand: async ctx => {
|
||||
ctx.setMyCommands([
|
||||
{
|
||||
command: 'start',
|
||||
description: 'Start bot or restart'
|
||||
},
|
||||
{
|
||||
command: 'lang',
|
||||
description: 'Change language'
|
||||
}
|
||||
])
|
||||
setCommand: ({ setMyCommands }, next) => {
|
||||
return next().then(() => {
|
||||
setMyCommands([
|
||||
{
|
||||
command: 'start',
|
||||
description: 'Start bot or restart'
|
||||
},
|
||||
{
|
||||
command: 'lang',
|
||||
description: 'Change language'
|
||||
}
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,22 +26,6 @@ const Route = new Router(({ callbackQuery }) => {
|
|||
}
|
||||
})
|
||||
|
||||
;(async () => {
|
||||
if (config.isDev) {
|
||||
const { TG_APIROOT, BOT_CLUSTER_CORE } = process.env
|
||||
console.log(
|
||||
Object.assign(await bot.telegram.getMe(), {
|
||||
BOT_CLUSTER_CORE,
|
||||
TG_APIROOT
|
||||
})
|
||||
)
|
||||
await bot.use(config.settings.setCommand)
|
||||
// Dev handlers
|
||||
bot.use(cliUpdateTime())
|
||||
bot.use(Telegraf.log())
|
||||
}
|
||||
})()
|
||||
|
||||
bot.route = Route
|
||||
bot.context.db = db
|
||||
bot.context.extra = Extra
|
||||
|
@ -52,8 +36,23 @@ bot.use(session())
|
|||
bot.use(new I18n(config.i18nOpts))
|
||||
bot.use(updaterUser)
|
||||
bot.use(reportErr)
|
||||
;(async () => {
|
||||
if (config.isDev) {
|
||||
const { TG_APIROOT, BOT_CLUSTER_CORE } = process.env
|
||||
bot.use(Telegraf.log())
|
||||
bot.use(cliUpdateTime())
|
||||
await bot.use(config.settings.setCommand)
|
||||
console.log(
|
||||
Object.assign(await bot.telegram.getMe(), {
|
||||
BOT_CLUSTER_CORE,
|
||||
TG_APIROOT
|
||||
})
|
||||
)
|
||||
}
|
||||
})()
|
||||
|
||||
bot.on('callback_query', Route)
|
||||
bot.route.otherwise(({ deleteMessage }) => deleteMessage())
|
||||
|
||||
consign(config.consignOpts).include('handlers').then('route').into(bot)
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@ Object.keys(__MODELS__).forEach(model => {
|
|||
return (db[model.toString()] = __MODELS__[model.toString()])
|
||||
})
|
||||
|
||||
module.exports = { db }
|
||||
|
||||
/*
|
||||
readdirSync(path.resolve(__dirname, MODEL_PATTERN)).forEach(model => {
|
||||
const setModels = model.split('.')[0].toString().trim().match(DEFAULT_CHECK)
|
||||
|
@ -17,5 +19,3 @@ readdirSync(path.resolve(__dirname, MODEL_PATTERN)).forEach(model => {
|
|||
db[setModels.toString()] = require(MODEL_PATTERN + setModels)
|
||||
})
|
||||
*/
|
||||
|
||||
module.exports = { db }
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const mongoose = require('mongoose')
|
||||
const { cS } = require('../config/config')
|
||||
const { config, cS } = require('../config/config')
|
||||
|
||||
class Mongoose {
|
||||
async mongooseConnect() {
|
||||
|
@ -12,7 +12,6 @@ class Mongoose {
|
|||
socketTimeoutMS: 10000,
|
||||
connectTimeoutMS: 30000
|
||||
})
|
||||
|
||||
.catch(err => {
|
||||
console.error(err.stack)
|
||||
console.error(
|
||||
|
@ -21,6 +20,8 @@ class Mongoose {
|
|||
process.exit(0)
|
||||
})
|
||||
|
||||
mongoose.set('debug', config.isDev)
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
mongoose.connection.close(() => {
|
||||
console.log(`Disconnect ${process.env.MONGOOSE_URL}`)
|
||||
|
|
|
@ -15,7 +15,10 @@ const modelUser = new Schema({
|
|||
|
||||
username: String,
|
||||
|
||||
language_code: String,
|
||||
date: {
|
||||
type: Number,
|
||||
default: Date.now
|
||||
},
|
||||
|
||||
settings: {
|
||||
lang: {
|
||||
|
@ -24,6 +27,7 @@ const modelUser = new Schema({
|
|||
required: true
|
||||
}
|
||||
}
|
||||
// dataRoot: [{ type: Array, ref: 'State' }]
|
||||
})
|
||||
|
||||
module.exports = model('User', modelUser)
|
||||
|
|
|
@ -25,6 +25,5 @@ default:
|
|||
✅ Language changed successfully (${value})
|
||||
noAnswer: 🤨 I do not understand you
|
||||
closeMessage: ❌ Close
|
||||
rateLimit: <b>❗️Please wait a second (5s)</b>
|
||||
|
||||
|
||||
|
|
|
@ -25,4 +25,3 @@ default:
|
|||
✅ Язык успешно изменен (${value})
|
||||
noAnswer: 🤨 Я не понимаю тебя
|
||||
closeMessage: ❌ Закрыть
|
||||
rateLimit: <b>❗️Пожалуйста, подождите секунд (5s)</b>
|
||||
|
|
|
@ -2,26 +2,28 @@ const { escapeHtml } = require('../util/escapeHTML')
|
|||
|
||||
module.exports = async ({ db, from, i18n, session }, next) => {
|
||||
// eslint-disable-next-line camelcase
|
||||
const { id, first_name, username, language_code } = from
|
||||
if (!id) return await next()
|
||||
await db.User.findOne({ user_id: id })
|
||||
.then(root => {
|
||||
const { id: user_id, first_name, username } = from
|
||||
// eslint-disable-next-line camelcase
|
||||
if (!user_id) return await next()
|
||||
await db.User.findOne({ user_id })
|
||||
.sort({ user_id_1: 1 })
|
||||
.then(async root => {
|
||||
const nameFirst = escapeHtml(first_name) ?? 'Unknown'
|
||||
if (!root) {
|
||||
root = new db.User()
|
||||
root.user_id = id
|
||||
// eslint-disable-next-line camelcase
|
||||
root.first_name = escapeHtml(first_name) ?? 'Unknown'
|
||||
root.user_id = user_id
|
||||
root.first_name = nameFirst
|
||||
root.username = username ?? 'Unknown'
|
||||
}
|
||||
// eslint-disable-next-line camelcase
|
||||
root.first_name = escapeHtml(first_name) ?? 'Unknown'
|
||||
root.username = username
|
||||
// eslint-disable-next-line camelcase
|
||||
root.language_code = language_code
|
||||
|
||||
root.first_name = nameFirst
|
||||
root.username = username ?? 'Unknown'
|
||||
|
||||
session.root = root
|
||||
|
||||
i18n.locale(session.root.settings.lang)
|
||||
})
|
||||
.finally(() => {
|
||||
|
||||
return next().then(async () => {
|
||||
await session.root.save()
|
||||
})
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
module.exports = bot => {
|
||||
const { langTask } = bot.handlers
|
||||
|
||||
bot.command('lang', langTask.updaterLang)
|
||||
|
||||
const routeList = async ({
|
||||
i18n,
|
||||
state,
|
||||
|
@ -17,6 +19,5 @@ module.exports = bot => {
|
|||
}
|
||||
}
|
||||
|
||||
bot.command('lang', langTask.updaterLang)
|
||||
bot.route.on('set_lang', routeList)
|
||||
}
|
||||
|
|
|
@ -11,5 +11,4 @@ module.exports = bot => {
|
|||
}
|
||||
|
||||
bot.route.on('startMenu', routeList)
|
||||
bot.route.otherwise(({ deleteMessage }) => deleteMessage())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue