diff --git a/src/commands/index.ts b/src/commands/index.ts index 6d05682..6da4994 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -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" @@ -136,4 +137,4 @@ export const commands = new ManagedCommands() + .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) + }, + })