Skip to content

Commit

Permalink
add /pin, /unpin & /unpinall commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukaizen committed Mar 11, 2024
1 parent 6923f09 commit 494a6df
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/helpers/helper_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function ownerOnlyCallback(handler: any) {
await handler(ctx);
}
else {
await ctx.answerCallbackQuery({ text: "Only owner of this chat can use this command!"});
await ctx.answerCallbackQuery({ text: "Only owner of this chat can use this button!"});
}
};
}
Expand Down Expand Up @@ -69,7 +69,7 @@ export function elevatedUsersCallbackOnly(handler: any) {
await handler(ctx);
}
else {
await ctx.answerCallbackQuery({ text: "Only admins can use this command!"});
await ctx.answerCallbackQuery({ text: "Only admins can use this button!"});
}
};

Expand Down Expand Up @@ -218,6 +218,25 @@ export function canDeleteMessages(handler: any) {
}
}
}

export function canPinMessages(handler: any) {
return async (ctx: any) => {
let bot_id = ctx.me.id;
let chat_id = ctx.chat.id;
let bot_info = await ctx.api.getChatMember(chat_id, bot_id);
if (bot_info.status == "administrator") {
if (bot_info.can_pin_messages == true) {
await handler(ctx);
}
else {
await ctx.reply("I don't have enough admin rights to pin messages!", {reply_parameters: {message_id: ctx.message.message_id}});
}
}
else {
await ctx.reply("I need to be admin for this!", {reply_parameters: {message_id: ctx.message.message_id}});
}
}
}
// ====================================================

export async function extract_time(ctx: any, time_val: string): Promise<string | number> {
Expand Down
73 changes: 73 additions & 0 deletions src/modules/admin.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,75 @@
import bot from "../bot";
import { InlineKeyboard } from "grammy";
import { elevatedUsersOnly, canPinMessages, elevatedUsersCallbackOnly, userInfo} from "../helpers/helper_func";

bot.chatType("supergroup" || "group").command("pin", elevatedUsersOnly(canPinMessages(async(ctx: any) => {
let user_info = await userInfo(ctx);
if (user_info.can_pin_messages == false) {
await ctx.reply("You don't have enough rights to pin messages!", {reply_parameters: {message_id: ctx.message.message_id}});
return;
}
else {
if (ctx.message.reply_to_message == undefined) {
await ctx.reply("Reply to a message to pin it.", {reply_parameters: {message_id: ctx.message.message_id}});
return;
}

if (ctx.match == "silent" || ctx.match == "quiet" || ctx.match == "noalert") {
await ctx.api.pinChatMessage(ctx.chat.id, ctx.message.reply_to_message.message_id, {disable_notification: true});
}
else if (ctx.match == "alert" || ctx.match == "loud") {
await ctx.api.pinChatMessage(ctx.chat.id, ctx.message.reply_to_message.message_id, {disable_notification: false});
}
else {
await ctx.api.pinChatMessage(ctx.chat.id, ctx.message.reply_to_message.message_id, {disable_notification: true});
}
}
})));

bot.chatType("supergroup" || "group").command("unpin", elevatedUsersOnly(canPinMessages(async(ctx: any) => {
let user_info = await userInfo(ctx);
if (user_info.can_pin_messages == false) {
await ctx.reply("You don't have enough rights to unpin messages!", {reply_parameters: {message_id: ctx.message.message_id}});
return;
}
else {
if (ctx.message.reply_to_message == undefined) {
await ctx.api.unpinChatMessage(ctx.chat.id)
.then(ctx.reply("Unpinned the most recent pinned message!", {reply_parameters: {message_id: ctx.message.message_id}}))
.catch((GrammyError: any) => {ctx.reply("Failed to unpin message: invalid message / message probably does not exist.")});
}
else {
await ctx.api.unpinChatMessage(ctx.chat.id, {message_id: ctx.message.reply_to_message.message_id})
.then(ctx.reply("Unpinned the message successfully!", {reply_parameters: {message_id: ctx.message.message_id}}))
.catch((GrammyError: any) => {ctx.reply("Failed to unpin message: invalid message / message probably does not exist.")});
}
}
})));


bot.chatType("supergroup" || "group").command("unpinall", elevatedUsersOnly(canPinMessages(async(ctx: any) => {
let user_info = await userInfo(ctx);
if (user_info.can_pin_messages == false) {
await ctx.reply("You don't have enough rights to unpin messages!", {reply_parameters: {message_id: ctx.message.message_id}});
return;
}
else {
let confirmUnpin = new InlineKeyboard()
.text("Yes", "yes-unpin")
.text("No", "no-unpin");

await ctx.api.sendMessage(ctx.chat.id, "Are you sure you want to unpin <b>ALL the pinned messages</b> in this chat?\n\n<i>This action cannot be undone.</i>", {reply_markup: confirmUnpin, reply_parameters: {message_id: ctx.message.message_id}, parse_mode: "HTML"});
}
})));

bot.callbackQuery("yes-unpin", elevatedUsersCallbackOnly(async(ctx: any) => {
await ctx.api.unpinAllChatMessages(ctx.chat.id)
.then(ctx.editMessageText("Unpinned all the messages successfully!"))
.catch((GrammyError: any) => {ctx.editMessageText("Failed to unpin messages: invalid message / message probably does not exist.")});
}));

bot.callbackQuery("no-unpin", elevatedUsersCallbackOnly(async(ctx: any) => {
await ctx.editMessageText("Okay fine. Tell me when you change your mind!", { parse_mode: "HTML" });
}));


0 comments on commit 494a6df

Please sign in to comment.