File size: 5,564 Bytes
b779216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from pypresence import Presence
from pypresence.exceptions import DiscordNotFound, InvalidPipe
import datetime as dt
import threading
import functools

class RichPresenceManager:
    def __init__(self):
        self.client_id = "1339001292319621181"
        self.rpc = None
        self.running = False
        self.current_state = "Idling"
        self.lock = threading.Lock()
        self.discord_available = True
        
        self.presence_configs = {
            # Roformer
            "Performing BS/Mel Roformer Separation": {
                "small_image": "roformer",
                "small_text": "BS/Mel Roformer"
            },
            "Performing BS/Mel Roformer Batch Separation": {
                "small_image": "roformer",
                "small_text": "BS/Mel Roformer"
            },
            # MDXC
            "Performing MDXC Separationn": {
                "small_image": "mdxc",
                "small_text": "MDXC"
            },
            "Performing MDXC Batch Separation": {
                "small_image": "mdxc",
                "small_text": "MDXC"
            },
            # MDX-NET
            "Performing MDX-NET Separation": {
                "small_image": "mdxnet",
                "small_text": "MDX-NET"
            },
            "Performing MDX-NET Batch Separation": {
                "small_image": "mdxnet",
                "small_text": "MDX-NET"
            },
            # VR Arch
            "Performing VR Arch Separation": {
                "small_image": "vrarch",
                "small_text": "VR Arch"
            },
            "Performing VR Arch Batch Separation": {
                "small_image": "vrarch",
                "small_text": "VR Arch"
            },
            # Demucs
            "Performing Demucs Separation": {
                "small_image": "demucs",
                "small_text": "Demucs"
            },
            "Performing Demucs Batch Separation": {
                "small_image": "demucs",
                "small_text": "Demucs"
            },
            # Idling
            "Idling": {
                "small_image": "idling",
                "small_text": "Idling"
            }
        }

    def get_presence_config(self, state):
        return self.presence_configs.get(state, self.presence_configs["Idling"])

    def start_presence(self):
        try:
            if not self.running:
                self.rpc = Presence(self.client_id)
                try:
                    self.rpc.connect()
                    self.running = True
                    self.discord_available = True
                    self.update_presence()
                    print("Discord Rich Presence connected successfully")
                except (DiscordNotFound, InvalidPipe):
                    print("Discord is not running. Rich Presence will be disabled.")
                    self.discord_available = False
                    self.running = False
                    self.rpc = None
                except Exception as error:
                    print(f"An error occurred connecting to Discord: {error}")
                    self.discord_available = False
                    self.running = False
                    self.rpc = None
        except Exception as e:
            print(f"Unexpected error in start_presence: {e}")
            self.discord_available = False
            self.running = False
            self.rpc = None

    def update_presence(self):
        if self.rpc and self.running and self.discord_available:
            try:
                config = self.get_presence_config(self.current_state)
                self.rpc.update(
                    state=self.current_state,
                    details="Ultimate Vocal Remover 5 Gradio UI",
                    buttons=[{"label": "Download", "url": "https://github.com/Eddycrack864/UVR5-UI"}],
                    large_image="logo",
                    large_text="Separating tracks with UVR5 UI",
                    small_image=config["small_image"],
                    small_text=config["small_text"],
                    start=dt.datetime.now().timestamp(),
                )
            except Exception as e:
                print(f"Error updating Discord presence: {e}")
                self.discord_available = False
                self.cleanup()

    def set_state(self, state):
        if self.discord_available:
            with self.lock:
                self.current_state = state
                if self.running:
                    self.update_presence()

    def cleanup(self):
        self.running = False
        if self.rpc and self.discord_available:
            try:
                self.rpc.close()
            except:
                pass
        self.rpc = None
        self.discord_available = False

    def stop_presence(self):
        self.cleanup()

RPCManager = RichPresenceManager()

def track_presence(state_message):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            if RPCManager.running and RPCManager.discord_available:
                RPCManager.set_state(state_message)
            try:
                result = func(*args, **kwargs)
                return result
            finally:
                if RPCManager.running and RPCManager.discord_available:
                    RPCManager.set_state("Idling")
        return wrapper
    return decorator