Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Google_Translator_Bot/Commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])

Comment on lines -85 to +125

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function broadcast_ refactored with the following changes:

done += 1
if broadcast_ids.get(broadcast_id) is None:
break
Expand All @@ -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.",
Expand All @@ -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')


Expand Down
5 changes: 2 additions & 3 deletions Google_Translator_Bot/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Database.is_user_exist refactored with the following changes:

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({})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Database.get_all_users refactored with the following changes:

async def delete_user(self, user_id):
await self.dcol.delete_many({'id': int(user_id)})
6 changes: 4 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
class Config(object):


class Config((object)):
Comment on lines -2 to +4

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 2-14 refactored with the following changes:


BOT_TOKEN = os.environ.get("BOT_TOKEN", "")

Expand All @@ -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()}