2022-02-26 13:44:28 +00:00
|
|
|
"""
|
|
|
|
Video + Music Stream Telegram Bot
|
|
|
|
Copyright (c) 2022-present levina=lab <https://github.com/levina-lab>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but without any warranty; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/licenses.html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
2022-01-31 12:41:47 +00:00
|
|
|
|
2022-02-10 09:43:17 +00:00
|
|
|
from git import Repo
|
2022-03-01 14:15:05 +00:00
|
|
|
from git.exc import InvalidGitRepositoryError, GitCommandError
|
2022-02-11 06:43:58 +00:00
|
|
|
|
|
|
|
from pyrogram.types import Message
|
|
|
|
from pyrogram import Client, filters
|
2022-02-26 13:44:28 +00:00
|
|
|
|
|
|
|
from program import LOGS
|
2022-03-01 14:15:05 +00:00
|
|
|
from config import UPSTREAM_BRANCH, UPSTREAM_REPO, BOT_USERNAME
|
2022-01-31 12:41:47 +00:00
|
|
|
|
2022-02-11 06:43:58 +00:00
|
|
|
from driver.filters import command
|
|
|
|
from driver.decorators import bot_creator
|
2022-03-01 14:15:05 +00:00
|
|
|
from driver.database.dbqueue import get_active_chats, remove_active_chat
|
2022-01-31 12:41:47 +00:00
|
|
|
|
2022-02-10 09:46:08 +00:00
|
|
|
|
2022-03-01 14:15:05 +00:00
|
|
|
@Client.on_message(command(["update", f"update@{BOT_USERNAME}"]) & ~filters.edited)
|
|
|
|
@bot_creator
|
|
|
|
async def update_bot(_, message: Message):
|
|
|
|
chat_id = message.chat.id
|
|
|
|
msg = await bot.send_message(chat_id, "❖ Checking updates...")
|
2022-01-31 12:41:47 +00:00
|
|
|
try:
|
|
|
|
repo = Repo()
|
2022-03-01 14:15:05 +00:00
|
|
|
except GitCommandError:
|
|
|
|
await msg.edit("Git command error !")
|
|
|
|
return
|
2022-01-31 12:41:47 +00:00
|
|
|
except InvalidGitRepositoryError:
|
2022-03-01 14:15:05 +00:00
|
|
|
await msg.edit("❖ Checking Git updates...")
|
2022-02-10 21:56:59 +00:00
|
|
|
repo = Repo.init()
|
2022-03-01 14:15:05 +00:00
|
|
|
if "origin" in repo.remotes:
|
|
|
|
origin = repo.remote("origin")
|
|
|
|
else:
|
|
|
|
origin = repo.create_remote("origin", UPSTREAM_REPO)
|
2022-02-10 21:56:59 +00:00
|
|
|
origin.fetch()
|
2022-03-01 14:15:05 +00:00
|
|
|
repo.create_head(UPSTREAM_BRANCH, origin.refs[UPSTREAM_BRANCH])
|
|
|
|
repo.heads[UPSTREAM_BRANCH].set_tracking_branch(
|
|
|
|
origin.refs[UPSTREAM_BRANCH]
|
|
|
|
)
|
|
|
|
repo.heads[UPSTREAM_BRANCH].checkout(True)
|
|
|
|
try:
|
|
|
|
repo.create_remote("origin", UPSTREAM_REPO)
|
|
|
|
except BaseException:
|
|
|
|
pass
|
|
|
|
nrs = repo.remote("origin")
|
|
|
|
nrs.fetch(UPSTREAM_BRANCH)
|
|
|
|
try:
|
|
|
|
nrs.pull(UPSTREAM_BRANCH)
|
|
|
|
except GitCommandError:
|
|
|
|
repo.git.reset("--hard", "FETCH_HEAD")
|
|
|
|
await install_requirements(
|
|
|
|
"pip3 install --no-cache-dir -r requirements.txt"
|
|
|
|
)
|
|
|
|
await msg.edit("Done, bot has updated !\n\n> now the bot will be rebooted to apply the update and will active again in 5-10 second(s).")
|
|
|
|
return
|
|
|
|
served_chats = []
|
|
|
|
try:
|
|
|
|
chats = await get_active_chats()
|
|
|
|
for chat in chats:
|
|
|
|
served_chats.append(int(chat["chat_id"]))
|
|
|
|
except BaseException:
|
|
|
|
pass
|
|
|
|
for x in served_chats:
|
|
|
|
try:
|
|
|
|
await remove_active_chat(x)
|
|
|
|
except BaseException:
|
|
|
|
pass
|
2022-02-10 21:56:59 +00:00
|
|
|
return
|
2022-03-01 14:15:05 +00:00
|
|
|
os.system(f"kill -9 {os.getpid()} && python3 main.py")
|
2022-02-10 21:56:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@Client.on_message(command(["restart", f"restart@{BOT_USERNAME}"]) & ~filters.edited)
|
2022-02-10 09:43:17 +00:00
|
|
|
@bot_creator
|
2022-01-31 12:41:47 +00:00
|
|
|
async def restart_bot(_, message: Message):
|
2022-02-26 13:44:28 +00:00
|
|
|
try:
|
|
|
|
msg = await message.reply_text("❖ Restarting bot...")
|
2022-03-01 14:15:05 +00:00
|
|
|
LOGS.info("[INFO]: >> BOT SERVER RESTARTED <<")
|
2022-02-26 13:44:28 +00:00
|
|
|
except BaseException as err:
|
|
|
|
LOGS.info(f"[ERROR]: {err}")
|
|
|
|
return
|
|
|
|
await msg.edit_text("✅ Bot has restarted !\n\n» back active again in 5-10 seconds.")
|
|
|
|
os.system(f"kill -9 {os.getpid()} && python3 main.py")
|