From 5e0d01b8b472804b3f830ab9b7a09dbd0e03846f Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 11 Dec 2021 04:42:45 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- Google_Translator_Bot/Commands.py | 27 ++++++++++++++------------- Google_Translator_Bot/Database.py | 5 ++--- config.py | 6 ++++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Google_Translator_Bot/Commands.py b/Google_Translator_Bot/Commands.py index 1173af2..d65fded 100644 --- a/Google_Translator_Bot/Commands.py +++ b/Google_Translator_Bot/Commands.py @@ -82,46 +82,47 @@ async def broadcast_(c, m): return all_users = await db.get_all_users() broadcast_msg = m.reply_to_message - + while True: broadcast_id = ''.join([random.choice(string.ascii_letters) for i in range(3)]) if not broadcast_ids.get(broadcast_id): break - + out = await m.reply_text( - text = f"Broadcast initiated! You will be notified with log file when all the users are notified." + text='Broadcast initiated! You will be notified with log file when all the users are notified.' ) + start_time = time.time() total_users = await db.total_users_count() done = 0 failed = 0 success = 0 - + broadcast_ids[broadcast_id] = dict( total = total_users, current = done, failed = failed, success = success ) - + async with aiofiles.open('broadcast.txt', 'w') as broadcast_log_file: async for user in all_users: - + sts, msg = await send_msg( user_id = int(user['id']), message = broadcast_msg ) if msg is not None: await broadcast_log_file.write(msg) - + if sts == 200: success += 1 else: failed += 1 - + if sts == 400: await db.delete_user(user['id']) - + done += 1 if broadcast_ids.get(broadcast_id) is None: break @@ -136,11 +137,11 @@ async def broadcast_(c, m): if broadcast_ids.get(broadcast_id): broadcast_ids.pop(broadcast_id) completed_in = datetime.timedelta(seconds=int(time.time()-start_time)) - + await asyncio.sleep(3) - + await out.delete() - + if failed == 0: await m.reply_text( text=f"broadcast completed in `{completed_in}`\n\nTotal users {total_users}.\nTotal done {done}, {success} success and {failed} failed.", @@ -152,7 +153,7 @@ async def broadcast_(c, m): caption=f"""broadcast completed in `{completed_in}`\n\nTotal users {total_users}.\nTotal done {done}, {success} success and {failed} failed.""", quote=True ) - + await aiofiles.os.remove('broadcast.txt') diff --git a/Google_Translator_Bot/Database.py b/Google_Translator_Bot/Database.py index 2df13e8..03f5960 100644 --- a/Google_Translator_Bot/Database.py +++ b/Google_Translator_Bot/Database.py @@ -19,12 +19,11 @@ async def add_user(self, id): await self.dcol.insert_one(user) async def is_user_exist(self, id): user = await self.dcol.find_one({'id':int(id)}) - return True if user else False + return bool(user) async def total_users_count(self): count = await self.dcol.count_documents({}) return count async def get_all_users(self): - all_users = self.dcol.find({}) - return all_users + return self.dcol.find({}) async def delete_user(self, user_id): await self.dcol.delete_many({'id': int(user_id)}) diff --git a/config.py b/config.py index bd3e53d..95680fc 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,7 @@ import os -class Config(object): + + +class Config((object)): BOT_TOKEN = os.environ.get("BOT_TOKEN", "") @@ -11,4 +13,4 @@ class Config(object): DEV_NAME = os.environ.get("DEV_NAME", "Muhammed") - DEV_ID = set(int(x) for x in os.environ.get("DEV_ID", "1855070892").split()) + DEV_ID = {int(x) for x in os.environ.get("DEV_ID", "1855070892").split()}