File size: 9,604 Bytes
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11170eb
ca4eb6d
 
 
 
11170eb
ca4eb6d
11170eb
ca4eb6d
 
11170eb
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11170eb
ca4eb6d
 
11170eb
ca4eb6d
11170eb
ca4eb6d
11170eb
ca4eb6d
 
11170eb
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
709df35
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11170eb
ca4eb6d
11170eb
ca4eb6d
11170eb
ca4eb6d
 
11170eb
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
11170eb
ca4eb6d
 
 
 
 
 
 
11170eb
ca4eb6d
 
 
11170eb
ca4eb6d
11170eb
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11170eb
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
11170eb
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9da6fea
709df35
8e6421c
709df35
 
9da6fea
 
c664d1d
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from html import escape as escape_html

from pyrogram.errors import ChatAdminRequired, RightForbidden, RPCError
from pyrogram.filters import regex
from pyrogram.types import CallbackQuery, Message

from Powers import LOGGER, SUPPORT_GROUP
from Powers.bot_class import Gojo
from Powers.database.pins_db import Pins
from Powers.utils.custom_filters import admin_filter, command
from Powers.utils.kbhelpers import ikb
from Powers.utils.string import build_keyboard, parse_button


@Gojo.on_message(command("pin") & admin_filter)
async def pin_message(_, m: Message):
    pin_args = m.text.split(None, 1)
    if m.reply_to_message:
        try:
            disable_notification = True

            if len(pin_args) >= 2 and pin_args[1] in ["alert", "notify", "loud"]:
                disable_notification = False

            await m.reply_to_message.pin(
                disable_notification=disable_notification,
            )
            LOGGER.info(
                f"{m.from_user.id} pinned msgid-{m.reply_to_message.message_id} in {m.chat.id}",
            )

            if m.chat.username:
                # If chat has a username, use this format
                link_chat_id = m.chat.username
                message_link = (
                    f"https://t.me/{link_chat_id}/{m.reply_to_message.message_id}"
                )
            elif (str(m.chat.id)).startswith("-100"):
                # If chat does not have a username, use this
                link_chat_id = (str(m.chat.id)).replace("-100", "")
                message_link = (
                    f"https://t.me/c/{link_chat_id}/{m.reply_to_message.message_id}"
                )
            await m.reply_text(
                text=f"I have Pinned [this message]({message_link})!",
                disable_web_page_preview=True,
            )

        except ChatAdminRequired:
            await m.reply_text(text="I'm not admin or I don't have rights.")
        except RightForbidden:
            await m.reply_text(text="I don't have enough rights to pin messages.")
        except RPCError as ef:
            await m.reply_text(
                text=f"""Some error occured, report to @{SUPPORT_GROUP}

      <b>Error:</b> <code>{ef}</code>"""
            )
            LOGGER.error(ef)
    else:
        await m.reply_text("Reply to a message to pin it!")

    return


@Gojo.on_message(command("unpin") & admin_filter)
async def unpin_message(c: Gojo, m: Message):
    try:
        if m.reply_to_message:
            await c.unpin_chat_message(m.chat.id, m.reply_to_message.message_id)
            LOGGER.info(
                f"{m.from_user.id} unpinned msgid: {m.reply_to_message.message_id} in {m.chat.id}",
            )
            await m.reply_text(text="Unpinned last message.")
        else:
            await c.unpin_chat_message(m.chat.id)
            await m.reply_text(text="Unpinned last pinned message!")
    except ChatAdminRequired:
        await m.reply_text(text="I'm not admin or I don't have rights.")
    except RightForbidden:
        await m.reply_text(text="I don't have enough rights to unpin messages.")
    except RPCError as ef:
        await m.reply_text(
            text=f"""Some error occured, report to @{SUPPORT_GROUP}

      <b>Error:</b> <code>{ef}</code>"""
        )
        LOGGER.error(ef)

    return


@Gojo.on_message(command("unpinall") & admin_filter)
async def unpinall_message(_, m: Message):
    await m.reply_text(
        "Do you really want to unpin all messages in this chat?",
        reply_markup=ikb([[("Yes", "unpin all in this chat"), ("No", "close_admin")]]),
    )
    return


@Gojo.on_callback_query(regex("^unpin_all_in_this_chat$"))
async def unpinall_calllback(c: Gojo, q: CallbackQuery):
    user_id = q.from_user.id
    user_status = (await q.message.chat.get_member(user_id)).status
    if user_status not in {"creator", "administrator"}:
        await q.answer(
            "You're not even an admin, don't try this explosive shit!",
            show_alert=True,
        )
        return
    if user_status != "creator":
        await q.answer(
            "You're just an admin, not owner\nStay in your limits!",
            show_alert=True,
        )
        return
    try:
        await c.unpin_all_chat_messages(q.message.chat.id)
        LOGGER.info(f"{q.from_user.id} unpinned all messages in {q.message.chat.id}")
        await q.message.edit_text(text="Unpinned all messages in this chat.")
    except ChatAdminRequired:
        await q.message.edit_text(text="I'm not admin or I don't have rights.")
    except RightForbidden:
        await q.message.edit_text(text="I don't have enough rights to unpin messages.")
    except RPCError as ef:
        await q.message.edit_text(
           text=f"""Some error occured, report to @{SUPPORT_GROUP}

      <b>Error:</b> <code>{ef}</code>"""
        )
        LOGGER.error(ef)
    return


