2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-22 18:08:51 +00:00
the-guard-bot/bot/context.ts

84 lines
2.2 KiB
TypeScript
Raw Normal View History

import warn from "../actions/warn";
import ban from "../actions/ban";
import batchBan from "../actions/batchBan";
import { scheduleDeletion } from "../utils/tg";
2018-05-04 14:21:51 +02:00
import { config } from "../utils/config";
import { ContextExtensions } from "../typings/context";
2018-05-04 14:21:51 +02:00
const {
warnInlineKeyboard,
2020-05-14 23:18:25 +02:00
chats = {},
deleteWarnsAfter = false,
deleteBansAfter = false,
} = config;
2018-05-04 14:21:51 +02:00
const normalisedDeleteWarnsAfter =
typeof deleteWarnsAfter === "object"
? deleteWarnsAfter
: { auto: deleteWarnsAfter, manual: deleteWarnsAfter };
const reply_markup = { inline_keyboard: warnInlineKeyboard };
2018-05-04 14:21:51 +02:00
export const extn: ContextExtensions = {
async ban({ admin, reason, userToBan, msg }) {
const banMessage = await ban({ admin, reason, userToBan });
2023-09-20 02:15:12 +05:30
const done = await this.loggedReply(banMessage, msg).then(
scheduleDeletion(deleteBansAfter),
);
if (msg)
this.telegram
.deleteMessage(msg.chat.id, msg.message_id)
.catch(() => null);
return done;
},
2020-05-14 23:18:25 +02:00
async batchBan({ admin, reason, targets }) {
const banMessage = await batchBan({ admin, reason, targets });
return this.loggedReply(banMessage).then(scheduleDeletion(deleteBansAfter));
2019-01-31 20:19:39 +01:00
},
async warn({ admin, amend, reason, userToWarn, mode, msg }) {
2019-05-31 17:02:00 +02:00
const warnMessage = await warn({ admin, amend, reason, userToWarn });
2023-09-20 02:15:12 +05:30
const done = await this.loggedReply(warnMessage, msg, {
reply_markup,
}).then(scheduleDeletion(normalisedDeleteWarnsAfter[mode]));
if (msg)
this.telegram
.deleteMessage(msg.chat.id, msg.message_id)
.catch(() => null);
return done;
2018-05-04 14:21:51 +02:00
},
async loggedReply(html, reply, extra) {
2020-05-14 23:18:25 +02:00
if (chats.adminLog) {
const msg =
reply &&
(await this.telegram.forwardMessage(
2020-05-14 23:18:25 +02:00
chats.adminLog,
reply.chat.id,
2023-09-20 02:15:12 +05:30
reply.message_id,
));
this.telegram
// @ts-expect-error sendMessage is monkeypatched to accept TgHtml
.sendMessage(chats.adminLog, html, {
parse_mode: "HTML",
reply_to_message_id: msg?.message_id,
})
2020-05-14 23:18:25 +02:00
.catch(() => null);
}
// @ts-expect-error sendMessage is monkeypatched to accept TgHtml
2020-05-14 23:18:25 +02:00
return this.replyWithHTML(html, extra);
},
replyWithCopy(content, options) {
return this.telegram.sendCopy(this.chat.id, content, options);
},
2018-05-04 14:21:51 +02:00
};