'use strict';
// Utils
const { html } = require('../../utils/html');
const { isMaster } = require('../../utils/config');
const { link, scheduleDeletion } = require('../../utils/tg');
const { parse, strip } = require('../../utils/cmd');
// DB
const {
admin,
getUser,
} = require('../../stores/user');
/** @param { import('../../typings/context').ExtendedContext } ctx */
const adminHandler = async (ctx) => {
if (!isMaster(ctx.from)) return null;
const { targets } = parse(ctx.message);
if (targets.length > 1) {
return ctx.replyWithHTML(
'ℹ️ Specify one user to promote.',
).then(scheduleDeletion());
}
const userToAdmin = targets.length
? await getUser(strip(targets[0]))
: ctx.from;
if (!userToAdmin) {
return ctx.replyWithHTML(
'❓ User unknown.\n' +
'Please forward their message, then try again.',
).then(scheduleDeletion());
}
if (userToAdmin.status === 'banned') {
return ctx.replyWithHTML('ℹ️ Can\'t admin banned user.');
}
if (userToAdmin.status === 'admin') {
return ctx.replyWithHTML(
html`⭐️ ${link(userToAdmin)} is already admin.`,
);
}
await admin(userToAdmin);
return ctx.replyWithHTML(html`⭐️ ${link(userToAdmin)} is now admin.`);
};
module.exports = adminHandler;