2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-24 10:58:19 +00:00

57 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2020-03-13 22:02:41 +01:00
// @ts-check
'use strict';
const { context } = require('../bot');
2020-06-15 14:45:55 +02:00
const { html, lrm } = require('../utils/html');
2020-05-13 15:11:43 +02:00
const { link } = require('../utils/tg');
const { isWarnNotExpired, expireWarnsAfter } = require('../utils/config');
2020-05-08 21:30:49 +02:00
const { numberOfWarnsToBan } = require('../utils/config').config;
const { warn } = require('../stores/user');
const ban = require('./ban');
const ms = require('millisecond');
/** @type {(n: number, d?: number) => string} */
const z = (n, d = 2) => String(n).padStart(d, '0');
/** @type {(d: Date) => string} */
const yyyymmdd = d => `${z(d.getFullYear(), 4)}-${z(d.getMonth() + 1)}-${z(d.getDate())}`;
/** @type {(a: number, b: number) => number} */
2019-07-07 17:58:23 +02:00
const cmp = (a, b) => Math.sign(a - b);
2019-05-31 17:02:00 +02:00
module.exports = async ({ admin, amend, reason, userToWarn }) => {
const by_id = admin.id;
const date = new Date();
2019-05-31 17:02:00 +02:00
const { warns } = await warn(
userToWarn,
{ by_id, date, reason },
{ amend },
2019-05-31 17:02:00 +02:00
);
2020-05-08 21:30:49 +02:00
const recentWarns = warns.filter(isWarnNotExpired(date));
2019-07-07 17:58:23 +02:00
const count = {
2020-05-13 15:11:43 +02:00
'-1': html`<b>${recentWarns.length}</b>/${numberOfWarnsToBan}`,
0: html`<b>Final warning</b>`,
// eslint-disable-next-line max-len
1: html`<b>${recentWarns.length}</b>/${numberOfWarnsToBan} (🚫 <b>banned</b>)`,
2019-07-07 17:58:23 +02:00
}[cmp(recentWarns.length + 1, numberOfWarnsToBan)];
2020-05-13 15:11:43 +02:00
const warnMessage = html`
2020-06-15 14:45:55 +02:00
${lrm}${admin.first_name} <b>warned</b> ${link(userToWarn)}.
${count}: ${lrm}${reason}
<i>${typeof expireWarnsAfter === 'undefined' || expireWarnsAfter === Infinity ? '' : `This warning expires on ${yyyymmdd(new Date(date.getTime() + ms(expireWarnsAfter)))}`}</i>
2020-05-13 15:11:43 +02:00
`;
2018-11-28 18:23:57 +01:00
if (recentWarns.length >= numberOfWarnsToBan) {
await ban({
admin: context.botInfo,
reason: 'Reached max number of warnings',
userToBan: userToWarn,
});
}
return warnMessage;
};