2020-05-08 21:30:49 +02:00
|
|
|
|
// @ts-check
|
2017-11-02 00:04:22 +03:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2020-05-08 21:30:49 +02:00
|
|
|
|
const { displayUser, scheduleDeletion } = require('../../utils/tg');
|
|
|
|
|
const { isMaster, isWarnNotExpired } = require('../../utils/config');
|
|
|
|
|
const { parse, strip } = require('../../utils/cmd');
|
2017-11-02 00:04:22 +03:30
|
|
|
|
|
|
|
|
|
// Bot
|
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
|
|
|
|
|
|
|
|
|
// DB
|
2017-11-02 16:54:17 +01:00
|
|
|
|
const { getUser } = require('../../stores/user');
|
2017-11-02 00:04:22 +03:30
|
|
|
|
|
2019-05-02 21:42:13 +02:00
|
|
|
|
const html = require('tg-html');
|
|
|
|
|
|
2019-05-31 13:36:37 +02:00
|
|
|
|
const formatDate = date =>
|
|
|
|
|
date && date.toISOString().slice(0, -5).replace('T', ' ');
|
2019-05-02 21:42:13 +02:00
|
|
|
|
|
|
|
|
|
const formatEntry = async (entry, defaultVal) => {
|
|
|
|
|
if (!entry || !entry.by_id) return defaultVal;
|
|
|
|
|
const { first_name } = await getUser({ id: entry.by_id }) || {};
|
2019-05-04 14:06:24 +02:00
|
|
|
|
if (!first_name) return html`${entry.reason} (${formatDate(entry.date)})`;
|
2019-05-02 21:42:13 +02:00
|
|
|
|
return html`${entry.reason} (${first_name}, ${formatDate(entry.date)})`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const formatWarn = async (warn, i) =>
|
2020-05-08 21:30:49 +02:00
|
|
|
|
isWarnNotExpired(new Date())(warn)
|
|
|
|
|
? html`${i + 1}. ${await formatEntry(warn, warn)}`
|
|
|
|
|
: html`<del>${i + 1}. ${await formatEntry(warn, warn)}</del>`;
|
2019-05-02 21:42:13 +02:00
|
|
|
|
|
|
|
|
|
const optional = (header, content) =>
|
|
|
|
|
content
|
|
|
|
|
? header + content + '\n'
|
|
|
|
|
: '';
|
|
|
|
|
|
2020-05-08 21:30:49 +02:00
|
|
|
|
const title = user => {
|
|
|
|
|
if (isMaster(user)) {
|
|
|
|
|
return html`🕴️ <b>Bot master</b>`;
|
|
|
|
|
} else if (user.status === 'admin') {
|
|
|
|
|
return html`⭐️ <b>Admin</b>`;
|
|
|
|
|
}
|
|
|
|
|
return html`👤 <b>User</b>`;
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-10 22:10:48 +01:00
|
|
|
|
/** @param { import('../../typings/context').ExtendedContext } ctx */
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const getWarnsHandler = async ({ from, message, reply }) => {
|
|
|
|
|
if (!from) {
|
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>This command is not available in channels.</b>',
|
2020-05-08 21:30:49 +02:00
|
|
|
|
replyOptions,
|
2019-01-28 21:46:29 +01:00
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
2017-11-02 00:04:22 +03:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
const { targets } = parse(message);
|
2017-11-02 15:30:54 +03:30
|
|
|
|
|
2019-01-28 21:46:29 +01:00
|
|
|
|
if (targets.length > 1) {
|
|
|
|
|
return reply(
|
2019-05-02 21:42:13 +02:00
|
|
|
|
'ℹ️ <b>Specify one user.</b>',
|
2020-05-08 21:30:49 +02:00
|
|
|
|
replyOptions,
|
2019-01-28 21:46:29 +01:00
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const theUser = targets.length && from.status === 'admin'
|
|
|
|
|
? await getUser(strip(targets[0]))
|
|
|
|
|
: from;
|
|
|
|
|
|
|
|
|
|
if (!theUser) {
|
|
|
|
|
return reply(
|
|
|
|
|
'❓ <b>User unknown.</b>',
|
2020-05-08 21:30:49 +02:00
|
|
|
|
replyOptions,
|
2019-01-28 21:46:29 +01:00
|
|
|
|
).then(scheduleDeletion());
|
|
|
|
|
}
|
2017-11-02 00:04:22 +03:30
|
|
|
|
|
2020-05-08 21:30:49 +02:00
|
|
|
|
const header = html`${title(theUser)} ${displayUser(theUser)}\n`;
|
2019-05-02 21:42:13 +02:00
|
|
|
|
const banReason = optional(
|
|
|
|
|
'\n🚫 <b>Ban reason:</b> ',
|
2020-05-08 21:30:49 +02:00
|
|
|
|
await formatEntry(theUser.ban_details, theUser.ban_reason),
|
2019-05-02 21:42:13 +02:00
|
|
|
|
);
|
2020-05-08 21:30:49 +02:00
|
|
|
|
const { warns = [] } = theUser;
|
2019-05-02 21:42:13 +02:00
|
|
|
|
const userWarns = optional(
|
|
|
|
|
'\n<b>⚠️ Warns:</b>\n',
|
2020-05-08 21:30:49 +02:00
|
|
|
|
(await Promise.all(warns.map(formatWarn))).join('\n'),
|
2019-05-02 21:42:13 +02:00
|
|
|
|
);
|
2017-11-02 00:04:22 +03:30
|
|
|
|
|
|
|
|
|
return reply(
|
2020-05-08 21:30:49 +02:00
|
|
|
|
header +
|
2017-11-05 20:41:31 +03:30
|
|
|
|
userWarns +
|
|
|
|
|
banReason,
|
2020-05-08 21:30:49 +02:00
|
|
|
|
replyOptions,
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-11-02 00:04:22 +03:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = getWarnsHandler;
|