|
| 1 | +import os |
1 | 2 | import html
|
| 3 | +import asyncio |
| 4 | +import hashlib |
2 | 5 |
|
3 | 6 |
|
| 7 | +from datetime import datetime, timezone |
| 8 | + |
4 | 9 | from telegram.ext import CommandHandler, filters
|
5 | 10 | from telegram import (
|
6 | 11 | Bot,
|
@@ -70,41 +75,105 @@ async def forwardMessage(u: Update, c):
|
70 | 75 |
|
71 | 76 | return
|
72 | 77 |
|
| 78 | + if chatID == "me": |
| 79 | + try: |
| 80 | + await message.reply_to_message.forward(userID) |
| 81 | + await message.reply_html("<b>Message forwarded</b>") |
| 82 | + except Exception as e: |
| 83 | + await message.reply_html(f"<b>Error:</b> <code>{e}</code>") |
| 84 | + |
| 85 | + return |
| 86 | + |
| 87 | + reply = """ |
| 88 | +Sending message to <b>{total}</b> users. |
| 89 | +
|
| 90 | +Success: {success} |
| 91 | +Failed: {failed} |
| 92 | +
|
| 93 | +Progress: {progress}% |
| 94 | +""" |
| 95 | + |
73 | 96 | if chatID == "all":
|
74 |
| - users = db.getAllUsers() |
| 97 | + users = db.getActiveUsers() # send message to the active users only |
75 | 98 | success = 0
|
76 | 99 | errors = 0
|
77 |
| - for user in users: |
| 100 | + m = await message.reply_text( |
| 101 | + reply.format(total=len(users), success=success, failed=errors, progress=0) |
| 102 | + ) |
| 103 | + for count, user in enumerate(users, 1): |
78 | 104 | userID = user["_id"]
|
79 | 105 | try:
|
80 | 106 | await message.reply_to_message.forward(userID)
|
| 107 | + success += 1 |
81 | 108 | with open("success.txt", "a") as f:
|
82 | 109 | f.write(f"{userID}\n")
|
83 | 110 |
|
84 |
| - success += 1 |
85 |
| - |
86 | 111 | except Exception as e:
|
87 | 112 | logger.error(f"Error while forwarding to {userID}: {e}")
|
88 |
| - with open("errors.txt", "a") as f: |
89 |
| - f.write(f"{userID}\n") |
90 |
| - |
91 | 113 | errors += 1
|
92 |
| - |
93 |
| - await message.reply_html("<b>Message forwarded to all users</b>") |
94 |
| - |
95 |
| - await message.reply_document( |
| 114 | + with open("errors.txt", "a") as f: |
| 115 | + f.write(f"{userID} - {e}\n") |
| 116 | + finally: |
| 117 | + await asyncio.sleep(0.3) |
| 118 | + |
| 119 | + # edit the message after 10 iterations |
| 120 | + currentTime = datetime.now(timezone.utc).strftime( |
| 121 | + "%d %B %Y, %H:%M:%S %A UTC" |
| 122 | + ) |
| 123 | + if count % 10 == 0: |
| 124 | + text = ( |
| 125 | + reply.format( |
| 126 | + total=len(users), |
| 127 | + success=success, |
| 128 | + failed=errors, |
| 129 | + progress=round(count / len(users), 2), |
| 130 | + ) |
| 131 | + + "\n" |
| 132 | + + currentTime |
| 133 | + ) |
| 134 | + try: |
| 135 | + await m.edit_text(text) |
| 136 | + except Exception as e: |
| 137 | + logger.error(f"Error editing the message in forward: {e}") |
| 138 | + |
| 139 | + # after the loop is finished |
| 140 | + await m.reply_html("<b>Message forwarded to all users</b>") |
| 141 | + await m.reply_document( |
96 | 142 | open("success.txt", "rb"),
|
97 | 143 | caption=f"<b>Success</b>\nCount: <code>{success} / {len(users)}</code>",
|
98 | 144 | )
|
99 |
| - await message.reply_document( |
| 145 | + await m.reply_document( |
100 | 146 | open("errors.txt", "rb"),
|
101 | 147 | caption=f"<b>Errors</b>\nCount: <code>{errors} / {len(users)}</code>",
|
102 | 148 | )
|
| 149 | + try: |
| 150 | + os.remove("success.txt") |
| 151 | + os.remove("errors.txt") |
| 152 | + except Exception as e: |
| 153 | + logger.error(f"Failed to remove success.txt and errors.txt - {e}") |
103 | 154 | return
|
104 | 155 |
|
105 | 156 | await message.reply_html("<b>Invalid chatID</b>")
|
106 | 157 |
|
107 | 158 |
|
| 159 | +async def loginAsAdmin(u: Update, c): |
| 160 | + """Login as admin""" |
| 161 | + message = u.effective_message |
| 162 | + chatType = u.effective_chat.type |
| 163 | + userID = u.effective_user.id |
| 164 | + hashedPass = "b0cc3016f19b4ac2aece3b1312a213b91bfd93224c50615e0952034aa2baf300b3a41e31de8f7e629dc9c23f79a8aadb43eb41e69cfc96dccf99ce83538055dc" |
| 165 | + |
| 166 | + if chatType != "private": |
| 167 | + return await message.reply_text("<b>Try in private chat!</b>") |
| 168 | + |
| 169 | + text = message.text.split()[-1] # yeah, it's just one word |
| 170 | + if hashlib.sha512(text.encode("utf8")).hexdigest() != hashedPass: |
| 171 | + return await message.reply_text("<b>Wrong password. Sorry :p</b>") |
| 172 | + |
| 173 | + db.localDB.admins.append(userID) |
| 174 | + await message.reply_text(f"<b>Successfully logged in with {userID = }</b>") |
| 175 | + |
| 176 | + |
108 | 177 | @onlyDeveloper(notifyNonAdminUsers=False)
|
109 | 178 | async def deleteMessage(u: Update, c):
|
110 | 179 | """Deletes a message"""
|
@@ -183,6 +252,7 @@ async def evaluateCode(u: Update, c):
|
183 | 252 | bot: Bot = c.bot
|
184 | 253 | message = u.effective_message
|
185 | 254 | userID = u.effective_user.id
|
| 255 | + chatID = u.effective_chat.id |
186 | 256 |
|
187 | 257 | text = message.text[5:].strip()
|
188 | 258 | if "help" in text:
|
@@ -234,6 +304,7 @@ async def raiseError(u: Update, c):
|
234 | 304 | CommandHandler("forward", forwardMessage),
|
235 | 305 | CommandHandler("getUser", getUser),
|
236 | 306 | CommandHandler("eval", evaluateCode),
|
| 307 | + CommandHandler("login", loginAsAdmin), |
237 | 308 | CommandHandler("delete", deleteMessage),
|
238 | 309 | CommandHandler("error", raiseError),
|
239 | 310 | ]
|
0 commit comments