Karma commited on
Commit
0056fc3
·
2 Parent(s): 5fda167 4917dfa

Merge pull request #47 from Infamous-Hydra/Infamous-Hydra-patch-3

Browse files
Files changed (1) hide show
  1. Mikobot/plugins/fsub.py +189 -0
Mikobot/plugins/fsub.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # <============================================== IMPORTS =========================================================>
2
+ from telethon import Button, events, types
3
+ from telethon.errors import ChatAdminRequiredError, UserNotParticipantError
4
+ from telethon.tl.functions.channels import GetParticipantRequest
5
+
6
+ from Database.mongodb import fsub_db as db
7
+ from Mikobot import BOT_ID
8
+ from Mikobot import DRAGONS as DEVS
9
+ from Mikobot import OWNER_ID, tbot
10
+ from Mikobot.events import register
11
+
12
+ # <=======================================================================================================>
13
+
14
+ # Constants
15
+ F_SUBSCRIBE_COMMAND = "/(fsub|Fsub|forcesubscribe|Forcesub|forcesub|Forcesubscribe)"
16
+ FORCESUBSCRIBE_ON = ["on", "yes", "y"]
17
+ FORCESUBSCRIBE_OFF = ["off", "no", "n"]
18
+
19
+
20
+ # <================================================ FUNCTION =======================================================>
21
+ def fsk_ck(**args):
22
+ def decorator(func):
23
+ tbot.add_event_handler(func, events.CallbackQuery(**args))
24
+ return func
25
+
26
+ return decorator
27
+
28
+
29
+ # Helper functions
30
+ async def is_admin(chat_id, user_id):
31
+ try:
32
+ p = await tbot(GetParticipantRequest(chat_id, user_id))
33
+ except UserNotParticipantError:
34
+ return False
35
+ return isinstance(
36
+ p.participant, (types.ChannelParticipantAdmin, types.ChannelParticipantCreator)
37
+ )
38
+
39
+
40
+ async def participant_check(channel, user_id):
41
+ try:
42
+ await tbot(GetParticipantRequest(channel, int(user_id)))
43
+ return True
44
+ except UserNotParticipantError:
45
+ return False
46
+ except Exception:
47
+ return False
48
+
49
+
50
+ # Main command function
51
+ @register(pattern=f"^{F_SUBSCRIBE_COMMAND} ?(.*)")
52
+ async def force_subscribe(event):
53
+ """Handle the force subscribe command."""
54
+ if event.is_private:
55
+ return
56
+
57
+ if event.is_group:
58
+ perm = await event.client.get_permissions(event.chat_id, event.sender_id)
59
+ if not perm.is_admin:
60
+ return await event.reply("You need to be an admin to do this.")
61
+
62
+ if not perm.is_creator:
63
+ return await event.reply(
64
+ "❗ Group creator required\nYou have to be the group creator to do that."
65
+ )
66
+
67
+ try:
68
+ channel = event.text.split(None, 1)[1]
69
+ except IndexError:
70
+ channel = None
71
+
72
+ if not channel:
73
+ chat_db = db.fs_settings(event.chat_id)
74
+ if not chat_db:
75
+ await event.reply("Force subscribe is disabled in this chat.")
76
+ else:
77
+ await event.reply(
78
+ f"Force subscribe is currently enabled. Users are forced to join @{chat_db.channel} to speak here."
79
+ )
80
+ elif channel.lower() in FORCESUBSCRIBE_ON:
81
+ await event.reply("Please specify the channel username.")
82
+ elif channel.lower() in FORCESUBSCRIBE_OFF:
83
+ await event.reply("**Force subscribe is disabled successfully.**")
84
+ db.disapprove(event.chat_id)
85
+ else:
86
+ try:
87
+ channel_entity = await event.client.get_entity(channel)
88
+ except:
89
+ return await event.reply("Invalid channel username provided.")
90
+
91
+ channel = channel_entity.username
92
+ try:
93
+ if not channel_entity.broadcast:
94
+ return await event.reply("That's not a valid channel.")
95
+ except:
96
+ return await event.reply("That's not a valid channel.")
97
+
98
+ if not await participant_check(channel, BOT_ID):
99
+ return await event.reply(
100
+ f"**Not an admin in the channel**\nI am not an admin in the [channel](https://t.me/{channel}). Add me as an admin to enable force subscribe.",
101
+ link_preview=False,
102
+ )
103
+
104
+ db.add_channel(event.chat_id, str(channel))
105
+ await event.reply(f"Force subscribe is enabled to @{channel}.")
106
+
107
+
108
+ # Event handler for new messages
109
+ @tbot.on(events.NewMessage())
110
+ async def force_subscribe_new_message(e):
111
+ """Handle new messages for force subscribe."""
112
+ if not db.fs_settings(e.chat_id):
113
+ return
114
+
115
+ if e.is_private or not e.from_id or e.sender_id in DEVS or e.sender_id == OWNER_ID:
116
+ return
117
+
118
+ if not e.chat.admin_rights or not e.chat.admin_rights.ban_users:
119
+ return
120
+
121
+ try:
122
+ channel = db.fs_settings(e.chat_id)["channel"]
123
+ check = await participant_check(channel, e.sender_id)
124
+ except (ChatAdminRequiredError, UserNotParticipantError):
125
+ return
126
+
127
+ if not check:
128
+ buttons = [
129
+ Button.url("Join Channel", f"t.me/{channel}"),
130
+ Button.inline("Unmute Me", data=f"fs_{e.sender_id}"),
131
+ ]
132
+
133
+ txt = f'<b><a href="tg://user?id={e.sender_id}">{e.sender.first_name}</a></b>, you have <b>not subscribed</b> to our <b><a href="t.me/{channel}">channel</a></b> yet. Please <b><a href="t.me/{channel}">join</a></b> and press the button below to unmute yourself.'
134
+ await e.reply(txt, buttons=buttons, parse_mode="html", link_preview=False)
135
+ await e.client.edit_permissions(e.chat_id, e.sender_id, send_messages=False)
136
+
137
+
138
+ # Inline query handler
139
+ @fsk_ck(pattern=r"fs(\_(.*))")
140
+ async def unmute_force_subscribe(event):
141
+ """Handle inline query for unmuting force subscribe."""
142
+ user_id = int(((event.pattern_match.group(1)).decode()).split("_", 1)[1])
143
+
144
+ if not event.sender_id == user_id:
145
+ return await event.answer("This is not meant for you.", alert=True)
146
+
147
+ channel = db.fs_settings(event.chat_id)["channel"]
148
+ try:
149
+ check = await participant_check(channel, user_id)
150
+ except ChatAdminRequiredError:
151
+ check = False
152
+ return
153
+
154
+ if not check:
155
+ return await event.answer(
156
+ "You have to join the channel first, to get unmuted!", alert=True
157
+ )
158
+
159
+ try:
160
+ await event.client.edit_permissions(event.chat_id, user_id, send_messages=True)
161
+ except ChatAdminRequiredError:
162
+ pass
163
+
164
+ await event.delete()
165
+
166
+
167
+ # <=================================================== HELP ====================================================>
168
+
169
+
170
+ __help__ = """
171
+ ➠ *Dazai has the capability to hush members who haven't yet subscribed to your channel until they decide to hit that subscribe button.*
172
+ ➠ *When activated, I'll silence those who are not subscribed and provide them with an option to unmute. Once they click the button, I'll lift the mute.*
173
+
174
+ ➠ Configuration Process
175
+ ➠ Exclusively for Creators
176
+ ➠ Grant me admin privileges in your group
177
+ ➠ Designate me as an admin in your channel
178
+
179
+ ➠ *Commands*
180
+ » /fsub channel_username - to initiate and customize settings for the channel.
181
+
182
+ ➠ *Kick things off with...*
183
+ » /fsub - to review the current settings.
184
+ » /fsub off - to deactivate the force subscription feature.
185
+
186
+ ➠ *If you disable fsub, you'll need to set it up again for it to take effect. Utilize* /fsub channel_username.
187
+ """
188
+ __mod_name__ = "F-SUB"
189
+ # <================================================ END =======================================================>