dotfiles/.local/bin/custom/mcpelauncher-rpc

70 lines
2.1 KiB
Python
Executable file

#!/usr/bin/env python3
import sys, getopt, pypresence, time, psutil
from subprocess import check_output
# ----- RPC Client ID
RPC = pypresence.Presence("731745989039489036")
# ----- Functions
def get_pid(name):
return map(int,check_output(["pidof","-s",name]).split())
def connect_rpc():
while True:
try:
RPC.connect()
break
except ConnectionRefusedError as e:
print("Failed to connect to RPC! Trying again in 10 seconds...")
time.sleep(10)
except (FileNotFoundError, AttributeError) as e:
print("RPC failed to connect due to Discord not being opened yet.")
time.sleep(10)
def check_mcbe():
# get_pid("mcpelauncher-client")
return 'MINECRAFT MAIN ' in (p.name() for p in psutil.process_iter())
def update_rpc():
RPC.update(details=mcbe_game, large_image='minecraft', large_text=mcbe_ltext, small_image='world', small_text=mcbe_stext, start=start_time)
if debug is True:
print(color.BOLD + '[ ' + color.END + color.GREEN + 'OK' + color.END + color.BOLD + ' ] ' + color.END + 'RPC has been updated.')
time.sleep(5)
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
# ----- Command-Line Arguments + Config
debug=False
mcbe_version=str(sys.argv[1])
mcbe_game=str("v" + mcbe_version)
mcbe_ltext=str("Using Linux MCPE Launcher")
mcbe_ign="Zerogame1432"
mcbe_stext="IGN: " + mcbe_ign
start_time=int(time.time())
# ----- Connect to RPC
connect_rpc()
# ----- Loops
while True:
try:
if check_mcbe() == True or debug == True:
update_rpc()
else:
print(color.BOLD + '[ ' + color.END + color.RED + 'ERROR' + color.END + color.BOLD + ' ] ' + color.END + 'Minecraft is not running')
RPC.close
break
except KeyboardInterrupt as kb:
print('\n' + color.BOLD + '[ ' + color.END + color.RED + 'ERROR' + color.END + color.BOLD + ' ] ' + color.END + 'RPC interrupted, closing...')
RPC.close
break