|
11 | 11 | import traceback |
12 | 12 | import typing |
13 | 13 | import BugReporter |
| 14 | +import Handlers |
14 | 15 | from collections import OrderedDict |
15 | 16 | from configparser import ConfigParser |
16 | 17 | from datetime import datetime, timedelta |
@@ -122,110 +123,12 @@ def __init__( |
122 | 123 | self.debug = False |
123 | 124 |
|
124 | 125 | if debug: |
125 | | - #TODO:Add debuging handlers |
126 | | - pass |
127 | | - |
128 | | - def confirm_admin(u: Update, c: CallbackContext): |
129 | | - query = u.callback_query |
130 | | - if u.effective_user.id == self.ownerID: |
131 | | - new_admin_id = int(query.data[7:]) |
132 | | - self.bot.send_message( |
133 | | - new_admin_id, |
134 | | - f'✅ Accepted, From now on, I know you as my admin') |
135 | | - self.adminID.append(new_admin_id) |
136 | | - self.set_data('adminID', self.adminID, DB = data_db) |
137 | | - self.admin_token.remove(self.admins_pendding[new_admin_id]) |
138 | | - del(self.admins_pendding[new_admin_id]) |
139 | | - query.answer('✅ Accepted') |
140 | | - query.message.edit_text(query.message.text+'\n\n✅ Accepted') |
141 | | - else: |
142 | | - query.answer() |
143 | | - |
144 | | - def decline_admin(u: Update, c: CallbackContext): |
145 | | - query = u.callback_query |
146 | | - if u.effective_user.id == self.ownerID: |
147 | | - new_admin_id = int(query.data[8:]) |
148 | | - self.bot.send_message( |
149 | | - new_admin_id, |
150 | | - f"❌ Declined, Owner didn't accepted your request") |
151 | | - self.admin_token.remove(self.admins_pendding[new_admin_id]) |
152 | | - del(self.admins_pendding[new_admin_id]) |
153 | | - query.answer('❌ Declined') |
154 | | - query.message.edit_text(query.message.text+'\n\n❌ Declined') |
155 | | - else: |
156 | | - query.answer() |
157 | | - |
158 | | - def unknown_query(u: Update, c: CallbackContext): |
159 | | - query = u.callback_query |
160 | | - logging.warning('unknown query, query data:'+query.data) |
161 | | - query.answer("❌ ERROR\nUnknown answer", show_alert = True,) |
162 | | - |
163 | | - self.dispatcher.add_handler(CallbackQueryHandler( |
164 | | - confirm_admin, pattern = 'accept-.*'), group=1) |
165 | | - self.dispatcher.add_handler(CallbackQueryHandler( |
166 | | - decline_admin, pattern = 'decline-.*'), group=1) |
167 | | - |
168 | | - def onjoin(u: Update, c: CallbackContext): |
169 | | - for member in u.message.new_chat_members: |
170 | | - if member.username == self.bot.username: |
171 | | - data = u.effective_chat.to_dict() |
172 | | - data['members-count'] = u.effective_chat.get_members_count()-1 |
173 | | - self.set_data(key = str(u.effective_chat.id), value = data) |
174 | | - self.bot.send_message( |
175 | | - self.ownerID, |
176 | | - '<i>Joined to a chat:</i>\n' + |
177 | | - html.escape(json.dumps( |
178 | | - data, indent = 2, ensure_ascii = False)), |
179 | | - ParseMode.HTML, |
180 | | - disable_notification = True) |
181 | | - if u.effective_chat.type != Chat.CHANNEL: |
182 | | - u.message.reply_markdown_v2( |
183 | | - self.get_string('group-intro')) |
184 | | - |
185 | | - def onkick(u: Update, c: CallbackContext): |
186 | | - if u.message.left_chat_member['username'] == self.bot.username: |
187 | | - data = self.get_data(str(u.effective_chat.id)) |
188 | | - if data: |
189 | | - self.bot.send_message( |
190 | | - self.ownerID, |
191 | | - '<i>Kicked from a chat:</i>\n' + |
192 | | - html.escape(json.dumps( |
193 | | - data, indent = 2, ensure_ascii = False)), |
194 | | - ParseMode.HTML, |
195 | | - disable_notification = True) |
196 | | - with self.env.begin(self.chats_db, write = True) as txn: |
197 | | - txn.delete(str(u.effective_chat.id).encode()) |
198 | | - |
199 | | - def onBotBlocked(u: Update, c:CallbackContext): |
200 | | - if (u.my_chat_member.new_chat_member.user.id == self.bot.id): |
201 | | - status = u.my_chat_member.new_chat_member.status |
202 | | - if status in (ChatMember.KICKED, ChatMember.LEFT, ChatMember.RESTRICTED): |
203 | | - logging.info('Bot had been kicked or blocked by a user') |
204 | | - with self.env.begin(self.chats_db, write = True) as txn: |
205 | | - txn.delete(str(u.my_chat_member.chat.id).encode()) |
206 | | - |
207 | | - |
208 | | - self.dispatcher.add_handler(ChatMemberHandler(onBotBlocked), group=1) |
209 | | - self.dispatcher.add_handler(MessageHandler( |
210 | | - Filters.status_update.new_chat_members, onjoin), group=1) |
211 | | - self.dispatcher.add_handler(MessageHandler( |
212 | | - Filters.status_update.left_chat_member, onkick), group=1) |
213 | | - |
214 | | - def error_handler(update: object, context: CallbackContext) -> None: |
215 | | - """Log the error and send a telegram message to notify the developer.""" |
216 | | - |
217 | | - self.log_bug( |
218 | | - context.error, |
219 | | - 'Exception while handling an update', |
220 | | - not isinstance(context.error, NetworkError), |
221 | | - update = update.to_dict() if isinstance(update, Update) else str(update), |
222 | | - user_data = context.user_data, |
223 | | - chat_data = context.chat_data |
224 | | - ) |
225 | | - |
226 | | - self.dispatcher.add_error_handler(error_handler) |
227 | | - |
228 | | - # ---------------------------------------------------- |
| 126 | + Handlers.add_debuging_handlers(self) |
| 127 | + |
| 128 | + Handlers.add_users_handlers(self) |
| 129 | + Handlers.add_admin_handlers(self) |
| 130 | + Handlers.add_owner_handlers(self) |
| 131 | + Handlers.add_other_handlers(self) |
229 | 132 |
|
230 | 133 | def log_bug(self, exc:Exception, msg='', report = True, disable_notification = False,**args): |
231 | 134 | info = BugReporter.exception(msg, exc, report = self.bug_reporter and report) |
|
0 commit comments