2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-30 21:55:17 +00:00

Create middlewares/logPresence (#104)

This commit is contained in:
Dani
2019-07-06 10:10:41 -04:00
committed by GingerPlusPlus
parent 2c1bcb9f96
commit 5782abb704
4 changed files with 41 additions and 7 deletions

View File

@@ -1,7 +1,5 @@
'use strict';
/* eslint-disable sort-keys */
/*
* Create `config.js` by running `cp example.config.js config.js`
* in the project folder, then edit it.
@@ -36,6 +34,13 @@ module.exports = {
chats: {
/**
* @type {(number | false)}
* Chat to send member join/leave notifications to.
* Pass false to disable this feature.
*/
presenceLog: false,
/**
* @type {(number | false)}
* Chat to send report notifications to.

View File

@@ -1,6 +1,5 @@
'use strict';
const millisecond = require('millisecond');
const { Composer } = require('telegraf');
const composer = new Composer();
@@ -18,6 +17,7 @@ const removeChannelForwardsHandler = require('./removeChannelForwards');
const removeCommandsHandler = require('./removeCommands');
const syncStatusHandler = require('./syncStatus');
const updateUserDataHandler = require('./updateUserData');
const presenceLogHandler = require('./logPresence');
const updateGroupTitleHandler = require('./updateGroupTitle');
composer.on('new_chat_members', addedToGroupHandler);
@@ -32,9 +32,8 @@ composer.on('new_chat_title', updateGroupTitleHandler);
composer.on('text', removeCommandsHandler);
composer.on(
[ 'new_chat_members', 'left_chat_member' ],
deleteJoinsAfter === false
? Composer.passThru()
: deleteAfter(millisecond(deleteJoinsAfter))
deleteAfter(deleteJoinsAfter),
presenceLogHandler,
);
module.exports = composer;

View File

@@ -0,0 +1,28 @@
'use strict';
const { chats = {} } = require('../../config');
function getUsername(user) {
let str = user.first_name;
if (user.last_name) str += ' ' + user.last_name;
if (user.username) str += ' (@' + user.username + ')';
return str;
}
function log(ctx, next) {
if (!chats.presenceLog) return next();
if (ctx.updateSubTypes[0] === 'new_chat_members') {
ctx.telegram.sendMessage(
chats.presenceLog,
ctx.message.new_chat_members.map(getUsername).join(', ') +
' #joined ' + ctx.chat.title
);
} else if (ctx.updateSubTypes[0] === 'left_chat_member') {
ctx.telegram.sendMessage(
chats.presenceLog,
getUsername(ctx.message.left_chat_member) +
' #left ' + ctx.chat.title
);
}
return next();
}
module.exports = log;

View File

@@ -40,7 +40,9 @@ const displayUser = user =>
* @returns {undefined}
*/
const deleteAfter = ms => (ctx, next) => {
setTimeout(ctx.deleteMessage, ms);
if (ms !== false) {
setTimeout(ctx.deleteMessage, millisecond(ms));
}
next();
};