Skip to content

Commit

Permalink
Admin log fixes
Browse files Browse the repository at this point in the history
Add adminLog to example config
Also log messages that were being replied to in ban/warn/report
  • Loading branch information
C0rn3j committed Sep 19, 2023
1 parent e3a0cc3 commit 27dc777
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bot/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ module.exports = {

loggedReply(html, extra) {
if (chats.adminLog) {
this.tg
this.telegram
.sendMessage(
chats.adminLog,
html.toJSON().replace(/\[<code>(\d+)<\/code>\]/g, '[#u$1]'),
html,
{ parse_mode: 'HTML' },
)
.catch(() => null);
Expand Down
6 changes: 6 additions & 0 deletions example.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ const config = {


chats: {
/**
* @type {(number | false)}
* Chat to send member (un)ban/(un)warn/report notifications and relevant messages to.
* Pass false to disable this feature.
*/
adminLog: false,

/**
* @type {(number | false)}
Expand Down
4 changes: 4 additions & 0 deletions handlers/commands/ban.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { displayUser, scheduleDeletion } = require('../../utils/tg');
const { html } = require('../../utils/html');
const { parse, strip, substom } = require('../../utils/cmd');
const { chats = {} } = require('../../utils/config').config;

// Bot

Expand Down Expand Up @@ -53,6 +54,9 @@ const banHandler = async (ctx) => {
}

if (ctx.message.reply_to_message) {
if (chats.adminLog) {
await ctx.telegram.forwardMessage(chats.adminLog, ctx.message.chat.id, ctx.message.reply_to_message.message_id)
}
ctx.deleteMessage(ctx.message.reply_to_message.message_id)
.catch(() => null);
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/commands/leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const leaveCommandHandler = async (ctx: ExtendedContext) => {
const group = query
? await managesGroup(
/^-?\d+/.test(query) ? { id: +query } : { title: query }
)
)
: ctx.chat;
if (!group) {
return ctx.replyWithHTML("❓ <b>Unknown group.</b>");
Expand Down
16 changes: 14 additions & 2 deletions handlers/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Utils
const Cmd = require('../../utils/cmd');
const { TgHtml } = require('../../utils/html');
const { chats = {} } = require('../../utils/config').config;
const {
link,
msgLink,
scheduleDeletion,
} = require('../../utils/tg');

const { chats = {} } = require('../../utils/config').config;

const isQualified = member => member.status === 'creator' ||
member.can_delete_messages &&
Expand All @@ -27,7 +27,7 @@ const reportHandler = async ctx => {
if (!ctx.message.reply_to_message) {
await ctx.deleteMessage();
return ctx.replyWithHTML(
'ℹ️ <b>Reply to message you\'d like to report</b>',
'ℹ️ <b>Reply to the message you\'d like to report</b>',
).then(scheduleDeletion());
}
const admins = (await ctx.getChatAdministrators())
Expand Down Expand Up @@ -60,6 +60,18 @@ const reportHandler = async ctx => {
} ] ] } },
);
}
if (chats.adminLog) {
await ctx.telegram.forwardMessage(chats.adminLog, ctx.message.chat.id, ctx.message.reply_to_message.message_id)
await ctx.telegram.sendMessage(
chats.adminLog,
TgHtml.tag`❗️ ${link(ctx.from)} reported <a href="${msgLink(
ctx.message.reply_to_message,
)}">a message</a> from ${link(ctx.message.reply_to_message.from)} in ${ctx.chat.title}!`,
{
parse_mode: 'HTML'
},
);
}
return null;
};

Expand Down
4 changes: 4 additions & 0 deletions handlers/commands/warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Utils
const { parse, strip, substom } = require('../../utils/cmd');
const { scheduleDeletion } = require('../../utils/tg');
const { chats = {} } = require('../../utils/config').config;

// DB
const { getUser } = require('../../stores/user');
Expand Down Expand Up @@ -46,6 +47,9 @@ const warnHandler = async (ctx) => {
}

if (ctx.message.reply_to_message) {
if (chats.adminLog) {
await ctx.telegram.forwardMessage(chats.adminLog, ctx.message.chat.id, ctx.message.reply_to_message.message_id)
}
ctx.deleteMessage(ctx.message.reply_to_message.message_id)
.catch(() => null);
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/middlewares/commandButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = (ctx, next) => {
};

/** @type { import('../../typings/context').ExtendedContext } */
const cbCtx = new Context(cbUpdate, ctx.tg, ctx.options);
const cbCtx = new Context(cbUpdate, ctx.telegram, ctx.options);
Object.assign(cbCtx, contextCustomizations);
cbCtx.botInfo = ctx.botInfo;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "the_guard_bot",
"version": "1.5.0",
"version": "1.5.1",
"description": "Telegram guard bot to manage a network of groups.",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion typings/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export interface Config {
chats?: {
/**
* Chat to log all admin actions to.
* Pass false to disable this feature.
*/
adminLog?: number;
adminLog?: number | false;

/**
* Chat to send member join/leave notifications to.
Expand Down

0 comments on commit 27dc777

Please sign in to comment.