2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-31 06:05:22 +00:00

Add warn expiry; closes #27.

This commit is contained in:
GingerPlusPlus
2018-11-28 18:23:57 +01:00
parent eeb571226d
commit 919c422c18
2 changed files with 21 additions and 6 deletions

View File

@@ -1,29 +1,35 @@
'use strict'; 'use strict';
const dedent = require('dedent-js'); const dedent = require('dedent-js');
const ms = require('millisecond');
const { context } = require('../bot'); const { context } = require('../bot');
const { link } = require('../utils/tg'); const { link } = require('../utils/tg');
const { numberOfWarnsToBan } = require('../config'); const {
expireWarnsAfter = Infinity,
numberOfWarnsToBan,
} = require('../config');
const { warn } = require('../stores/user'); const { warn } = require('../stores/user');
const ban = require('./ban'); const ban = require('./ban');
const isNewerThan = date => warning => warning.date >= date;
module.exports = async ({ admin, reason, userToWarn }) => { module.exports = async ({ admin, reason, userToWarn }) => {
const by_id = admin.id; const by_id = admin.id;
const date = new Date(); const date = new Date();
const { warns } = await warn(userToWarn, { by_id, date, reason }); const { warns } = await warn(userToWarn, { by_id, date, reason });
const recentWarns = warns.filter(isNewerThan(date - ms(expireWarnsAfter)));
const isLastWarn = ', <b>last warning!</b>' const isLastWarn = ', <b>last warning!</b>'
.repeat(warns.length === numberOfWarnsToBan - 1); .repeat(recentWarns.length === numberOfWarnsToBan - 1);
const warnMessage = dedent(` const warnMessage = dedent(`
⚠️ ${link(admin)} <b>warned</b> ${link(userToWarn)} <b>for</b>: ⚠️ ${link(admin)} <b>warned</b> ${link(userToWarn)} <b>for</b>:
${reason} (${warns.length}/${numberOfWarnsToBan}${isLastWarn})`); ${reason} (${recentWarns.length}/${numberOfWarnsToBan}${isLastWarn})`);
if (warns.length >= numberOfWarnsToBan) { if (recentWarns.length >= numberOfWarnsToBan) {
await ban({ await ban({
admin: context.botInfo, admin: context.botInfo,
reason: 'Reached max number of warnings', reason: 'Reached max number of warnings',

View File

@@ -14,8 +14,7 @@
* Millisecond * Millisecond
* String to be parsed by https://npmjs.com/millisecond, * String to be parsed by https://npmjs.com/millisecond,
* or number of milliseconds. Pass 0 to remove immediately. * or number of milliseconds. Pass 0 to remove immediately.
* Pass false to disable option. * @typedef {( number | string )} ms
* @typedef {( number | string | false )} ms
*/ */
module.exports = { module.exports = {
@@ -46,6 +45,7 @@ module.exports = {
* @type {ms} Millisecond * @type {ms} Millisecond
* Timeout before removing join and leave messages. * Timeout before removing join and leave messages.
* [Look at typedef above for details.] * [Look at typedef above for details.]
* Pass false to disable this feature.
*/ */
deleteJoinsAfter: '2 minutes', deleteJoinsAfter: '2 minutes',
@@ -55,6 +55,7 @@ module.exports = {
* [Look at typedef above for details.] * [Look at typedef above for details.]
* Pass an object with { auto, manual } for more granular control * Pass an object with { auto, manual } for more granular control
* over which messages get deleted * over which messages get deleted
* Pass false to disable this feature.
*/ */
deleteWarnsAfter: false, deleteWarnsAfter: false,
@@ -62,6 +63,7 @@ module.exports = {
* @type {ms} * @type {ms}
* Timeout before removing ban messages. * Timeout before removing ban messages.
* [Look at typedef above for details.] * [Look at typedef above for details.]
* Pass false to disable this feature.
*/ */
deleteBansAfter: false, deleteBansAfter: false,
@@ -82,6 +84,13 @@ module.exports = {
*/ */
excludeLinks: [], excludeLinks: [],
/**
* @type {ms}
* Don't count warns older than this value towards automatic ban.
* [Look at typedef above for details.]
*/
expireWarnsAfter: Infinity,
/** /**
* @type {InlineKeyboardMarkup} * @type {InlineKeyboardMarkup}
* Inline keyboard to be added to reply to /groups. * Inline keyboard to be added to reply to /groups.