@Gojo.on_message(command("antichannelpin") & admin_filter)
async def anti_channel_pin(_, m: Message):
    pinsdb = Pins(m.chat.id)

    if len(m.text.split()) == 1:
        status = pinsdb.get_settings()["antichannelpin"]
        await m.reply_text(
            text=f"Current AntiChannelPin status: {status}"
        )
        return

    if len(m.text.split()) == 2:
        if m.command[1] in ("yes", "on", "true"):
            pinsdb.antichannelpin_on()
            LOGGER.info(f"{m.from_user.id} enabled antichannelpin in {m.chat.id}")
            msg = "Turned on AntiChannelPin, now all message pinned by channel will be unpinned automtically!"
        elif m.command[1] in ("no", "off", "false"):
            pinsdb.antichannelpin_off()
            LOGGER.info(f"{m.from_user.id} disabled antichannelpin in {m.chat.id}")
            msg = "Turned off AntiChannelPin, now all message pinned by channel will stay pinned!"
        else:
            await m.reply_text(text="Please check help on how to use this this command.")
            return

    await m.reply_text(msg)
    return


@Gojo.on_message(command("pinned") & admin_filter)
async def pinned_message(c: Gojo, m: Message):
    chat_title = m.chat.title
    chat = await c.get_chat(chat_id=m.chat.id)
    msg_id = m.reply_to_message.message_id if m.reply_to_message else m.message_id

    if chat.pinned_message:
        pinned_id = chat.pinned_message.message_id
        if m.chat.username:
            link_chat_id = m.chat.username
            message_link = f"https://t.me/{link_chat_id}/{pinned_id}"
        elif (str(m.chat.id)).startswith("-100"):
            link_chat_id = (str(m.chat.id)).replace("-100", "")
            message_link = f"https://t.me/c/{link_chat_id}/{pinned_id}"

        await m.reply_text(
            f"The pinned message of {escape_html(chat_title)} is [here]({message_link}).",
            reply_to_message_id=msg_id,
            disable_web_page_preview=True,
        )
    else:
        await m.reply_text(f"There is no pinned message in {escape_html(chat_title)}.")


@Gojo.on_message(command("cleanlinked") & admin_filter)
async def clean_linked(_, m: Message):
    pinsdb = Pins(m.chat.id)

    if len(m.text.split()) == 1:
        status = pinsdb.get_settings()["cleanlinked"]
        await m.reply_text(
           text=f"Current AntiChannelPin status: {status}"
        )
        return

    if len(m.text.split()) == 2:
        if m.command[1] in ("yes", "on", "true"):
            pinsdb.cleanlinked_on()
            LOGGER.info(f"{m.from_user.id} enabled CleanLinked in {m.chat.id}")
            msg = "Turned on CleanLinked! Now all the messages from linked channel will be deleted!"
        elif m.command[1] in ("no", "off", "false"):
            pinsdb.cleanlinked_off()
            LOGGER.info(f"{m.from_user.id} disabled CleanLinked in {m.chat.id}")
            msg = "Turned off CleanLinked! Messages from linked channel will not be deleted!"
        else:
            await m.reply_text(text="Please check help on how to use this this command.")
            return

    await m.reply_text(msg)
    return


@Gojo.on_message(command("permapin") & admin_filter)
async def perma_pin(_, m: Message):
    if m.reply_to_message or len(m.text.split()) > 1:
        LOGGER.info(f"{m.from_user.id} used permampin in {m.chat.id}")
        if m.reply_to_message:
            text = m.reply_to_message.text
        elif len(m.text.split()) > 1:
            text = m.text.split(None, 1)[1]
        teks, button = await parse_button(text)
        button = await build_keyboard(button)
        button = ikb(button) if button else None
        z = await m.reply_text(teks, reply_markup=button)
        await z.pin()
    else:
        await m.reply_text("Reply to a message or enter text to pin it.")
    await m.delete()
    return


__PLUGIN__ = "pins"

__alt_name__ = ["pin", "unpin"]

__HELP__ = """
***Pin***

Here you find find all help related to groups pins and how to manage them via me.

**Admin Cmds:**
* /pin: Silently pins the message replied to - add `loud`, `notify` or `alert` to give notificaton to users.
* /unpin: Unpins the last pinned message.
* /pinned: Gives the current pinned message of the chat.
* /unpinall: Unpins all the pinned message in the current chat.
* /antichannelpin `<on/off/yes/no>`: Toggle antichannelpin status. All the messages from linked channel will be unpinned if enabled!
* /cleanlinked `<on/off/yes/no>`: Toggle cleanlinked status. All the messages from linked channel will be deleted if enabled!
* /permapin `<text>`: Pin a custom messages via bot. This message can contain markdown, and can be used in replies to the media include additional buttons and text."""