Skip to content

Commit

Permalink
fix ytdl, unload musicbot
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiTheModder committed Aug 6, 2024
1 parent 4195ade commit 1821927
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions modules/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# (at your option) any later version.
import hashlib
import os
import shutil
import subprocess
import sys

import requests
from pyrogram import Client, filters
Expand Down Expand Up @@ -141,6 +144,9 @@ async def unload_mods(_, message: Message):

if os.path.exists(f"{BASE_PATH}/modules/custom_modules/{module_name}.py"):
os.remove(f"{BASE_PATH}/modules/custom_modules/{module_name}.py")
if module_name == "musicbot":
subprocess.run([sys.executable, "-m", "pip", "uninstall", "-y", "requirements.txt"], cwd=f"{BASE_PATH}/musicbot")
shutil.rmtree(f"{BASE_PATH}/musicbot")
await message.edit(f"<b>The module <code>{module_name}</code> removed!</b>")
restart()
elif os.path.exists(f"{BASE_PATH}/modules/{module_name}.py"):
Expand Down
25 changes: 24 additions & 1 deletion utils/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
from typing import Dict, Tuple

from PIL import Image
import psutil
from pyrogram import Client, errors, enums
from pyrogram.errors import FloodWait, MessageNotModified
from pyrogram.types import Message

from utils import db

from .misc import modules_help, prefix, requirements_list

META_COMMENTS = re.compile(r"^ *# *meta +(\S+) *: *(.*?)\s*$", re.MULTILINE)
Expand Down Expand Up @@ -211,6 +214,13 @@ def text(message: Message) -> str:


def restart() -> None:
music_bot_pid = db.get("custom.musicbot", "music_bot_pid", None)
if music_bot_pid is not None:
try:
music_bot_process = psutil.Process(music_bot_pid)
music_bot_process.terminate()
except psutil.NoSuchProcess:
print("Music bot is not running.")
os.execvp(sys.executable, [sys.executable, "main.py"])


Expand Down Expand Up @@ -318,14 +328,27 @@ def import_library(library_name: str, package_name: str = None):
return importlib.import_module(library_name)
except ImportError as exc:
completed = subprocess.run(
[sys.executable, "-m", "pip", "install", package_name], check=True)
[sys.executable, "-m", "pip", "install", "--upgrade", package_name], check=True)
if completed.returncode != 0:
raise AssertionError(
f"Failed to install library {package_name} (pip exited with code {completed.returncode})"
) from exc
return importlib.import_module(library_name)


def uninstall_library(package_name: str):
"""
Uninstalls a library
:param package_name: package name in PyPi (pip uninstall example)
"""
completed = subprocess.run(
[sys.executable, "-m", "pip", "uninstall", "-y", package_name], check=True)
if completed.returncode != 0:
raise AssertionError(
f"Failed to uninstall library {package_name} (pip exited with code {completed.returncode})"
)


def resize_image(
input_img, output=None, img_type="PNG", size: int = 512, size2: int = None
):
Expand Down

0 comments on commit 1821927

Please sign in to comment.