forked from TeamUltroid/Ultroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmpermit.py
More file actions
307 lines (284 loc) · 11.4 KB
/
pmpermit.py
File metadata and controls
307 lines (284 loc) · 11.4 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Ultroid - UserBot
# Copyright (C) 2020 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
• `{i}a` or `{i}approve`
To Approve Someone In PM.
• `{i}da` or `{i}disapprove`
To Disapprove Someone In PM.
• `{i}block`
To Block Someone in PM.
• `{i}unblock`
To Unblock Someone in PM.
"""
from pyUltroid.functions.pmpermit_db import *
from telethon import events
from telethon.tl.functions.contacts import BlockRequest, UnblockRequest
from telethon.tl.functions.messages import ReportSpamRequest
from . import *
# ========================= CONSTANTS =============================
COUNT_PM = {}
LASTMSG = {}
if Redis("PMPIC"):
PMPIC = Redis("PMPIC")
else:
PMPIC = "https://telegra.ph/file/94f6a4aeb21ce2d58dd41.jpg"
if not Redis("PM_TEXT"):
UNAPPROVED_MSG = """
**PMSecurity of {}!**
Please wait for me to respnd or you will be blocked and reported as spam!!
You have {}/{} warnings!"""
else:
UNAPPROVED_MSG = (
"""
**PMSecurity of {}!**
"""
f"""{Redis("PM_TEXT")}"""
"""
Please wait for me to respnd or you will be blocked and reported as spam!!
You have {}/{} warnings!"""
)
UND = "Please wait for me to respnd or you will be blocked and reported as spam!!"
UNS = "You were spamming my Master's PM, which I didn't like."
WARNS = 3
NO_REPLY = "Reply to someone's msg or try this commmand in private."
PMCMDS = [
f"{hndlr}a",
f"{hndlr}approve",
f"{hndlr}da",
f"{hndlr}disapprove",
f"{hndlr}block",
f"{hndlr}unblock",
]
# =================================================================
sett = udB.get("PMSETTING")
if sett is None:
sett = True
if sett == "True" and sett != "False":
@ultroid_bot.on(events.NewMessage(outgoing=True, func=lambda e: e.is_private))
async def autoappr(e):
miss = await e.get_chat()
if miss.bot or miss.is_self or miss.verified:
return
mssg = e.text
if mssg in PMCMDS: # do not approve if outgoing is a command.
return
if not is_approved(e.chat_id):
approve_user(e.chat_id)
async for message in e.client.iter_messages(e.chat_id, search=UND):
await message.delete()
async for message in e.client.iter_messages(e.chat_id, search=UNS):
await message.delete()
if Var.LOG_CHANNEL:
name = await e.client.get_entity(e.chat_id)
name0 = str(name.first_name)
await e.client.send_message(
Var.LOG_CHANNEL,
f"#AutoApproved\nUser - [{name0}](tg://user?id={e.chat_id})",
)
@ultroid_bot.on(events.NewMessage(incoming=True, func=lambda e: e.is_private))
async def permitpm(event):
user = await event.get_chat()
if user.bot or user.is_self or user.verified:
return
apprv = is_approved(user.id)
if not apprv and event.text != UND:
try:
wrn = COUNT_PM[user.id]
except KeyError:
wrn = 0
if user.id in LASTMSG:
prevmsg = LASTMSG[user.id]
if event.text != prevmsg:
async for message in event.client.iter_messages(
user.id, search=UND
):
await message.delete()
async for message in event.client.iter_messages(
user.id, search=UNS
):
await message.delete()
await event.client.send_file(
user.id,
PMPIC,
caption=UNAPPROVED_MSG.format(OWNER_NAME, wrn, WARNS),
)
elif event.text == prevmsg:
async for message in event.client.iter_messages(
user.id, search=UND
):
await message.delete()
await event.client.send_file(
user.id,
PMPIC,
caption=UNAPPROVED_MSG.format(OWNER_NAME, wrn, WARNS),
)
LASTMSG.update({user.id: event.text})
else:
await event.client.send_file(
user.id,
PMPIC,
caption=UNAPPROVED_MSG.format(OWNER_NAME, wrn, WARNS),
)
LASTMSG.update({user.id: event.text})
if user.id not in COUNT_PM:
COUNT_PM.update({user.id: 1})
else:
COUNT_PM[user.id] = COUNT_PM[user.id] + 1
if COUNT_PM[user.id] > WARNS:
await event.respond(
"`You were spamming my Master's PM, which I didn't like.`\n`You have been BLOCKED and reported as SPAM, until further notice.`"
)
try:
del COUNT_PM[user.id]
del LASTMSG[user.id]
except KeyError:
if Var.LOG_CHANNEL:
await event.client.send_message(
Var.LOG_CHANNEL,
"PMPermit is messed! Pls restart the bot!!",
)
return LOGS.info("COUNT_PM is messed.")
await event.client(BlockRequest(user.id))
await event.client(ReportSpamRequest(peer=user.id))
if Var.LOG_CHANNEL:
name = await event.client.get_entity(user.id)
name0 = str(name.first_name)
await event.client.send_message(
Var.LOG_CHANNEL,
f"[{name0}](tg://user?id={user.id}) was blocked for spamming.",
)
@ultroid_cmd(pattern="(a|approve)(?: |$)")
async def approvepm(apprvpm):
if apprvpm.reply_to_msg_id:
reply = await apprvpm.get_reply_message()
replied_user = await apprvpm.client.get_entity(reply.sender_id)
aname = replied_user.id
name0 = str(replied_user.first_name)
uid = replied_user.id
if not is_approved(uid):
approve_user(uid)
await apprvpm.edit(f"[{name0}](tg://user?id={uid}) `approved to PM!`")
await asyncio.sleep(3)
await apprvpm.delete()
else:
await apprvpm.edit("`User may already be approved.`")
await asyncio.sleep(5)
await apprvpm.delete()
elif apprvpm.is_private:
user = await apprvpm.get_chat()
aname = await apprvpm.client.get_entity(user.id)
name0 = str(aname.first_name)
uid = user.id
if not is_approved(uid):
approve_user(uid)
await apprvpm.edit(f"[{name0}](tg://user?id={uid}) `approved to PM!`")
async for message in apprvpm.client.iter_messages(user.id, search=UND):
await message.delete()
async for message in apprvpm.client.iter_messages(user.id, search=UNS):
await message.delete()
await asyncio.sleep(3)
await apprvpm.delete()
if Var.LOG_CHANNEL:
await apprvpm.client.send_message(
Var.LOG_CHANNEL,
f"#APPROVED\nUser: [{name0}](tg://user?id={uid})",
)
else:
await apprvpm.edit("`User may already be approved.`")
await asyncio.sleep(5)
await apprvpm.delete()
if Var.LOG_CHANNEL:
await apprvpm.client.send_message(
Var.LOG_CHANNEL,
f"#APPROVED\nUser: [{name0}](tg://user?id={uid})",
)
else:
await apprvpm.edit(NO_REPLY)
@ultroid_cmd(pattern="(da|disapprove)(?: |$)")
async def disapprovepm(e):
if e.reply_to_msg_id:
reply = await e.get_reply_message()
replied_user = await e.client.get_entity(reply.sender_id)
aname = replied_user.id
name0 = str(replied_user.first_name)
if is_approved(replied_user.id):
disapprove_user(replied_user.id)
await e.edit(
f"[{name0}](tg://user?id={replied_user.id}) `Disaproved to PM!`"
)
await asyncio.sleep(5)
await e.delete()
else:
await e.edit(
f"[{name0}](tg://user?id={replied_user.id}) was never approved!"
)
await asyncio.sleep(5)
await e.delete()
elif e.is_private:
bbb = await e.get_chat()
aname = await e.client.get_entity(bbb.id)
name0 = str(aname.first_name)
if is_approved(bbb.id):
disapprove_user(bbb.id)
await e.edit(f"[{name0}](tg://user?id={bbb.id}) `Disaproved to PM!`")
await asyncio.sleep(5)
await e.delete()
if Var.LOG_CHANNEL:
await e.client.send_message(
Var.LOG_CHANNEL,
f"[{name0}](tg://user?id={bbb.id}) was disapproved to PM you.",
)
else:
await e.edit(f"[{name0}](tg://user?id={bbb.id}) was never approved!")
await asyncio.sleep(5)
await e.delete()
else:
await e.edit(NO_REPLY)
@ultroid_cmd(pattern="block$")
async def blockpm(block):
if block.reply_to_msg_id:
reply = await block.get_reply_message()
replied_user = await block.client.get_entity(reply.sender_id)
aname = replied_user.id
name0 = str(replied_user.first_name)
await block.client(BlockRequest(replied_user.id))
await block.edit("`You've been blocked!`")
uid = replied_user.id
elif block.is_private:
bbb = await block.get_chat()
await block.client(BlockRequest(bbb.id))
aname = await block.client.get_entity(bbb.id)
await block.edit("`You've been blocked!`")
name0 = str(aname.first_name)
uid = bbb.id
else:
await block.edit(NO_REPLY)
try:
disapprove_user(uid)
except AttributeError:
pass
if Var.LOG_CHANNEL:
await block.client.send_message(
Var.LOG_CHANNEL, f"#BLOCKED\nUser: [{name0}](tg://user?id={uid})"
)
@ultroid_cmd(pattern="unblock$")
async def unblockpm(unblock):
if unblock.reply_to_msg_id:
reply = await unblock.get_reply_message()
replied_user = await unblock.client.get_entity(reply.sender_id)
name0 = str(replied_user.first_name)
await unblock.client(UnblockRequest(replied_user.id))
await unblock.edit("`You have been unblocked.`")
else:
await unblock.edit(NO_REPLY)
if Var.LOG_CHANNEL:
await unblock.client.send_message(
Var.LOG_CHANNEL,
f"[{name0}](tg://user?id={replied_user.id}) was unblocked!.",
)
HELP.update({f"{__name__.split('.')[1]}": f"{__doc__.format(i=Var.HNDLR)}"})