Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 87 additions & 26 deletions multi_client.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,100 @@
import asyncio
import os
import subprocess
import sys
import time
from dotenv import load_dotenv

vars = ["API_ID", "API_HASH", "SESSION"]

def _check(z):
new = []
for var in vars:
ent = os.environ.get(var + z)
if not ent:
return False, new
new.append(ent)
return True, new

for z in range(5):
n = str(z + 1)
if z == 0:
z = ""
fine, out = _check(str(z))
if fine:
load_dotenv()

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

REQUIRED_VARS = ["API_ID", "API_HASH", "SESSION"]
BASE_VARS = ["API_ID", "API_HASH"]

def _check_and_launch(suffix):

env_vars_to_pass = {}
found_all = True
client_id = suffix if suffix else "1"

print(f"Checking configuration for Client ID {client_id}...")

for var in REQUIRED_VARS:
full_var_name = var + suffix
value = os.environ.get(full_var_name)

if not value:
# Skip checking for CLIENT1 if CLIENT1+ exists
if suffix == "" and os.environ.get(var + "1"):
found_all = False
return False

found_all = False
print(f" ⚠️ Skipping Client {client_id} because '{full_var_name}' was not found.")
return False

env_vars_to_pass[var] = value

if found_all:
print(f" ✅ Variables found. Launching Client {client_id}...")

process_env = os.environ.copy()

for base_var in BASE_VARS:
base_value = os.environ.get(base_var)
if base_value:
process_env[base_var] = base_value

process_env.update(env_vars_to_pass)

process_env['CLIENT_ID'] = client_id

# --- Setting Explicit Session Name (SESSION_NAME) ---
if client_id == "1":
process_env['SESSION_NAME'] = "asst" # asst.session
elif client_id == "2":
process_env['SESSION_NAME'] = "userbot" # userbot.session
else:
process_env['SESSION_NAME'] = f"ultroid_client_{client_id}"
# ----------------------------------------------------

# --- Setting PYTHONPATH ---
current_pythonpath = process_env.get('PYTHONPATH', '')
process_env['PYTHONPATH'] = f"{current_pythonpath}:{BASE_DIR}"
# --------------------------

# CWD IS SET TO BASE_DIR FOR ALL CLIENTS
client_cwd = BASE_DIR

subprocess.Popen(
[sys.executable, "-m", "pyUltroid", out[0], out[1], out[2], out[3], out[4], n],
[sys.executable, "-m", "pyUltroid"],
stdin=None,
stderr=None,
stdout=None,
cwd=None,
# CWD is always BASE_DIR
cwd=client_cwd,
env=process_env,
close_fds=True,
)
return True
return False

# -------------------------------------------------------------

print("--- Starting Primary Client Check (ID 1) ---")
primary_client_launched = _check_and_launch("")

loop = asyncio.get_event_loop()
print("\n--- Starting Additional Client Check (ID 2 through 5) ---")
for i in range(2, 6):
_check_and_launch(str(i))

# -------------------------------------------------------------

try:
loop.run_forever()
print("\nLauncher remains active to keep PyUltroid client processes running.")
while True:
time.sleep(3600)
except KeyboardInterrupt:
print("Launcher stopped manually.")
except Exception as er:
print(er)
finally:
loop.close()

print(f"Error in main loop: {er}")
Loading