From 919c422c184e7e9c5de22d9bb7a84858e2f36f16 Mon Sep 17 00:00:00 2001 From: GingerPlusPlus Date: Wed, 28 Nov 2018 18:23:57 +0100 Subject: [PATCH] Add warn expiry; closes #27. --- actions/warn.js | 14 ++++++++++---- example.config.js | 13 +++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/actions/warn.js b/actions/warn.js index 733fcca..8f850ce 100644 --- a/actions/warn.js +++ b/actions/warn.js @@ -1,29 +1,35 @@ 'use strict'; const dedent = require('dedent-js'); +const ms = require('millisecond'); const { context } = require('../bot'); const { link } = require('../utils/tg'); -const { numberOfWarnsToBan } = require('../config'); +const { + expireWarnsAfter = Infinity, + numberOfWarnsToBan, +} = require('../config'); const { warn } = require('../stores/user'); const ban = require('./ban'); +const isNewerThan = date => warning => warning.date >= date; module.exports = async ({ admin, reason, userToWarn }) => { const by_id = admin.id; const date = new Date(); const { warns } = await warn(userToWarn, { by_id, date, reason }); + const recentWarns = warns.filter(isNewerThan(date - ms(expireWarnsAfter))); const isLastWarn = ', last warning!' - .repeat(warns.length === numberOfWarnsToBan - 1); + .repeat(recentWarns.length === numberOfWarnsToBan - 1); const warnMessage = dedent(` ⚠️ ${link(admin)} warned ${link(userToWarn)} for: - ${reason} (${warns.length}/${numberOfWarnsToBan}${isLastWarn})`); + ${reason} (${recentWarns.length}/${numberOfWarnsToBan}${isLastWarn})`); - if (warns.length >= numberOfWarnsToBan) { + if (recentWarns.length >= numberOfWarnsToBan) { await ban({ admin: context.botInfo, reason: 'Reached max number of warnings', diff --git a/example.config.js b/example.config.js index d1b60b5..9e78282 100644 --- a/example.config.js +++ b/example.config.js @@ -14,8 +14,7 @@ * Millisecond * String to be parsed by https://npmjs.com/millisecond, * or number of milliseconds. Pass 0 to remove immediately. - * Pass false to disable option. - * @typedef {( number | string | false )} ms + * @typedef {( number | string )} ms */ module.exports = { @@ -46,6 +45,7 @@ module.exports = { * @type {ms} Millisecond * Timeout before removing join and leave messages. * [Look at typedef above for details.] + * Pass false to disable this feature. */ deleteJoinsAfter: '2 minutes', @@ -55,6 +55,7 @@ module.exports = { * [Look at typedef above for details.] * Pass an object with { auto, manual } for more granular control * over which messages get deleted + * Pass false to disable this feature. */ deleteWarnsAfter: false, @@ -62,6 +63,7 @@ module.exports = { * @type {ms} * Timeout before removing ban messages. * [Look at typedef above for details.] + * Pass false to disable this feature. */ deleteBansAfter: false, @@ -82,6 +84,13 @@ module.exports = { */ excludeLinks: [], + /** + * @type {ms} + * Don't count warns older than this value towards automatic ban. + * [Look at typedef above for details.] + */ + expireWarnsAfter: Infinity, + /** * @type {InlineKeyboardMarkup} * Inline keyboard to be added to reply to /groups.