2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-29 05:07:49 +00:00

Add isMaster util function

This commit is contained in:
GingerPlusPlus 2018-05-04 19:51:49 +02:00
parent 7b34308462
commit 2fafa93b10
6 changed files with 39 additions and 16 deletions

6
.gitignore vendored
View File

@ -1,6 +1,6 @@
node_modules
data/
config.json
config.js
/data/
/config.json
/config.js
.env
.idea/

View File

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

View File

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

20
utils/config.js Normal file
View File

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

5
utils/eq.js Normal file
View File

@ -0,0 +1,5 @@
'use strict';
const R = require('ramda');
module.exports = R.map(R.eqBy, require('./normalize'));

7
utils/normalize.js Normal file
View File

@ -0,0 +1,7 @@
'use strict';
const username = s => s.replace(/^@/, '').toLowerCase();
module.exports = {
username,
};