forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilters.py
executable file
·138 lines (124 loc) · 5.5 KB
/
filters.py
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
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from .. import loader, utils
import functools
from telethon import events
import logging
logger = logging.getLogger("FilterModule")
@loader.tds
class FiltersMod(loader.Module):
"""When you filter a text, it auto responds to it if a user triggers the word)"""
strings = {"name": "Filters"}
def __init__(self):
self._me = None
self._ratelimit = []
async def client_ready(self, client, db):
self._db = db
self._client = client
self._me = await client.get_me()
if "Filters.watchout" not in str(client.list_event_handlers()):
client.add_event_handler(
functools.partial(self.watchout),
events.NewMessage(outgoing=True, incoming=True, forwards=False))
async def filtercmd(self, message):
"""Adds a filter into the list."""
args = utils.get_args_split_by(message, ",")
chatid = str(message.chat_id)
filters = self._db.get("FilterModule", "filters", {})
if not args:
await message.edit(("<b>Enter a name for the filter first!</b>"))
return
name = args[0]
if chatid not in filters:
filters.setdefault(chatid, {})
if not message.is_reply:
if len(args) == 1:
await message.edit(("<b>Please reply to a message or enter a text to save as filter.!</b>"))
return
else:
value = args[1]
msg_to_log = await self._db.store_asset(value)
else:
value = await message.get_reply_message()
msg_to_log = await self._db.store_asset(value)
filters[chatid][name] = msg_to_log
self._db.set("FilterModule", "filters", filters)
await message.edit((
"<b>Successfully filtered.</b>".format(name)))
message.message = ""
async def stopcmd(self, message):
"""Removes a filter from the list."""
filtern = utils.get_args_raw(message)
filters = self._db.get("FilterModule", "filters", {})
chatid = str(message.chat_id)
if not filtern:
await message.edit(("<b>Please specify the name of the filter.</b>"))
return
try:
del filters[chatid][filtern]
await message.edit(("<b>Filter </b><i>{}</i><b> successfully removed from the chat.</b>".format(filtern)))
self._db.set("FilterModule", "filters", filters)
except KeyError:
await message.edit(("<b>Filter </b><i>{}</i><b> not found in this chat</b>".format(filtern)))
async def stopallcmd(self, message):
"""Clears out the filter list."""
filters = self._db.get("FilterModule", "filters", {})
chatid = str(message.chat_id)
try:
del filters[chatid]
self._db.set("FilterModule", "filters", filters)
await message.edit(("<b>All filters successfully removed from the chat.</b>"))
except KeyError:
await message.edit(("<b>There are no filters to clear out in this chat.</b>"))
async def filterscmd(self, message):
"""Shows saved filters."""
filters = ""
filt = self._db.get("FilterModule", "filters", {})
chatid = str(message.chat_id)
try:
for i in filt[chatid]:
filters += "<b> - " + str(i) + "</b>\n"
pass
except Exception:
pass
filterl = "<b>Word(s) that you filtered in this chat: </b>\n\n{}".format(filters)
if filters:
await message.edit(filterl)
else:
await message.edit(("<b>No filters found in this chat.</b>"))
async def watchout(self, message):
filters = self._db.get("FilterModule", "filters", {})
args = message.text.split(" ")
exec = True
chatid = str(message.chat_id)
if chatid not in str(filters):
return
for key in filters[chatid]:
if key in args:
id = filters[chatid][key]
value = await self._db.fetch_asset(id)
if not value.media and not value.web_preview:
if value.text.startswith(".") is True:
arg = value.text[1::]
if value.text.startswith("..") is True:
arg = value.text[2::]
if value.text.startswith(".") is False:
arg = value.text
exec = False
respond = await message.respond(arg)
if exec is True:
argspr = arg.split(" ")
respond.message, cmd = self.allmodules.dispatch(argspr[0], respond)
await cmd(respond)
else:
await message.reply(value)