From 362ad35a5f36eec6a588bc6c694dab0a8da5978f Mon Sep 17 00:00:00 2001 From: ApfelTeeSaft <91074565+ApfelTeeSaft@users.noreply.github.com> Date: Mon, 23 Oct 2023 17:09:56 +0200 Subject: [PATCH] Create rpc.py --- rpc.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 rpc.py diff --git a/rpc.py b/rpc.py new file mode 100644 index 0000000..4eb7223 --- /dev/null +++ b/rpc.py @@ -0,0 +1,41 @@ +import os +import time +import pygetwindow as gw +from pypresence import Presence + +# Define the path to the configuration file +config_file_path = "config.txt" + +# Check if the configuration file exists, and if not, prompt for the Client ID +if not os.path.exists(config_file_path): + client_id = input("Enter your Discord application's Client ID: ") + with open(config_file_path, "w") as config_file: + config_file.write(client_id) +else: + with open(config_file_path, "r") as config_file: + client_id = config_file.read() + +# Initialize Discord presence +RPC = Presence(client_id) +RPC.connect() + +while True: + try: + # Get the currently focused window + focused_window = gw.getWindowsWithTitle(gw.getActiveWindow().title)[0] + + # Set the Discord Rich Presence data + presence_data = { + "state": focused_window.title, + "large_text": "https://assets-global.website-files.com/646218c67da47160c64a84d5/6463428fbecdbd5e3e1ba1c7_29.png", + "large_image": "https://assets-global.website-files.com/646218c67da47160c64a84d5/6463428fbecdbd5e3e1ba1c7_29.png", + } + + # Update the Discord status + RPC.update(**presence_data) + + except IndexError: + # If no window is focused, set a default presence + RPC.update(state="Idle") + + time.sleep(5) # Adjust the update interval as needed