Karma
commited on
Commit
·
4db115a
1
Parent(s):
770fcd1
later
Browse files- Mikobot/plugins/info.py +5 -166
Mikobot/plugins/info.py
CHANGED
@@ -1,28 +1,20 @@
|
|
1 |
# <============================================== IMPORTS =========================================================>
|
2 |
-
import asyncio
|
3 |
import os
|
4 |
import re
|
5 |
from html import escape
|
6 |
from random import choice
|
7 |
|
8 |
-
from telegram import
|
9 |
-
ChatMemberAdministrator,
|
10 |
-
InlineKeyboardButton,
|
11 |
-
InlineKeyboardMarkup,
|
12 |
-
Update,
|
13 |
-
)
|
14 |
from telegram.constants import ChatID, ChatType, ParseMode
|
15 |
-
from telegram.error import BadRequest
|
16 |
-
from telegram.ext import
|
17 |
from telegram.helpers import mention_html
|
18 |
|
19 |
-
import Database.sql.global_bans_sql as gban_sql
|
20 |
-
import Database.sql.users_sql as user_sql
|
21 |
from Database.sql.approve_sql import is_approved
|
22 |
from Infamous.karma import START_IMG
|
23 |
from Mikobot import DEV_USERS, DRAGONS, INFOPIC, OWNER_ID, function
|
24 |
from Mikobot.__main__ import STATS, USER_INFO
|
25 |
-
from Mikobot.plugins.helper_funcs.chat_status import
|
26 |
from Mikobot.plugins.users import get_user_id
|
27 |
|
28 |
# <=======================================================================================================>
|
@@ -207,155 +199,6 @@ async def stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
207 |
)
|
208 |
|
209 |
|
210 |
-
async def get_invalid_chats(
|
211 |
-
update: Update, context: ContextTypes.DEFAULT_TYPE, remove: bool = False
|
212 |
-
):
|
213 |
-
bot = context.bot
|
214 |
-
chat_id = update.effective_chat.id
|
215 |
-
chats = user_sql.get_all_chats()
|
216 |
-
kicked_chats, progress = 0, 0
|
217 |
-
chat_list = []
|
218 |
-
progress_message = None
|
219 |
-
|
220 |
-
for chat in chats:
|
221 |
-
if ((100 * chats.index(chat)) / len(chats)) > progress:
|
222 |
-
progress_bar = f"{progress}% completed in getting invalid chats."
|
223 |
-
if progress_message:
|
224 |
-
try:
|
225 |
-
await bot.editMessageText(
|
226 |
-
progress_bar,
|
227 |
-
chat_id,
|
228 |
-
progress_message.message_id,
|
229 |
-
)
|
230 |
-
except:
|
231 |
-
pass
|
232 |
-
else:
|
233 |
-
progress_message = await bot.sendMessage(
|
234 |
-
chat_id,
|
235 |
-
progress_bar,
|
236 |
-
message_thread_id=update.effective_message.message_thread_id
|
237 |
-
if update.effective_chat.is_forum
|
238 |
-
else None,
|
239 |
-
)
|
240 |
-
progress += 5
|
241 |
-
|
242 |
-
cid = chat.chat_id
|
243 |
-
await asyncio.sleep(0.1)
|
244 |
-
try:
|
245 |
-
await bot.get_chat(cid, timeout=60)
|
246 |
-
except (BadRequest, Forbidden):
|
247 |
-
kicked_chats += 1
|
248 |
-
chat_list.append(cid)
|
249 |
-
except:
|
250 |
-
pass
|
251 |
-
|
252 |
-
try:
|
253 |
-
await progress_message.delete()
|
254 |
-
except:
|
255 |
-
pass
|
256 |
-
|
257 |
-
if not remove:
|
258 |
-
return kicked_chats
|
259 |
-
else:
|
260 |
-
for muted_chat in chat_list:
|
261 |
-
await asyncio.sleep(0.1)
|
262 |
-
user_sql.rem_chat(muted_chat)
|
263 |
-
return kicked_chats
|
264 |
-
|
265 |
-
|
266 |
-
async def get_invalid_gban(
|
267 |
-
update: Update, context: ContextTypes.DEFAULT_TYPE, remove: bool = False
|
268 |
-
):
|
269 |
-
bot = context.bot
|
270 |
-
banned = gban_sql.get_gban_list()
|
271 |
-
ungbanned_users = 0
|
272 |
-
ungban_list = []
|
273 |
-
|
274 |
-
for user in banned:
|
275 |
-
user_id = user["user_id"]
|
276 |
-
await asyncio.sleep(0.1)
|
277 |
-
try:
|
278 |
-
await bot.get_chat(user_id)
|
279 |
-
except BadRequest:
|
280 |
-
ungbanned_users += 1
|
281 |
-
ungban_list.append(user_id)
|
282 |
-
except:
|
283 |
-
pass
|
284 |
-
|
285 |
-
if not remove:
|
286 |
-
return ungbanned_users
|
287 |
-
else:
|
288 |
-
for user_id in ungban_list:
|
289 |
-
await asyncio.sleep(0.1)
|
290 |
-
gban_sql.ungban_user(user_id)
|
291 |
-
return ungbanned_users
|
292 |
-
|
293 |
-
|
294 |
-
@check_admin(only_dev=True)
|
295 |
-
async def dbcleanup(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
296 |
-
msg = update.effective_message
|
297 |
-
|
298 |
-
await msg.reply_text("Getting invalid chat count...")
|
299 |
-
invalid_chat_count = await get_invalid_chats(update, context)
|
300 |
-
|
301 |
-
await msg.reply_text("Getting invalid gban count...")
|
302 |
-
invalid_gban_count = await get_invalid_gban(update, context)
|
303 |
-
|
304 |
-
reply = f"Total invalid chats - {invalid_chat_count}\n"
|
305 |
-
reply += f"Total invalid gban users - {invalid_gban_count}"
|
306 |
-
|
307 |
-
buttons = [[InlineKeyboardButton("Cleanup DB", callback_data="db_cleanup")]]
|
308 |
-
|
309 |
-
await update.effective_message.reply_text(
|
310 |
-
reply,
|
311 |
-
reply_markup=InlineKeyboardMarkup(buttons),
|
312 |
-
)
|
313 |
-
|
314 |
-
|
315 |
-
async def callback_button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
316 |
-
bot = context.bot
|
317 |
-
query = update.callback_query
|
318 |
-
message = query.message
|
319 |
-
chat_id = update.effective_chat.id
|
320 |
-
query_type = query.data
|
321 |
-
|
322 |
-
admin_list = [OWNER_ID] + DEV_USERS
|
323 |
-
|
324 |
-
await bot.answer_callback_query(query.id)
|
325 |
-
|
326 |
-
if query_type == "db_leave_chat":
|
327 |
-
if query.from_user.id in admin_list:
|
328 |
-
await bot.editMessageText("Leaving chats...", chat_id, message.message_id)
|
329 |
-
chat_count = await get_invalid_chats(update, context, True)
|
330 |
-
await bot.sendMessage(
|
331 |
-
chat_id,
|
332 |
-
f"Left {chat_count} chats.",
|
333 |
-
message_thread_id=message.message_thread_id
|
334 |
-
if update.effective_chat.is_forum
|
335 |
-
else None,
|
336 |
-
)
|
337 |
-
else:
|
338 |
-
await query.answer("You are not allowed to use this.")
|
339 |
-
elif query_type == "db_cleanup":
|
340 |
-
if query.from_user.id in admin_list:
|
341 |
-
await bot.editMessageText("Cleaning up DB...", chat_id, message.message_id)
|
342 |
-
invalid_chat_count = await get_invalid_chats(update, context, True)
|
343 |
-
invalid_gban_count = await get_invalid_gban(update, context, True)
|
344 |
-
reply = "Cleaned up {} chats and {} gban users from DB.".format(
|
345 |
-
invalid_chat_count,
|
346 |
-
invalid_gban_count,
|
347 |
-
)
|
348 |
-
await bot.sendMessage(
|
349 |
-
chat_id,
|
350 |
-
reply,
|
351 |
-
message_thread_id=message.message_thread_id
|
352 |
-
if update.effective_chat.is_forum
|
353 |
-
else None,
|
354 |
-
)
|
355 |
-
else:
|
356 |
-
await query.answer("You are not allowed to use this.")
|
357 |
-
|
358 |
-
|
359 |
# <=================================================== HELP ====================================================>
|
360 |
|
361 |
|
@@ -368,15 +211,11 @@ __help__ = """
|
|
368 |
# <================================================ HANDLER =======================================================>
|
369 |
STATS_HANDLER = CommandHandler(["stats", "gstats"], stats, block=False)
|
370 |
INFO_HANDLER = CommandHandler(("info", "book"), info, block=False)
|
371 |
-
DB_CLEANUP_HANDLER = CommandHandler("dbcleanup", dbcleanup, block=False)
|
372 |
-
BUTTON_HANDLER = CallbackQueryHandler(callback_button, pattern="db_.*", block=False)
|
373 |
|
374 |
-
function(DB_CLEANUP_HANDLER)
|
375 |
-
function(BUTTON_HANDLER)
|
376 |
function(STATS_HANDLER)
|
377 |
function(INFO_HANDLER)
|
378 |
|
379 |
__mod_name__ = "INFO"
|
380 |
__command_list__ = ["info"]
|
381 |
-
__handlers__ = [INFO_HANDLER, STATS_HANDLER
|
382 |
# <================================================ END =======================================================>
|
|
|
1 |
# <============================================== IMPORTS =========================================================>
|
|
|
2 |
import os
|
3 |
import re
|
4 |
from html import escape
|
5 |
from random import choice
|
6 |
|
7 |
+
from telegram import ChatMemberAdministrator, Update
|
|
|
|
|
|
|
|
|
|
|
8 |
from telegram.constants import ChatID, ChatType, ParseMode
|
9 |
+
from telegram.error import BadRequest
|
10 |
+
from telegram.ext import CommandHandler, ContextTypes
|
11 |
from telegram.helpers import mention_html
|
12 |
|
|
|
|
|
13 |
from Database.sql.approve_sql import is_approved
|
14 |
from Infamous.karma import START_IMG
|
15 |
from Mikobot import DEV_USERS, DRAGONS, INFOPIC, OWNER_ID, function
|
16 |
from Mikobot.__main__ import STATS, USER_INFO
|
17 |
+
from Mikobot.plugins.helper_funcs.chat_status import support_plus
|
18 |
from Mikobot.plugins.users import get_user_id
|
19 |
|
20 |
# <=======================================================================================================>
|
|
|
199 |
)
|
200 |
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
# <=================================================== HELP ====================================================>
|
203 |
|
204 |
|
|
|
211 |
# <================================================ HANDLER =======================================================>
|
212 |
STATS_HANDLER = CommandHandler(["stats", "gstats"], stats, block=False)
|
213 |
INFO_HANDLER = CommandHandler(("info", "book"), info, block=False)
|
|
|
|
|
214 |
|
|
|
|
|
215 |
function(STATS_HANDLER)
|
216 |
function(INFO_HANDLER)
|
217 |
|
218 |
__mod_name__ = "INFO"
|
219 |
__command_list__ = ["info"]
|
220 |
+
__handlers__ = [INFO_HANDLER, STATS_HANDLER]
|
221 |
# <================================================ END =======================================================>
|