import * as TelegramBot from "node-telegram-bot-api" ; import { BotConfig } from "./botConfig" ; import { Commands } from "./commands" ; import * as CONSTS from "./consts" ; import * as PhotoData from "./photoData" ; import { TelegramDownload } from "./telegramDownload" ; import { Utils } from "./utils" ; const tracer = require ( "tracer" ) ; const logger = tracer . colorConsole ( { level : process . env . DEBUG !== undefined ? process . env . DEBUG : "info" } ) ; class Operations { param data param bot param config param members param msg param command param commandArgs public static async parseCommand ( data , bot , config , members , msg , command , commandArgs ) { if ( members instanceof Error ) { return ; } if ( CONSTS . COMMANDS_ADMINS_ONLY . indexOf ( command ) !== - 1 ) { if ( msg . from ) { if ( members . map ( ( member ) => member . user . id ) . indexOf ( msg . from . id ) === - 1 ) { return ; } } } switch ( command ) { case CONSTS . COMMANDS . SET_PAUSED : await Commands . _COMMANDS_SET_PAUSED ( PhotoData . PhotoDataStructure . getData , data , bot , msg ) ; break ; case CONSTS . COMMANDS . SET_RESUMED : await Commands . _COMMANDS_SET_RESUMED ( PhotoData . PhotoDataStructure . getData , data , bot , msg ) ; break ; case CONSTS . COMMANDS . SET_INTERVAL : await Commands . _COMMANDS_SET_INTERVAL ( PhotoData . PhotoDataStructure . getData , data , bot , config , msg , commandArgs ) ; break ; case CONSTS . COMMANDS . NEXT_PHOTO : await Commands . _COMMANDS_NEXT_PHOTO ( PhotoData . PhotoDataStructure . getData , data , bot , msg ) ; break ; case CONSTS . COMMANDS . DELETE : await Commands . _COMMANDS_DELETE ( Operations . delPhoto , data , bot , msg ) ; break ; case CONSTS . COMMANDS . BAN : await Commands . _COMMANDS_BAN ( Operations . delPhoto , data , bot , msg ) ; break ; case CONSTS . COMMANDS . UNBAN : await Commands . _COMMANDS_UNBAN ( Operations . unbanPhoto , data , msg ) ; break ; case CONSTS . COMMANDS . QUEUE_STATUS : await Commands . _COMMANDS_QUEUE_STATUS ( PhotoData . PhotoDataStructure . getData , data , bot , msg ) ; break ; default : } } param data param bot param msg param ban public static async delPhoto ( data , bot , msg , ban = false ) { const chatId = msg . chat . id ; const chatData = PhotoData . PhotoDataStructure . getData ( data , chatId ) ; let fileIdList ; if ( msg . reply_to_message ) { fileIdList = ( msg . reply_to_message . photo ? msg . reply_to_message . photo . map < string > ( ( p ) => p . file_id ) : [ ] ) . concat ( msg . reply_to_message . document ? [ msg . reply_to_message . document . file_id ] : [ ] ) ; } else { fileIdList = ( chatData . history . length !== 0 ? [ chatData . history [ chatData . history . length - 1 ] ] : [ ] ) ; } if ( fileIdList . length === 0 ) { return ; } if ( ban ) { fileIdList . map ( ( p ) => chatData . banList . indexOf ( p ) === - 1 ? chatData . banList . push ( p ) : null ) ; logger . info ( CONSTS . BANNED_TEXT ( chatId , fileIdList . join ( ", " ) ) ) ; await bot . sendMessage ( chatId , CONSTS . BANNED_PHOTO , { reply_to_message_id : msg . message_id } ) ; } else { logger . info ( CONSTS . DELETE_TEXT ( chatId , fileIdList . join ( ", " ) ) ) ; await bot . sendMessage ( chatId , CONSTS . DELETE_PHOTO , { reply_to_message_id : msg . message_id } ) ; } if ( fileIdList . includes ( chatData . history [ chatData . history . length - 1 ] ) ) { await chatData . nextPhoto ( bot ) ; } chatData . queue = chatData . queue . filter ( ( q ) => ! fileIdList . includes ( q ) ) ; chatData . history = chatData . history . filter ( ( q ) => ! fileIdList . includes ( q ) ) ; } param data param msg public static async unbanPhoto ( data , msg ) { const chatId = msg . chat . id ; const fileIdList = ( msg . reply_to_message ! . photo ? msg . reply_to_message ! . photo . map < string > ( ( p ) => p . file_id ) : [ ] ) . concat ( msg . reply_to_message ! . document ? [ msg . reply_to_message ! . document . file_id ] : [ ] ) ; const chatData = PhotoData . PhotoDataStructure . getData ( data , chatId ) ; chatData . banList = chatData . banList . map < string > ( ( b ) => fileIdList . indexOf ( b ) !== - 1 ? "" : b ) . filter ( ( b ) => b ) ; logger . info ( CONSTS . UNBANNED_TEXT ( chatId , fileIdList . join ( ", " ) ) ) ; } param data param bot param downloader param msg public static async addPhoto ( data , bot , downloader , msg ) { const result = await Utils . checkQueue ( bot , data , downloader , msg ) ; if ( result !== undefined && result . length > 0 ) { await Utils . sendQueueResult ( bot , msg , result ) ; } return result ; } param data param bot param downloader param msg param imageBuffer param ent param url param imgUrl public static async uploadPhoto ( data , bot , downloader , msg , imageBuffer , ent , url , imgUrl ? ) { logger . info ( CONSTS . UPLOADING_PHOTO ( `${ msg . chat . title }(${ msg . chat . id })` , imageBuffer , url ) ) ; const illust = imgUrl instanceof PhotoData . PixivIllustStructure ? imgUrl : null ; const caption = ( illust ? CONSTS . GROUP_PHOTO_PIXIV_CAPTION ( illust ) : CONSTS . GROUP_PHOTO_CAPTION ) ; return Operations . sendPhotoPromise ( data , bot , downloader , msg , imageBuffer , { caption } ) ; } public static async sendPhotoPromise ( data , bot , downloader , msg , buffer , options ? ) { const opt = Object . apply ( { reply_to_message_id : msg . message_id , disable_notification : true } , options as [ any ] ) as TelegramBot . SendPhotoOptions ; return bot . sendPhoto ( msg . chat . id , buffer , opt ) . then ( async ( m ) => { let ret ; if ( ! ( m instanceof Error ) ) { ret = await Utils . sendQueueResult ( bot , msg , await Utils . checkQueue ( bot , data , downloader , m ) ) ; await bot . deleteMessage ( m . chat . id , m . message_id . toString ( ) ) ; } return ret ; } ) . catch ( ( e ) => { logger . error ( e ) ; } ) ; } public constructor ( ) { this . init ( ) ; } public init ( ) { logger . DEBUG ( "Operations initialized" ) ; } } export { Operations } ;