'use strict';
// Utils
const { parse, strip, substom } = require('../../utils/cmd');
const { scheduleDeletion } = require('../../utils/tg');
// DB
const { getUser } = require('../../stores/user');
/** @param { import('../../typings/context').ExtendedContext } ctx */
const warnHandler = async (ctx) => {
if (!ctx.message.chat.type.endsWith('group')) {
return ctx.replyWithHTML(
'ℹ️ This command is only available in groups.',
);
}
if (ctx.from.status !== 'admin') return null;
const { flags, reason, targets } = parse(ctx.message);
if (targets.length !== 1) {
return ctx.replyWithHTML(
'ℹ️ Specify one user to warn.',
).then(scheduleDeletion());
}
const userToWarn = await getUser(strip(targets[0]));
if (!userToWarn) {
return ctx.replyWithHTML(
'❓ User unknown.\n' +
'Please forward their message, then try again.',
).then(scheduleDeletion());
}
if (userToWarn.id === ctx.botInfo.id) return null;
if (userToWarn.status === 'admin') {
return ctx.replyWithHTML('ℹ️ Can\'t warn other admins.');
}
if (reason.length === 0) {
return ctx.replyWithHTML('ℹ️ Need a reason to warn.')
.then(scheduleDeletion());
}
return ctx.warn({
admin: ctx.from,
amend: flags.has('amend'),
reason: '[' + ctx.chat.title + '] ' + await substom(reason),
userToWarn,
mode: 'manual',
msg: ctx.message.reply_to_message,
});
};
module.exports = warnHandler;