2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-01 06:35:23 +00:00

Rewrite /report

This commit is contained in:
Wojciech Pawlik
2020-05-13 17:22:30 +02:00
parent f15b22d1f0
commit e814ec185d

View File

@@ -1,57 +1,58 @@
'use strict'; 'use strict';
// Utils // Utils
const Cmd = require('../../utils/cmd');
const { TgHtml } = require('../../utils/html');
const { const {
escapeHtml,
link, link,
msgLink, msgLink,
scheduleDeletion, scheduleDeletion,
} = require('../../utils/tg'); } = require('../../utils/tg');
// Bot
const { replyOptions } = require('../../bot/options');
const { chats = {} } = require('../../utils/config').config; const { chats = {} } = require('../../utils/config').config;
const isQualified = member => member.status === 'creator' ||
member.can_delete_messages &&
member.can_restrict_members;
const adminMention = ({ user }) =>
TgHtml.tag`<a href="tg://user?id=${user.id}">&#8203;</a>`;
/** @param { import('../../typings/context').ExtendedContext } ctx */ /** @param { import('../../typings/context').ExtendedContext } ctx */
const reportHandler = async ctx => { const reportHandler = async ctx => {
if (!ctx.chat.type.endsWith('group')) return null; if (!ctx.chat.type.endsWith('group')) return null;
const msg = ctx.message; if (!ctx.message.reply_to_message) {
if (!msg.reply_to_message) { return ctx.replyWithHTML(
return ctx.reply(
' <b>Reply to message you\'d like to report</b>', ' <b>Reply to message you\'d like to report</b>',
replyOptions
).then(scheduleDeletion()); ).then(scheduleDeletion());
} }
const admins = (await ctx.getChatAdministrators()) const admins = (await ctx.getChatAdministrators())
.filter(member => .filter(isQualified)
member.status === 'creator' || .map(adminMention);
member.can_delete_messages && // eslint-disable-next-line max-len
member.can_restrict_members const s = TgHtml.tag`❗️ ${link(ctx.from)} <b>reported the message to the admins</b>.${TgHtml.join('', admins)}`;
// eslint-disable-next-line function-paren-newline
).map(member => member.user);
const adminObjects = admins.map(user => ({
first_name: '', // small hack to be able to use link function
id: user.id,
}));
const adminsMention = adminObjects.map(link).join('');
const s = `❗️${link(ctx.from)} <b>reported the message to the admins.</b>` +
`${adminsMention}`;
const report = await ctx.replyWithHTML(s, { const report = await ctx.replyWithHTML(s, {
reply_to_message_id: msg.reply_to_message.message_id reply_to_message_id: ctx.message.reply_to_message.message_id,
}); });
if (chats.report) { if (chats.report) {
await ctx.tg.sendMessage( await ctx.tg.sendMessage(
chats.report, chats.report,
`❗️ Report in <a href="${msgLink(msg.reply_to_message)}">` + TgHtml.tag`❗️ ${link(ctx.from)} reported <a href="${msgLink(
escapeHtml(msg.chat.title) + ctx.message.reply_to_message,
'</a>!', )}">a message</a> in ${ctx.chat.title}!`,
{ ...replyOptions, {
parse_mode: 'HTML',
reply_markup: { inline_keyboard: [ [ { reply_markup: { inline_keyboard: [ [ {
text: '✔️ Handled', text: '✔️ Handled',
// eslint-disable-next-line max-len callback_data: Cmd.stringify({
callback_data: `/del -chat_id=${report.chat.id} -msg_id=${report.message_id} Report handled` command: 'del',
} ] ] } } flags: {
chat_id: report.chat.id,
msg_id: report.message_id,
},
reason: 'Report handled',
}),
} ] ] } },
); );
} }
return null; return null;