Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ _commandsBase
}

const res = await Moderation.ban(repliedTo.from, context.chat, context.from, null, [repliedTo], args.reason)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
.createCommand({
Expand Down Expand Up @@ -65,9 +67,11 @@ _commandsBase
[repliedTo],
args.reason
)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
.createCommand({
Expand Down Expand Up @@ -102,8 +106,10 @@ _commandsBase
}

const res = await Moderation.unban(user, context.chat, context.from)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
9 changes: 5 additions & 4 deletions src/commands/del.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ _commandsBase.createCommand({
})

const res = await Moderation.deleteMessages([repliedTo], context.from, "Command /del")
// TODO: better error and ok response
const msg = await context.reply(res.isErr() ? "Cannot delete the message" : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply("Cannot delete the message")
await wait(5000)
await msg.delete()
}
},
})
8 changes: 5 additions & 3 deletions src/commands/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ _commandsBase.createCommand({
}

const res = await Moderation.kick(repliedTo.from, context.chat, context.from, [repliedTo], args.reason)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
24 changes: 15 additions & 9 deletions src/commands/mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ _commandsBase
[repliedTo],
args.reason
)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
.createCommand({
Expand All @@ -65,9 +67,11 @@ _commandsBase
}

const res = await Moderation.mute(repliedTo.from, context.chat, context.from, null, [repliedTo], args.reason)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
.createCommand({
Expand Down Expand Up @@ -101,8 +105,10 @@ _commandsBase
}

const res = await Moderation.unmute(user, context.chat, context.from)
const msg = await context.reply(res.isErr() ? res.error.fmtError : "OK")
await wait(5000)
await msg.delete()
if (res.isErr()) {
const msg = await context.reply(res.error.fmtError)
await wait(5000)
await msg.delete()
}
},
})
12 changes: 11 additions & 1 deletion src/commands/report.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from "@/logger"
import { modules } from "@/modules"
import { fmt } from "@/utils/format"
import { _commandsBase } from "./_base"

_commandsBase.createCommand({
Expand All @@ -14,6 +15,15 @@ _commandsBase.createCommand({
return
}

await modules.get("tgLogger").report(repliedTo, context.from)
const reportSent = await modules.get("tgLogger").report(repliedTo, context.from)
await context.reply(
reportSent
? fmt(({ b, n }) => [b`✅ Message reported!`, n`Moderators have been notified.`], { sep: "\n" })
: fmt(({ b, n }) => [b`⚠️ Report not sent`, n`Please try again in a moment.`], { sep: "\n" }),
{
disable_notification: false,
reply_parameters: { message_id: repliedTo.message_id },
}
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
},
})
1 change: 0 additions & 1 deletion src/modules/moderation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const MAP_ACTIONS: Record<
MUTE_ALL: "mute_all",
}

// TODO: missing in-channel user feedback (eg. <user> has been muted by <admin>...)
class ModerationClass<C extends Context> implements MiddlewareObj<C> {
private composer = new Composer<C>()
private static instance: ModerationClass<Context> | null = null
Expand Down
35 changes: 35 additions & 0 deletions src/modules/tg-logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class TgLogger extends Module<ModuleShared> {
? new InlineKeyboard().url("See Deleted Message", props.preDeleteRes.link)
: undefined
await this.log(isAutoModeration ? this.topics.autoModeration : this.topics.adminActions, mainMsg, { reply_markup })
if (!isAutoModeration) await this.logModActionInChat(props)
return mainMsg
}

Expand Down Expand Up @@ -498,4 +499,38 @@ export class TgLogger extends Module<ModuleShared> {
await this.log(this.topics.exceptions, msg)
return msg
}

private async logModActionInChat(p: ModerationAction): Promise<void> {
if (
p.action !== "BAN" &&
p.action !== "KICK" &&
p.action !== "MUTE" &&
p.action !== "UNBAN" &&
p.action !== "UNMUTE"
)
return

const msg = fmt(
({ b, n, skip }) => [
skip`${MOD_ACTION_TITLE(p)}`,
n`${b`Target:`} ${fmtUser(p.target, false)}`,
n`${b`Moderator:`} ${fmtUser(p.from, false)}`,
"duration" in p && p.duration ? n`${b`Duration:`} ${p.duration.raw} (until ${p.duration.dateStr})` : undefined,
"reason" in p && p.reason ? n`${b`Reason:`} ${p.reason}` : undefined,
],
Comment thread
toto04 marked this conversation as resolved.
{ sep: "\n" }
)

await this.shared.api
.sendMessage(p.chat.id, msg, {
disable_notification: false,
link_preview_options: { is_disabled: true },
})
.catch((error: unknown) => {
logger.warn(
{ error, action: p.action },
"[Moderation:logActionInChat] Failed to post moderation action in chat"
)
})
}
}
9 changes: 7 additions & 2 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ export function fmt(cb: (formatters: Formatters) => string | (string | undefined
)
}

export function fmtUser(user: Partial<Pick<User, "id" | "first_name" | "last_name">> & { id: number }): string {
export function fmtUser(
user: Partial<Pick<User, "id" | "first_name" | "last_name">> & { id: number },
showId: boolean = true
): string {
const fullname = user.last_name ? `${user.first_name} ${user.last_name}` : user.first_name
return formatters.n`${formatters.link(fullname ?? "[no-name]", `tg://user?id=${user.id}`)} [${formatters.code`${user.id}`}]`
return showId
? formatters.n`${formatters.link(fullname ?? "[no-name]", `tg://user?id=${user.id}`)} [${formatters.code`${user.id}`}]`
: formatters.n`${formatters.link(fullname ?? "[no-name]", `tg://user?id=${user.id}`)}`
}

export function fmtChat(chat: Chat, inviteLink?: string): string {
Expand Down
Loading