2017-09-23 20:57:32 +02:00
'use strict' ;
// Utils
2020-05-13 17:22:30 +02:00
const Cmd = require ( '../../utils/cmd' ) ;
const { TgHtml } = require ( '../../utils/html' ) ;
2019-04-06 15:39:40 +02:00
const {
link ,
msgLink ,
scheduleDeletion ,
} = require ( '../../utils/tg' ) ;
2017-09-23 20:57:32 +02:00
2020-03-09 23:27:19 +01:00
const { chats = { } } = require ( '../../utils/config' ) . config ;
2019-01-21 17:22:53 +01:00
2020-05-13 17:22:30 +02:00
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 } ">​</a> ` ;
2020-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
2017-09-23 20:57:32 +02:00
const reportHandler = async ctx => {
2019-01-24 19:53:10 +01:00
if ( ! ctx . chat . type . endsWith ( 'group' ) ) return null ;
2020-05-13 17:22:30 +02:00
if ( ! ctx . message . reply _to _message ) {
return ctx . replyWithHTML (
2017-10-31 23:08:22 +01:00
'ℹ ️ <b>Reply to message you\'d like to report</b>' ,
2018-11-20 11:47:30 +05:30
) . then ( scheduleDeletion ( ) ) ;
2017-09-23 20:57:32 +02:00
}
2017-11-11 11:50:37 +01:00
const admins = ( await ctx . getChatAdministrators ( ) )
2020-05-13 17:22:30 +02:00
. filter ( isQualified )
. map ( adminMention ) ;
// eslint-disable-next-line max-len
2020-09-18 12:49:16 +02:00
const s = TgHtml . tag ` ❗️ ${ link ( ctx . from ) } <b>reported the message from ${ link ( ctx . message . reply _to _message . from ) } to the admins</b>. ${ TgHtml . join ( '' , admins ) } ` ;
2020-02-06 12:35:04 +01:00
const report = await ctx . replyWithHTML ( s , {
2020-05-13 17:22:30 +02:00
reply _to _message _id : ctx . message . reply _to _message . message _id ,
2017-09-23 20:57:32 +02:00
} ) ;
2020-02-06 12:35:04 +01:00
if ( chats . report ) {
await ctx . tg . sendMessage (
chats . report ,
2020-05-13 17:22:30 +02:00
TgHtml . tag ` ❗️ ${ link ( ctx . from ) } reported <a href=" ${ msgLink (
ctx . message . reply _to _message ,
2020-09-18 12:49:16 +02:00
) } " > a message < / a > f r o m $ { l i n k ( c t x . m e s s a g e . r e p l y _ t o _ m e s s a g e . f r o m ) } i n $ { c t x . c h a t . t i t l e } ! ` ,
2020-05-13 17:22:30 +02:00
{
parse _mode : 'HTML' ,
2020-02-06 12:35:04 +01:00
reply _markup : { inline _keyboard : [ [ {
text : '✔️ Handled' ,
2020-05-13 17:22:30 +02:00
callback _data : Cmd . stringify ( {
command : 'del' ,
flags : {
chat _id : report . chat . id ,
msg _id : report . message _id ,
} ,
reason : 'Report handled' ,
} ) ,
} ] ] } } ,
2020-02-06 12:35:04 +01:00
) ;
}
return null ;
2017-09-23 20:57:32 +02:00
} ;
module . exports = reportHandler ;