aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorIgor <50257429+igorechek06@users.noreply.github.com>2021-10-12 21:10:36 +0900
committerIgor <50257429+igorechek06@users.noreply.github.com>2021-10-12 21:10:36 +0900
commit4e7b5e6493497eb2de0dd40df19fc6492427e446 (patch)
tree0071164c777e9bc287d8cd7b62026e0850464bbf /utils
parentf1dbf92a26490095a45e4a39fd654debc0e5828c (diff)
downloadkarpov_ai_bot-4e7b5e6493497eb2de0dd40df19fc6492427e446.tar.gz
karpov_ai_bot-4e7b5e6493497eb2de0dd40df19fc6492427e446.zip
Исправил баги
Diffstat (limited to 'utils')
-rw-r--r--utils/filters.py137
1 files changed, 6 insertions, 131 deletions
diff --git a/utils/filters.py b/utils/filters.py
index b6ddec5..c8a1fb5 100644
--- a/utils/filters.py
+++ b/utils/filters.py
@@ -1,94 +1,20 @@
1import re 1from __future__ import annotations
2import typing as p
3from random import random
4from aiogram import types as t, filters as f, Bot
5
6from shared.instances import bot
7# from libs.classes import Errors as e
8# from libs.objects import Database
9# from libs.system import regex as r
10
11objType = p.Union[t.Message, t.CallbackQuery, t.ChatMemberUpdated]
12
13
14class _helper:
15 @staticmethod
16 def get_user_and_chat(obj: objType):
17 if isinstance(obj, t.Message):
18 chat = obj.chat
19 user = obj.from_user
20 elif isinstance(obj, t.CallbackQuery):
21 chat = obj.message.chat
22 user = obj.from_user
23 elif isinstance(obj, t.ChatMemberUpdated):
24 chat = obj.chat
25 user = obj.from_user
26 else:
27 raise TypeError()
28
29 return user, chat
30
31 @staticmethod
32 def has_permission(admin: t.ChatMember, permission: str):
33 if t.ChatMemberStatus.is_chat_creator(admin.status):
34 return True
35 elif t.ChatMemberStatus.is_chat_admin(admin.status):
36 return getattr(admin, permission)
37 else:
38 return False
39
40 2
41class AdminFilter(f.BoundFilter): 3from random import random
42 def __init__(self, user_id: int = None):
43 self._user_id = user_id
44
45 async def check(self, obj: objType):
46 user, chat = _helper.get_user_and_chat(obj)
47
48 admin = await chat.get_member(self._user_id or user.id)
49 if t.ChatMemberStatus.is_chat_admin(admin.status):
50 return True
51 else:
52 return False
53
54
55class AliasFilter(f.BoundFilter):
56 def __init__(self):
57 pass
58
59 async def check(self, msg: objType) -> bool:
60 if not isinstance(msg, t.Message):
61 raise TypeError()
62 if await message.is_private.check(msg):
63 return False
64 chat = Database.get_chat(msg.chat.id)
65
66 if msg.sticker:
67 aliases = list(chat.settings.sticker_alias.keys())
68 text = msg.sticker.file_unique_id
69 elif msg.text:
70 aliases = list(chat.settings.text_alias.keys())
71 text = msg.text
72 4
73 for alias in aliases: 5from aiogram import types as t, filters as f
74 pattern = re.compile(r.alias(alias), re.IGNORECASE)
75 if pattern.match(text):
76 return True
77 6
78 return False 7from shared import instances as ins
79 8
80 9
81class message: 10class message:
82 is_chat = f.ChatTypeFilter((t.ChatType.GROUP, t.ChatType.SUPERGROUP)) 11 is_chat = f.ChatTypeFilter((t.ChatType.GROUP, t.ChatType.SUPERGROUP))
83 is_private = f.ChatTypeFilter(t.ChatType.PRIVATE) 12 is_private = f.ChatTypeFilter(t.ChatType.PRIVATE)
84 is_reply = f.IsReplyFilter(True) 13 is_reply = f.IsReplyFilter(True)
85 is_alias = AliasFilter()
86 14
87 @staticmethod 15 @staticmethod
88 def chance(percent: int): 16 def chance(msg: t.Message):
89 def filter(msg: t.Message): 17 return random() <= (ins.gen_chance / 100)
90 return random() <= (percent / 100)
91 return filter
92 18
93 @staticmethod 19 @staticmethod
94 def has_text(msg: t.Message): 20 def has_text(msg: t.Message):
@@ -96,60 +22,9 @@ class message:
96 return True 22 return True
97 23
98 24
99class bot:
100 is_admin = AdminFilter(bot.id)
101
102 @staticmethod
103 def has_permission(permissions: p.List[str]):
104 permissions = permissions
105
106 async def filter(obj: objType):
107 bot = Bot.get_current()
108 user, chat = _helper.get_user_and_chat(obj)
109 admin = await chat.get_member(bot.id)
110 if not _helper.has_permission(admin, permissions):
111 raise e.BotHasNotPermission()
112 return True
113
114 return filter
115
116
117class user: 25class user:
118 is_admin = AdminFilter()
119
120 @staticmethod
121 def has_permission(permissions: p.List[str]):
122 permissions = permissions
123
124 async def filter(obj: objType):
125 user, chat = _helper.get_user_and_chat(obj)
126 admin = await chat.get_member(user.id)
127 if not _helper.has_permission(admin, permissions):
128 raise e.HasNotPermission()
129 return True
130
131 return filter
132
133 @staticmethod 26 @staticmethod
134 def add_member(upd: t.ChatMemberUpdated): 27 def add_member(upd: t.ChatMemberUpdated):
135 old = upd.old_chat_member 28 old = upd.old_chat_member
136 new = upd.new_chat_member 29 new = upd.new_chat_member
137 return not t.ChatMemberStatus.is_chat_member(old.status) and t.ChatMemberStatus.is_chat_member(new.status) 30 return not t.ChatMemberStatus.is_chat_member(old.status) and t.ChatMemberStatus.is_chat_member(new.status)
138
139 @staticmethod
140 def removed_member(upd: t.ChatMemberUpdated):
141 old = upd.old_chat_member
142 new = upd.new_chat_member
143 return t.ChatMemberStatus.is_chat_member(old.status) and not t.ChatMemberStatus.is_chat_member(new.status)
144
145 @staticmethod
146 def promote_admin(upd: t.ChatMemberUpdated):
147 old = upd.old_chat_member
148 new = upd.new_chat_member
149 return not t.ChatMemberStatus.is_chat_admin(old.status) and t.ChatMemberStatus.is_chat_admin(new.status)
150
151 @staticmethod
152 def restrict_admin(upd: t.ChatMemberUpdated):
153 old = upd.old_chat_member
154 new = upd.new_chat_member
155 return t.ChatMemberStatus.is_chat_admin(old.status) and not t.ChatMemberStatus.is_chat_admin(new.status)