2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-22 09:57:50 +00:00
Thomas Rory Gummerson 37974a3d92 Updates
2023-03-08 14:08:57 +01:00

131 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @ts-check
'use strict';
// Utils
const { inspect } = require('util');
const { displayUser, scheduleDeletion } = require('../../utils/tg');
const { html, lrm, TgHtml } = require('../../utils/html');
const { isMaster, isWarnNotExpired } = require('../../utils/config');
const { parse, strip } = require('../../utils/cmd');
// DB
const { getUser, permit } = require('../../stores/user');
/** @param {Date} date */
const formatDate = (date) =>
date && date.toISOString().slice(0, -5).replace('T', ' ') + ' UTC';
/**
* @param {string} defaultVal
*/
const formatEntry = async (entry, defaultVal) => {
if (!entry || !entry.by_id) return html`${defaultVal}`;
const { first_name } = (await getUser({ id: entry.by_id })) || {};
if (!first_name)
return html`${lrm}${entry.reason} (${formatDate(entry.date)})`;
return html`${lrm}${entry.reason} (${first_name}, ${formatDate(entry.date)})`;
};
const formatWarn = async (warn, i) =>
isWarnNotExpired(new Date())(warn)
? html`${i + 1}. ${await formatEntry(warn, warn)}`
: html`<del>${i + 1}. ${await formatEntry(warn, warn)}</del>`;
/**
* @param {string | TgHtml | undefined } content
*/
const isNotEmpty = (content) => content?.length;
const optional = (header, sep, content) =>
isNotEmpty(content) ? html`${header}${sep}${content}` : html``;
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>`;
};
/** @param { import('../../typings/context').ExtendedContext } ctx */
const getWarnsHandler = async (ctx) => {
if (!ctx.from) {
return ctx
.replyWithHTML(
' <b>This command is not available in channels.</b>'
)
.then(scheduleDeletion());
}
if (typeof ctx.message === 'undefined') return;
if (!('text' in ctx.message)) return;
const { flags, targets } = parse(ctx.message);
if (targets.length > 1) {
return ctx
.replyWithHTML(' <b>Specify one user.</b>')
.then(scheduleDeletion());
}
const theUser =
targets.length && ctx.from.status === 'admin'
? await getUser(strip(targets[0]))
: ctx.from;
if (!theUser) {
return ctx
.replyWithHTML('❓ <b>User unknown.</b>')
.then(scheduleDeletion());
}
if (flags.has('raw') && ctx.from.status === 'admin') {
return ctx
.replyWithHTML(TgHtml.pre(inspect(theUser)))
.then(scheduleDeletion());
}
const header = html`${title(theUser)} ${displayUser(theUser)}`;
const banReason = optional(
html`🚫 <b>Ban reason:</b>`,
' ',
await formatEntry(theUser.ban_details, theUser.ban_reason || '')
);
const { warns = [] } = theUser;
const userWarns = optional(
html`<b>⚠️ Warns:</b>`,
'\n',
TgHtml.join('\n', await Promise.all(warns.map(formatWarn)))
);
const firstSeen = optional(
html`👀 <b>First seen:</b>`,
' ',
formatDate(theUser.createdAt)
);
const permits = permit.isValid(theUser.permit)
? // eslint-disable-next-line max-len
`🎟 ${
(await getUser({ id: theUser.permit.by_id })).first_name
}, ${formatDate(theUser.permit.date)}`
: '';
const oneliners = TgHtml.join(
'\n',
[header, firstSeen, permits].filter(isNotEmpty)
);
return ctx
.replyWithHTML(
TgHtml.join(
'\n\n',
[oneliners, userWarns, banReason].filter(isNotEmpty)
)
)
.then(scheduleDeletion());
};
module.exports = getWarnsHandler;