mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-29 13:17:56 +00:00
Allow to remove any warn by specifying its date
This commit is contained in:
parent
6d6c624df8
commit
16f303622b
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { last } = require('ramda');
|
||||
const XRegExp = require('xregexp');
|
||||
|
||||
// Utils
|
||||
const { escapeHtml, link, scheduleDeletion } = require('../../utils/tg');
|
||||
@ -18,10 +19,21 @@ const { getUser, unwarn } = require('../../stores/user');
|
||||
|
||||
const noop = Function.prototype;
|
||||
|
||||
const dateRegex = XRegExp.tag('nix')`^
|
||||
\d{4} # year
|
||||
-\d{2} # month
|
||||
(-\d{2} # day
|
||||
([T\s]\d{2} # hour
|
||||
(:\d{2} # min
|
||||
(:\d{2} # sec
|
||||
(.\d{3}Z? # ms
|
||||
)?)?)?)?)?
|
||||
$`;
|
||||
|
||||
const unwarnHandler = async ({ from, message, reply, telegram }) => {
|
||||
if (!from || from.status !== 'admin') return null;
|
||||
|
||||
const { targets } = parse(message);
|
||||
const { reason, targets } = parse(message);
|
||||
|
||||
if (targets.length !== 1) {
|
||||
return reply(
|
||||
@ -55,7 +67,28 @@ const unwarnHandler = async ({ from, message, reply, telegram }) => {
|
||||
telegram.unbanChatMember(group.id, userToUnwarn.id));
|
||||
}
|
||||
|
||||
await unwarn(userToUnwarn);
|
||||
let lastWarn;
|
||||
if (!reason) {
|
||||
lastWarn = last(allWarns);
|
||||
} else if (dateRegex.test(reason)) {
|
||||
const normalized = reason.replace(' ', 'T').toUpperCase();
|
||||
lastWarn = allWarns.find(({ date }) =>
|
||||
date && date.toISOString().startsWith(normalized));
|
||||
} else {
|
||||
return reply(
|
||||
'⚠ <b>Invalid date</b>',
|
||||
replyOptions
|
||||
).then(scheduleDeletion());
|
||||
}
|
||||
|
||||
if (!lastWarn) {
|
||||
return reply(
|
||||
'❓ <b>404: Warn not found</b>',
|
||||
replyOptions
|
||||
).then(scheduleDeletion());
|
||||
}
|
||||
|
||||
await unwarn(userToUnwarn, lastWarn);
|
||||
|
||||
if (userToUnwarn.status === 'banned') {
|
||||
telegram.sendMessage(
|
||||
@ -68,8 +101,6 @@ const unwarnHandler = async ({ from, message, reply, telegram }) => {
|
||||
// (it's an expected, non-critical failure)
|
||||
}
|
||||
|
||||
const lastWarn = last(allWarns);
|
||||
|
||||
return reply(
|
||||
`❎ ${link(from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
||||
`<b>for:</b>\n\n${escapeHtml(lastWarn.reason || lastWarn)}` +
|
||||
|
@ -12,7 +12,8 @@ const { getUser } = require('../../stores/user');
|
||||
|
||||
const html = require('tg-html');
|
||||
|
||||
const formatDate = date => date && date.toISOString().slice(0, 10);
|
||||
const formatDate = date =>
|
||||
date && date.toISOString().slice(0, -5).replace('T', ' ');
|
||||
|
||||
const formatEntry = async (entry, defaultVal) => {
|
||||
if (!entry || !entry.by_id) return defaultVal;
|
||||
|
@ -146,11 +146,11 @@ const warn = ({ id }, reason) =>
|
||||
{ returnUpdatedDocs: true }
|
||||
).then(getUpdatedDocument);
|
||||
|
||||
const unwarn = ({ id }) =>
|
||||
const unwarn = ({ id }, warnQuery) =>
|
||||
User.update(
|
||||
{ id },
|
||||
{
|
||||
$pop: { warns: 1 },
|
||||
$pull: { warns: warnQuery },
|
||||
$set: { status: 'member' },
|
||||
$unset: { ban_details: true, ban_reason: true },
|
||||
}
|
||||
|
@ -18,13 +18,12 @@ const botReply = ({ from, entities = [] }) => {
|
||||
};
|
||||
|
||||
const parse = message => {
|
||||
// eslint-disable-next-line no-empty-character-class
|
||||
const regex = /^\/\w+(?:@\w+)?((?:@\w+|\d+|\s+)*)(.*)$/s;
|
||||
const regex = /^\/\w+(?:@\w+)?((?:@\w+|\d+|\s+)*)(?:\s+(.*))?$/s;
|
||||
|
||||
const textMentions = message.entities.filter(isTextMention);
|
||||
const noTextMentions = textMentions.reduceRight(spliceOut, message.text);
|
||||
|
||||
const [ , ids, reason ] = regex.exec(noTextMentions);
|
||||
const [ , ids, reason = '' ] = regex.exec(noTextMentions);
|
||||
const users = textMentions.concat(ids.match(/@\w+|\d+/g) || []);
|
||||
const { reply_to_message } = message;
|
||||
const targets = users.length
|
||||
|
Loading…
x
Reference in New Issue
Block a user