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
3 changes: 2 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { printCtxFrom } from "@/utils/users"
import { linkAdminDashboard } from "./link-admin-dashboard"
import { management } from "./management"
import { moderation } from "./moderation"
import { pin } from "./pin"
import { report } from "./report"
import { search } from "./search"

Expand Down Expand Up @@ -136,4 +137,4 @@ export const commands = new ManagedCommands<Role, Context, TelemetryContextFlavo
)
},
})
.withCollection(linkAdminDashboard, report, search, management, moderation)
.withCollection(linkAdminDashboard, report, search, management, moderation, pin)
59 changes: 59 additions & 0 deletions src/commands/pin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { CommandsCollection } from "@/lib/managed-commands"
import { logger } from "@/logger"
import { fmt } from "@/utils/format"
import { ephemeral } from "@/utils/messages"
import type { Role } from "@/utils/types"

export const pin = new CommandsCollection<Role>()
.createCommand({
trigger: "pin",
description: "Pin a message in a group",
scope: "group",
reply: "required",
permissions: {
allowGroupAdmins: true,
allowedRoles: ["direttivo", "owner"],
},
handler: async ({ context, repliedTo }) => {
const member = await context.getChatMember(context.me.id)
if (member.status !== "administrator")
return await ephemeral(context.reply(fmt(({ n }) => n`❌ The bot is not an admin`)), 10_000)

if (!member.can_pin_messages)
return await ephemeral(
context.reply(fmt(({ n, code }) => n`❌ The bot is missing the ${code`Pin messages`} permission.`)),
10_000
)

const res = await context.pinChatMessage(repliedTo.message_id).catch(() => false)
if (!res) return await ephemeral(context.reply(fmt(({ n }) => n`❌ Cannot pin the message`)), 10_000)

await ephemeral(context.reply(fmt(({ n }) => n`✅ Message pinned`)), 10_000)
},
})
.createCommand({
trigger: "unpin",
description: "Unpin a message in a group",
scope: "group",
reply: "required",
permissions: {
allowGroupAdmins: true,
allowedRoles: ["direttivo", "owner"],
},
handler: async ({ context, repliedTo }) => {
const member = await context.getChatMember(context.me.id)
if (member.status !== "administrator")
return await ephemeral(context.reply(fmt(({ n }) => n`❌ The bot is not an admin`)), 10_000)

if (!member.can_pin_messages)
return await ephemeral(
context.reply(fmt(({ n, code }) => n`❌ The bot is missing the ${code`Pin messages`} permission.`)),
10_000
)

const res = await context.unpinChatMessage(repliedTo.message_id).catch(() => false)
if (!res) return await ephemeral(context.reply(fmt(({ n }) => n`❌ Cannot unpin the message`)), 10_000)

await ephemeral(context.reply(fmt(({ n }) => n`✅ Message unpinned`)), 10_000)
},
})
Loading