-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfilter.py
142 lines (116 loc) · 4.66 KB
/
filter.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
139
140
141
142
# -*- coding: utf-8 -*-
# Module author: @ftgmodulesbyfl1yd
from .. import loader, utils
@loader.tds
class FiltersMod(loader.Module):
"""Filters module"""
strings = {"name": "Filters"}
async def client_ready(self, client, db):
self.db = db
async def filtercmd(self, message):
"""Adds a filter into the list."""
filters = self.db.get("Filters", "filters", {})
key = utils.get_args_raw(message).lower()
reply = await message.get_reply_message()
chatid = str(message.chat_id)
if not key and not reply:
return await message.edit("<b>No args or reply.</b>")
if chatid not in filters:
filters.setdefault(chatid, {})
if key in filters[chatid]:
return await message.edit("<b>Such a filter already exists.</b>")
if reply:
if key:
msgid = await self.db.store_asset(reply)
else:
return await message.edit(
"<b>You need arguments to save the filter!</b>"
)
else:
try:
msgid = (
await message.client.send_message(
f"friendly-{(await message.client.get_me()).id}-assets",
key.split("/")[1],
)
).id
key = key.split("/")[0]
except IndexError:
return await message.edit(
"<b>Need a second argument (through / ) or a reply.</b>"
)
filters[chatid].setdefault(key, msgid)
self.db.set("Filters", "filters", filters)
await message.edit(f'<b>Filter "{key}" saved!</b>')
async def stopcmd(self, message):
"""Removes a filter from the list."""
filters = self.db.get("Filters", "filters", {})
args = utils.get_args_raw(message)
chatid = str(message.chat_id)
if chatid not in filters:
return await message.edit("<b>There are no filters in this chat.</b>")
if not args:
return await message.edit("<b>No args.</b>")
if args:
try:
filters[chatid].pop(args)
self.db.set("Filters", "filters", filters)
await message.edit(f'<b>Filter "{args}" removed from chat list!</b>')
except KeyError:
return await message.edit(f'<b>No "{args}" filter.</b>')
else:
return await message.edit("<b>No args.</b>")
async def stopallcmd(self, message):
"""Clears out the filter list."""
filters = self.db.get("Filters", "filters", {})
chatid = str(message.chat_id)
if chatid not in filters:
return await message.edit("<b>There are no filters in this chat.</b>")
filters.pop(chatid)
self.db.set("Filters", "filters", filters)
await message.edit("<b>All filters have been removed from the chat list!</b>")
async def filterscmd(self, message):
"""Shows saved filters."""
filters = self.db.get("Filters", "filters", {})
chatid = str(message.chat_id)
if chatid not in filters:
return await message.edit("<b>There are no filters in this chat.</b>")
msg = ""
for _ in filters[chatid]:
msg += f"<b>• {_}</b>\n"
await message.edit(
f"<b>List of filters in this chat: {len(filters[chatid])}\n\n{msg}</b>"
)
async def watcher(self, message):
try:
filters = self.db.get("Filters", "filters", {})
chatid = str(message.chat_id)
m = message.text.lower()
if chatid not in filters:
return
for _ in filters[chatid]:
msg = await self.db.fetch_asset(filters[chatid][_])
def_pref = self.db.get("friendly-telegram.main", "command_prefix")
pref = "." if not def_pref else def_pref[0]
if len(_.split()) == 1:
if _ in m.split():
await self.exec_comm(msg, message, pref)
else:
if _ in m:
await self.exec_comm(msg, message, pref)
except:
pass
async def exec_comm(self, msg, message, pref):
try:
if msg.text[0] == pref:
smsg = msg.text.split()
return await self.allmodules.commands[smsg[0][1:]](
await message.reply(
smsg[0] + " ".join(_ for _ in smsg if len(smsg) > 1)
)
)
else:
pass
except:
pass
await message.reply(msg)