Skip to content

Commit

Permalink
cancel regex, attempt to fix tg download, fix PORT logic for VPS
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Dec 29, 2024
1 parent 9d99e0e commit e19a231
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 85 deletions.
7 changes: 6 additions & 1 deletion bot/core/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def add_handlers():
BotCommands.BotSetCommand,
CustomFilters.sudo,
),
"cancel": (cancel, BotCommands.CancelTaskCommand, CustomFilters.authorized),
"cancel_all_buttons": (
cancel_all_buttons,
BotCommands.CancelAllCommand,
Expand Down Expand Up @@ -151,3 +150,9 @@ def add_handlers():
& CustomFilters.owner,
),
)
TgClient.bot.add_handler(
MessageHandler(
cancel,
filters=regex(r"^/stop(_\w+)?(?!all)") & CustomFilters.authorized,
),
)
2 changes: 1 addition & 1 deletion bot/core/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async def load_configurations():
subprocess.run(["chmod", "600", ".netrc"], check=False)
subprocess.run(["cp", ".netrc", "/root/.netrc"], check=False)

PORT = environ.get("BASE_URL_PORT") or environ.get("PORT")
PORT = environ.get("PORT") or environ.get("BASE_URL_PORT", 80)
await create_subprocess_shell(
f"gunicorn web.wserver:app --bind 0.0.0.0:{PORT} --worker-class gevent",
)
Expand Down
2 changes: 1 addition & 1 deletion bot/helper/ext_utils/status_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ async def get_readable_message(sid, is_user, page_no=1, status="All", page_step=
msg += f" | <b>Time: </b>{task.seeding_time()}"
else:
msg += f"\n<b>Size: </b>{task.size()}"
msg += f"\n<b>Gid: </b><code>{task.gid()}</code>\n\n"
msg += f"\n/stop_{task.gid()}\n\n"

if len(msg) == 0:
if status == "All":
Expand Down
17 changes: 2 additions & 15 deletions bot/helper/mirror_leech_utils/download_utils/telegram_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,10 @@ async def _download(self, message, path):

async def add_download(self, message, path, session):
self.session = session
if (
self.session is None
and self._listener.user_transmission
and self._listener.is_super_chat
):
self.session = "user"
message = await TgClient.user.get_messages(
chat_id=message.chat.id,
message_ids=message.id,
)
elif self.session and self.session != TgClient.bot:
if self.session != TgClient.bot:
message = await self.session.get_messages(
chat_id=message.chat.id,
message_ids=message.id,
chat_id=message.chat.id, message_ids=message.id
)
else:
self.session = "bot"

media = (
message.document
Expand Down
85 changes: 41 additions & 44 deletions bot/helper/telegram_helper/bot_commands.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,61 @@
from bot.core.config_manager import Config

i = Config.CMD_SUFFIX

class _BotCommands:
def __init__(self):
self.StartCommand = f"start{Config.CMD_SUFFIX}"
self.MirrorCommand = [f"mirror{Config.CMD_SUFFIX}", f"m{Config.CMD_SUFFIX}"]
self.StartCommand = f"start{i}"
self.MirrorCommand = [f"mirror{i}", f"m{i}"]
self.QbMirrorCommand = [
f"qbmirror{Config.CMD_SUFFIX}",
f"qm{Config.CMD_SUFFIX}",
f"qbmirror{i}",
f"qm{i}",
]
self.YtdlCommand = [f"ytdl{Config.CMD_SUFFIX}", f"y{Config.CMD_SUFFIX}"]
self.LeechCommand = [f"leech{Config.CMD_SUFFIX}", f"l{Config.CMD_SUFFIX}"]
self.YtdlCommand = [f"ytdl{i}", f"y{i}"]
self.LeechCommand = [f"leech{i}", f"l{i}"]
self.QbLeechCommand = [
f"qbleech{Config.CMD_SUFFIX}",
f"ql{Config.CMD_SUFFIX}",
f"qbleech{i}",
f"ql{i}",
]
self.YtdlLeechCommand = [
f"ytdlleech{Config.CMD_SUFFIX}",
f"yl{Config.CMD_SUFFIX}",
f"ytdlleech{i}",
f"yl{i}",
]
self.CloneCommand = f"clone{Config.CMD_SUFFIX}"
self.CountCommand = f"count{Config.CMD_SUFFIX}"
self.DeleteCommand = f"del{Config.CMD_SUFFIX}"
self.CancelTaskCommand = [
f"cancel{Config.CMD_SUFFIX}",
f"c{Config.CMD_SUFFIX}",
]
self.CancelAllCommand = f"cancelall{Config.CMD_SUFFIX}"
self.CloneCommand = f"clone{i}"
self.CountCommand = f"count{i}"
self.DeleteCommand = f"del{i}"
self.CancelAllCommand = f"cancelall{i}"
self.ForceStartCommand = [
f"forcestart{Config.CMD_SUFFIX}",
f"fs{Config.CMD_SUFFIX}",
f"forcestart{i}",
f"fs{i}",
]
self.ListCommand = f"list{Config.CMD_SUFFIX}"
self.SearchCommand = f"search{Config.CMD_SUFFIX}"
self.StatusCommand = f"status{Config.CMD_SUFFIX}"
self.UsersCommand = f"users{Config.CMD_SUFFIX}"
self.AuthorizeCommand = f"authorize{Config.CMD_SUFFIX}"
self.UnAuthorizeCommand = f"unauthorize{Config.CMD_SUFFIX}"
self.AddSudoCommand = f"addsudo{Config.CMD_SUFFIX}"
self.RmSudoCommand = f"rmsudo{Config.CMD_SUFFIX}"
self.PingCommand = f"ping{Config.CMD_SUFFIX}"
self.RestartCommand = f"restart{Config.CMD_SUFFIX}"
self.RestartSessionsCommand = f"restartses{Config.CMD_SUFFIX}"
self.StatsCommand = f"stats{Config.CMD_SUFFIX}"
self.HelpCommand = f"help{Config.CMD_SUFFIX}"
self.LogCommand = f"log{Config.CMD_SUFFIX}"
self.ShellCommand = f"shell{Config.CMD_SUFFIX}"
self.AExecCommand = f"aexec{Config.CMD_SUFFIX}"
self.ExecCommand = f"exec{Config.CMD_SUFFIX}"
self.ClearLocalsCommand = f"clearlocals{Config.CMD_SUFFIX}"
self.ListCommand = f"list{i}"
self.SearchCommand = f"search{i}"
self.StatusCommand = f"status{i}"
self.UsersCommand = f"users{i}"
self.AuthorizeCommand = f"authorize{i}"
self.UnAuthorizeCommand = f"unauthorize{i}"
self.AddSudoCommand = f"addsudo{i}"
self.RmSudoCommand = f"rmsudo{i}"
self.PingCommand = f"ping{i}"
self.RestartCommand = f"restart{i}"
self.RestartSessionsCommand = f"restartses{i}"
self.StatsCommand = f"stats{i}"
self.HelpCommand = f"help{i}"
self.LogCommand = f"log{i}"
self.ShellCommand = f"shell{i}"
self.AExecCommand = f"aexec{i}"
self.ExecCommand = f"exec{i}"
self.ClearLocalsCommand = f"clearlocals{i}"
self.BotSetCommand = [
f"bsetting{Config.CMD_SUFFIX}",
f"bs{Config.CMD_SUFFIX}",
f"bsetting{i}",
f"bs{i}",
]
self.UserSetCommand = [
f"usetting{Config.CMD_SUFFIX}",
f"us{Config.CMD_SUFFIX}",
f"usetting{i}",
f"us{i}",
]
self.SelectCommand = f"sel{Config.CMD_SUFFIX}"
self.RssCommand = f"rss{Config.CMD_SUFFIX}"
self.SelectCommand = f"sel{i}"
self.RssCommand = f"rss{i}"


BotCommands = _BotCommands()
22 changes: 11 additions & 11 deletions bot/helper/telegram_helper/message_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,26 @@ async def delete_status():
async def get_tg_link_message(link, user_id=""):
message = None
links = []
user_s = None
user_session = None

if user_id:
if user_id in session_cache:
user_s = session_cache[user_id]
user_session = session_cache[user_id]
else:
user_dict = user_data.get(user_id, {})
session_string = user_dict.get("session_string")
if session_string:
user_s = Client(
user_session = Client(
f"session_{user_id}",
Config.TELEGRAM_API,
Config.TELEGRAM_HASH,
session_string=session_string,
no_updates=True,
)
await user_s.start()
session_cache[user_id] = user_s
await user_session.start()
session_cache[user_id] = user_session
else:
user_s = TgClient.user
user_session = TgClient.user

if link.startswith("https://t.me/"):
private = False
Expand All @@ -221,7 +221,7 @@ async def get_tg_link_message(link, user_id=""):
r"tg:\/\/openmessage\?user_id=([0-9]+)&message_id=([0-9-]+)",
link,
)
if not TgClient.user:
if not user_session:
raise TgLinkException(
"USER_SESSION_STRING required for this private link!",
)
Expand Down Expand Up @@ -260,21 +260,21 @@ async def get_tg_link_message(link, user_id=""):
private = True
except Exception as e:
private = True
if not user_s:
if not user_session:
raise e

if not private:
return (links, TgClient.bot) if links else (message, TgClient.bot)
if user_s:
if user_session:
try:
user_message = await user_s.get_messages(
user_message = await user_session.get_messages(
chat_id=chat,
message_ids=msg_id,
)
except Exception as e:
raise TgLinkException("We don't have access to this chat!") from e
if not user_message.empty:
return (links, user_s) if links else (user_message, user_s)
return (links, user_session) if links else (user_message, user_session)
return None
raise TgLinkException("Private: Please report!")

Expand Down
15 changes: 5 additions & 10 deletions bot/modules/cancel_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,28 @@
@new_task
async def cancel(_, message):
user_id = message.from_user.id if message.from_user else message.sender_chat.id
msg = message.text.split()
msg = message.text.split("_", maxsplit=1)
await delete_message(message)
if len(msg) > 1:
gid = msg[1]
gid = msg[1].split("@", maxsplit=1)
gid = gid[0]
if len(gid) == 4:
multi_tags.discard(gid)
return
task = await get_task_by_gid(gid)
if task is None:
await send_message(message, f"GID: <code>{gid}</code> Not Found.")
await delete_message(message)
return
elif reply_to_id := message.reply_to_message_id:
async with task_dict_lock:
task = task_dict.get(reply_to_id)
if task is None:
await send_message(message, "This is not an active task!")
return
elif len(msg) == 1:
msg = (
"Reply to an active Command message which was used to start the download"
f" or send <code>/{BotCommands.CancelTaskCommand[0]} GID</code> to cancel it!"
)
await send_message(message, msg)
return
if user_id not in (Config.OWNER_ID, task.listener.user_id) and (
user_id not in user_data or not user_data[user_id].get("is_sudo")
):
await send_message(message, "This task is not for you!")
return
obj = task.task()
await obj.cancel_task()
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/mirror_leech.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async def new_event(self):
seed_time = None
reply_to = None
file_ = None
session = ""
session = TgClient.bot

try:
self.multi = int(args["-i"])
Expand Down
2 changes: 1 addition & 1 deletion config_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
# qBittorrent/Aria2c
TORRENT_TIMEOUT = 0
BASE_URL = ""
BASE_URL_PORT = 0
BASE_URL_PORT = 80
WEB_PINCODE = False

# Queueing system
Expand Down

0 comments on commit e19a231

Please sign in to comment.