|
| 1 | +import warn from "../actions/warn"; |
| 2 | +import ban from "../actions/ban"; |
| 3 | +import batchBan from "../actions/batchBan"; |
| 4 | +import { scheduleDeletion } from "../utils/tg"; |
| 5 | + |
| 6 | +import { config } from "../utils/config"; |
| 7 | +import { ContextExtensions } from "../typings/context"; |
| 8 | + |
| 9 | +const { |
| 10 | + warnInlineKeyboard, |
| 11 | + chats = {}, |
| 12 | + deleteWarnsAfter = false, |
| 13 | + deleteBansAfter = false, |
| 14 | +} = config; |
| 15 | + |
| 16 | +const normalisedDeleteWarnsAfter = |
| 17 | + typeof deleteWarnsAfter === "object" |
| 18 | + ? deleteWarnsAfter |
| 19 | + : { auto: deleteWarnsAfter, manual: deleteWarnsAfter }; |
| 20 | + |
| 21 | +const reply_markup = { inline_keyboard: warnInlineKeyboard }; |
| 22 | + |
| 23 | +export const extn: ContextExtensions = { |
| 24 | + async ban({ admin, reason, userToBan, msg }) { |
| 25 | + const banMessage = await ban({ admin, reason, userToBan }); |
| 26 | + |
| 27 | + const done = await this.loggedReply(banMessage, msg).then( |
| 28 | + scheduleDeletion(deleteBansAfter), |
| 29 | + ); |
| 30 | + |
| 31 | + if (msg) |
| 32 | + this.telegram |
| 33 | + .deleteMessage(msg.chat.id, msg.message_id) |
| 34 | + .catch(() => null); |
| 35 | + |
| 36 | + return done; |
| 37 | + }, |
| 38 | + |
| 39 | + async batchBan({ admin, reason, targets }) { |
| 40 | + const banMessage = await batchBan({ admin, reason, targets }); |
| 41 | + return this.loggedReply(banMessage).then(scheduleDeletion(deleteBansAfter)); |
| 42 | + }, |
| 43 | + |
| 44 | + async warn({ admin, amend, reason, userToWarn, mode, msg }) { |
| 45 | + const warnMessage = await warn({ admin, amend, reason, userToWarn }); |
| 46 | + |
| 47 | + const done = await this.loggedReply(warnMessage, msg, { |
| 48 | + reply_markup, |
| 49 | + }).then(scheduleDeletion(normalisedDeleteWarnsAfter[mode])); |
| 50 | + |
| 51 | + if (msg) |
| 52 | + this.telegram |
| 53 | + .deleteMessage(msg.chat.id, msg.message_id) |
| 54 | + .catch(() => null); |
| 55 | + |
| 56 | + return done; |
| 57 | + }, |
| 58 | + |
| 59 | + async loggedReply(html, reply, extra) { |
| 60 | + if (chats.adminLog) { |
| 61 | + const msg = |
| 62 | + reply && |
| 63 | + (await this.telegram.forwardMessage( |
| 64 | + chats.adminLog, |
| 65 | + reply.chat.id, |
| 66 | + reply.message_id, |
| 67 | + )); |
| 68 | + this.telegram |
| 69 | + // @ts-expect-error sendMessage is monkeypatched to accept TgHtml |
| 70 | + .sendMessage(chats.adminLog, html, { |
| 71 | + parse_mode: "HTML", |
| 72 | + reply_to_message_id: msg?.message_id, |
| 73 | + }) |
| 74 | + .catch(() => null); |
| 75 | + } |
| 76 | + // @ts-expect-error sendMessage is monkeypatched to accept TgHtml |
| 77 | + return this.replyWithHTML(html, extra); |
| 78 | + }, |
| 79 | +}; |
0 commit comments