File size: 12,329 Bytes
618430a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# Ultroid - UserBot
# Copyright (C) 2021-2025 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://github.com/TeamUltroid/pyUltroid/blob/main/LICENSE>.
import asyncio
import inspect
import re
import sys
from io import BytesIO
from pathlib import Path
from time import gmtime, strftime
from traceback import format_exc
from telethon import Button
from telethon import __version__ as telever
from telethon import events
from telethon.errors.common import AlreadyInConversationError
from telethon.errors.rpcerrorlist import (
AuthKeyDuplicatedError,
BotInlineDisabledError,
BotMethodInvalidError,
ChatSendInlineForbiddenError,
ChatSendMediaForbiddenError,
ChatSendStickersForbiddenError,
FloodWaitError,
MessageDeleteForbiddenError,
MessageIdInvalidError,
MessageNotModifiedError,
UserIsBotError,
)
from telethon.events import MessageEdited, NewMessage
from telethon.utils import get_display_name
from pyUltroid.exceptions import DependencyMissingError
from strings import get_string
from .. import *
from .. import _ignore_eval
from ..dB import DEVLIST
from ..dB._core import LIST, LOADED
from ..fns.admins import admin_check
from ..fns.helper import bash
from ..fns.helper import time_formatter as tf
from ..version import __version__ as pyver
from ..version import ultroid_version as ult_ver
from . import SUDO_M, owner_and_sudos
from ._wrappers import eod
MANAGER = udB.get_key("MANAGER")
TAKE_EDITS = udB.get_key("TAKE_EDITS")
black_list_chats = udB.get_key("BLACKLIST_CHATS")
allow_sudo = SUDO_M.should_allow_sudo
def compile_pattern(data, hndlr):
if data.startswith("^"):
data = data[1:]
if data.startswith("."):
data = data[1:]
if hndlr in [" ", "NO_HNDLR"]:
# No Hndlr Feature
return re.compile("^" + data)
return re.compile("\\" + hndlr + data)
def ultroid_cmd(
pattern=None, manager=False, ultroid_bot=ultroid_bot, asst=asst, **kwargs
):
owner_only = kwargs.get("owner_only", False)
groups_only = kwargs.get("groups_only", False)
admins_only = kwargs.get("admins_only", False)
fullsudo = kwargs.get("fullsudo", False)
only_devs = kwargs.get("only_devs", False)
func = kwargs.get("func", lambda e: not e.via_bot_id)
def decor(dec):
async def wrapp(ult):
if not ult.out:
if owner_only:
return
if ult.sender_id not in owner_and_sudos():
return
if ult.sender_id in _ignore_eval:
return await eod(
ult,
get_string("py_d1"),
)
if fullsudo and ult.sender_id not in SUDO_M.fullsudos:
return await eod(ult, get_string("py_d2"), time=15)
chat = ult.chat
if hasattr(chat, "title"):
if (
"#noub" in chat.title.lower()
and not (chat.admin_rights or chat.creator)
and not (ult.sender_id in DEVLIST)
):
return
if ult.is_private and (groups_only or admins_only):
return await eod(ult, get_string("py_d3"))
elif admins_only and not (chat.admin_rights or chat.creator):
return await eod(ult, get_string("py_d5"))
if only_devs and not udB.get_key("I_DEV"):
return await eod(
ult,
get_string("py_d4").format(HNDLR),
time=10,
)
try:
await dec(ult)
except FloodWaitError as fwerr:
await asst.send_message(
udB.get_key("LOG_CHANNEL"),
f"`FloodWaitError:\n{str(fwerr)}\n\nSleeping for {tf((fwerr.seconds + 10)*1000)}`",
)
await ultroid_bot.disconnect()
await asyncio.sleep(fwerr.seconds + 10)
await ultroid_bot.connect()
await asst.send_message(
udB.get_key("LOG_CHANNEL"),
"`Bot is working again`",
)
return
except ChatSendInlineForbiddenError:
return await eod(ult, "`Inline Locked In This Chat.`")
except (ChatSendMediaForbiddenError, ChatSendStickersForbiddenError):
return await eod(ult, get_string("py_d8"))
except (BotMethodInvalidError, UserIsBotError):
return await eod(ult, get_string("py_d6"))
except AlreadyInConversationError:
return await eod(
ult,
get_string("py_d7"),
)
except (BotInlineDisabledError, DependencyMissingError) as er:
return await eod(ult, f"`{er}`")
except (
MessageIdInvalidError,
MessageNotModifiedError,
MessageDeleteForbiddenError,
) as er:
LOGS.exception(er)
except AuthKeyDuplicatedError as er:
LOGS.exception(er)
await asst.send_message(
udB.get_key("LOG_CHANNEL"),
"Session String expired, create new session from π",
buttons=[
Button.url("Bot", "t.me/SessionGeneratorBot?start="),
Button.url(
"Repl",
"https://replit.com/@TheUltroid/UltroidStringSession",
),
],
)
sys.exit()
except events.StopPropagation:
raise events.StopPropagation
except KeyboardInterrupt:
pass
except Exception as e:
LOGS.exception(e)
date = strftime("%Y-%m-%d %H:%M:%S", gmtime())
naam = get_display_name(chat)
ftext = "**Ultroid Client Error:** `Forward this to` @UltroidSupportChat\n\n"
ftext += "**Py-Ultroid Version:** `" + str(pyver)
ftext += "`\n**Ultroid Version:** `" + str(ult_ver)
ftext += "`\n**Telethon Version:** `" + str(telever)
ftext += f"`\n**Hosted At:** `{HOSTED_ON}`\n\n"
ftext += "--------START ULTROID CRASH LOG--------"
ftext += "\n**Date:** `" + date
ftext += "`\n**Group:** `" + str(ult.chat_id) + "` " + str(naam)
ftext += "\n**Sender ID:** `" + str(ult.sender_id)
ftext += "`\n**Replied:** `" + str(ult.is_reply)
ftext += "`\n\n**Event Trigger:**`\n"
ftext += str(ult.text)
ftext += "`\n\n**Traceback info:**`\n"
ftext += str(format_exc())
ftext += "`\n\n**Error text:**`\n"
ftext += str(sys.exc_info()[1])
ftext += "`\n\n--------END ULTROID CRASH LOG--------"
ftext += "\n\n\n**Last 5 commits:**`\n"
stdout, stderr = await bash('git log --pretty=format:"%an: %s" -5')
result = stdout + (stderr or "")
ftext += f"{result}`"
if len(ftext) > 4096:
with BytesIO(ftext.encode()) as file:
file.name = "logs.txt"
error_log = await asst.send_file(
udB.get_key("LOG_CHANNEL"),
file,
caption="**Ultroid Client Error:** `Forward this to` @UltroidSupportChat\n\n",
)
else:
error_log = await asst.send_message(
udB.get_key("LOG_CHANNEL"),
ftext,
)
if ult.out:
await ult.edit(
f"<b><a href={error_log.message_link}>[An error occurred]</a></b>",
link_preview=False,
parse_mode="html",
)
cmd = None
blacklist_chats = False
chats = None
if black_list_chats:
blacklist_chats = True
chats = list(black_list_chats)
_add_new = allow_sudo and HNDLR != SUDO_HNDLR
if _add_new:
if pattern:
cmd = compile_pattern(pattern, SUDO_HNDLR)
ultroid_bot.add_event_handler(
wrapp,
NewMessage(
pattern=cmd,
incoming=True,
forwards=False,
func=func,
chats=chats,
blacklist_chats=blacklist_chats,
),
)
if pattern:
cmd = compile_pattern(pattern, HNDLR)
ultroid_bot.add_event_handler(
wrapp,
NewMessage(
outgoing=True if _add_new else None,
pattern=cmd,
forwards=False,
func=func,
chats=chats,
blacklist_chats=blacklist_chats,
),
)
if TAKE_EDITS:
def func_(x):
return not x.via_bot_id and not (x.is_channel and x.chat.broadcast)
ultroid_bot.add_event_handler(
wrapp,
MessageEdited(
pattern=cmd,
forwards=False,
func=func_,
chats=chats,
blacklist_chats=blacklist_chats,
),
)
if manager and MANAGER:
allow_all = kwargs.get("allow_all", False)
allow_pm = kwargs.get("allow_pm", False)
require = kwargs.get("require", None)
async def manager_cmd(ult):
if not allow_all and not (await admin_check(ult, require=require)):
return
if not allow_pm and ult.is_private:
return
try:
await dec(ult)
except Exception as er:
if chat := udB.get_key("MANAGER_LOG"):
text = f"**#MANAGER_LOG\n\nChat:** `{get_display_name(ult.chat)}` `{ult.chat_id}`"
text += f"\n**Replied :** `{ult.is_reply}`\n**Command :** {ult.text}\n\n**Error :** `{er}`"
try:
return await asst.send_message(
chat, text, link_preview=False
)
except Exception as er:
LOGS.exception(er)
LOGS.info(f"β’ MANAGER [{ult.chat_id}]:")
LOGS.exception(er)
if pattern:
cmd = compile_pattern(pattern, "/")
asst.add_event_handler(
manager_cmd,
NewMessage(
pattern=cmd,
forwards=False,
incoming=True,
func=func,
chats=chats,
blacklist_chats=blacklist_chats,
),
)
if DUAL_MODE and not (manager and DUAL_HNDLR == "/"):
if pattern:
cmd = compile_pattern(pattern, DUAL_HNDLR)
asst.add_event_handler(
wrapp,
NewMessage(
pattern=cmd,
incoming=True,
forwards=False,
func=func,
chats=chats,
blacklist_chats=blacklist_chats,
),
)
file = Path(inspect.stack()[1].filename)
if "addons/" in str(file):
if LOADED.get(file.stem):
LOADED[file.stem].append(wrapp)
else:
LOADED.update({file.stem: [wrapp]})
if pattern:
if LIST.get(file.stem):
LIST[file.stem].append(pattern)
else:
LIST.update({file.stem: [pattern]})
return wrapp
return decor
|