-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsfwfilter.py
More file actions
146 lines (128 loc) · 5.42 KB
/
nsfwfilter.py
File metadata and controls
146 lines (128 loc) · 5.42 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
# Oreo
"""
✘ Commands Available -
•`{i}addnsfw <ban/mute/kick>`
If someone sends 18+ content it will be deleted and action will be taken.
•`{i}remnsfw`
Remove Chat from nsfw filtering.
"""
import os
from . import LOGS
try:
from ProfanityDetector import detector
except ImportError:
detector = None
LOGS.error("nsfwfilter: 'Profanitydetector' not installed!")
from pyOreo.dB.nsfw_db import is_nsfw, nsfw_chat, rem_nsfw
from . import HNDLR, async_searcher, eor, events, udB, oreo_bot, oreo_cmd
@oreo_cmd(pattern="addnsfw( (.*)|$)", admins_only=True)
async def addnsfw(e):
if not udB.get_key("DEEP_API"):
return await eor(
e, f"Get Api from deepai.org and Add It `{HNDLR}setdb DEEP_API your-api`"
)
action = e.pattern_match.group(1).strip()
if not action or ("ban" or "kick" or "mute") not in action:
action = "mute"
nsfw_chat(e.chat_id, action)
oreo_bot.add_handler(nsfw_check, events.NewMessage(incoming=True))
await e.eor("Added This Chat To Nsfw Filter")
@oreo_cmd(pattern="remnsfw", admins_only=True)
async def remnsfw(e):
rem_nsfw(e.chat_id)
await e.eor("Removed This Chat from Nsfw Filter.")
NWARN = {}
async def nsfw_check(e):
chat = e.chat_id
action = is_nsfw(chat)
if action and udB.get_key("DEEP_API") and e.media:
pic, name, nsfw = "", "", 0
try:
pic = await e.download_media(thumb=-1)
except BaseException:
pass
if e.file:
name = e.file.name
if detector and name:
x, y = detector(name)
if y:
nsfw += 1
if pic and not nsfw:
r = await async_searcher(
"https://api.deepai.org/api/nsfw-detector",
data={
"image": open(pic, "rb"),
},
post=True,
re_json=True,
headers={"api-key": udB.get_key("DEEP_API")},
)
try:
k = float((r["output"]["nsfw_score"]))
except KeyError as er:
LOGS.exception(er)
LOGS.info(r)
return
score = int(k * 100)
if score > 45:
nsfw += 1
os.remove(pic)
if nsfw:
await e.delete()
if NWARN.get(e.sender_id):
count = NWARN[e.sender_id] + 1
if count < 3:
NWARN.update({e.sender_id: count})
return await oreo_bot.send_message(
chat,
f"**NSFW Warn {count}/3** To [{e.sender.first_name}](tg://user?id={e.sender_id})\nNSFW prohibited! Repeated violation would lead to {action}",
)
if "mute" in action:
try:
await oreo_bot.edit_permissions(
chat, e.sender_id, until_date=None, send_messages=False
)
await oreo_bot.send_message(
chat,
f"NSFW Warn 3/3 to [{e.sender.first_name}](tg://user?id={e.sender_id})\n\n**Action Taken** : {action}",
)
except BaseException:
await oreo_bot.send_message(
chat,
f"NSFW Warn 3/3 to [{e.sender.first_name}](tg://user?id={e.sender_id})\n\nUnable to {action}.",
)
elif "ban" in action:
try:
await oreo_bot.edit_permissions(
chat, e.sender_id, view_messages=False
)
await oreo_bot.send_message(
chat,
f"NSFW Warn 3/3 to [{e.sender.first_name}](tg://user?id={e.sender_id})\n\n**Action Taken** : {action}",
)
except BaseException:
await oreo_bot.send_message(
chat,
f"NSFW Warn 3/3 to [{e.sender.first_name}](tg://user?id={e.sender_id})\n\nUnable to {action}.",
)
elif "kick" in action:
try:
await oreo_bot.kick_participant(chat, e.sender_id)
await oreo_bot.send_message(
chat,
f"NSFW Warn 3/3 to [{e.sender.first_name}](tg://user?id={e.sender_id})\n\n**Action Taken** : {action}",
)
except BaseException:
await oreo_bot.send_message(
chat,
f"NSFW Warn 3/3 to [{e.sender.first_name}](tg://user?id={e.sender_id})\n\nUnable to {action}.",
)
NWARN.pop(e.sender_id)
else:
NWARN.update({e.sender_id: 1})
return await oreo_bot.send_message(
chat,
f"**NSFW Warn 1/3** To [{e.sender.first_name}](tg://user?id={e.sender_id})\nNSFW prohibited! Repeated violation would lead to {action}",
)
if udB.get_key("NSFW"):
oreo_bot.add_handler(nsfw_check, events.NewMessage(incoming=True))