diff --git a/.gitignore b/.gitignore index 155f90b..5340214 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ node_modules -data/ -config.json -config.js +/data/ +/config.json +/config.js .env .idea/ diff --git a/handlers/middlewares/addedToGroup.js b/handlers/middlewares/addedToGroup.js index 1efd1e2..fcf6d21 100644 --- a/handlers/middlewares/addedToGroup.js +++ b/handlers/middlewares/addedToGroup.js @@ -5,18 +5,14 @@ const { replyOptions } = require('../../bot/options'); const { admin } = require('../../stores/user'); const { addGroup } = require('../../stores/group'); -const { master } = require('../../config'); +const { isMaster } = require('../../utils/config'); const addedToGroupHandler = async (ctx, next) => { const msg = ctx.message; - const isMaster = ctx.from.id === Number(master) || - ctx.from.username && - ctx.from.username.toLowerCase() === - String(master).replace('@', '').toLowerCase(); const wasAdded = msg.new_chat_members.some(user => user.username === ctx.me); - if (wasAdded && isMaster) { + if (wasAdded && isMaster(ctx.from)) { await admin(ctx.from); const link = ctx.chat.username ? `https://t.me/${ctx.chat.username.toLowerCase()}` diff --git a/handlers/middlewares/updateUserData.js b/handlers/middlewares/updateUserData.js index 886ecc2..c8d5d21 100644 --- a/handlers/middlewares/updateUserData.js +++ b/handlers/middlewares/updateUserData.js @@ -1,7 +1,6 @@ 'use strict'; -// Config -const { master } = require('../../config'); +const { isMaster } = require('../../utils/config'); // DB const { updateUser } = require('../../stores/user'); @@ -17,11 +16,7 @@ const updateUserDataHandler = async (ctx, next) => { ctx.state = { isAdmin: user && user.status === 'admin', - isMaster: user && - (user.id === Number(master) || - user.username && - user.username.toLowerCase() === - String(master).replace('@', '').toLowerCase()), + isMaster: isMaster(ctx.from), user, }; diff --git a/utils/config.js b/utils/config.js new file mode 100644 index 0000000..f1c4a02 --- /dev/null +++ b/utils/config.js @@ -0,0 +1,20 @@ +'use strict'; + +const config = require('../config'); +const eq = require('./eq'); + +const masterById = /^\d+$/.test(config.master); +const masterByUsername = /^@?\w+$/.test(config.master); + +if (!masterById && !masterByUsername) { + throw new Error('Invalid value for `master` in config file: ' + + config.master); +} + +const isMaster = masterById + ? user => user.id === Number(config.master) + : user => user.username && eq.username(user.username, config.master); + +module.exports = { + isMaster, +}; diff --git a/utils/eq.js b/utils/eq.js new file mode 100644 index 0000000..ddb95cb --- /dev/null +++ b/utils/eq.js @@ -0,0 +1,5 @@ +'use strict'; + +const R = require('ramda'); + +module.exports = R.map(R.eqBy, require('./normalize')); diff --git a/utils/normalize.js b/utils/normalize.js new file mode 100644 index 0000000..fc9ebb6 --- /dev/null +++ b/utils/normalize.js @@ -0,0 +1,7 @@ +'use strict'; + +const username = s => s.replace(/^@/, '').toLowerCase(); + +module.exports = { + username, +};