From c5a80edac0eba294a1fd0f76c9d252ebf4f4afeb Mon Sep 17 00:00:00 2001 From: "Juraj K." <140317448+mostypc123@users.noreply.github.com> Date: Thu, 23 Jan 2025 19:54:30 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20added=20discord=20presence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/discord.xcfg | 1 + src/main.py | 54 +++++++++++++++++++++++++++++++++++++++++++- src/requirements.txt | 1 + src/settings.py | 31 ++++++++++++++++++------- 4 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 src/discord.xcfg diff --git a/src/discord.xcfg b/src/discord.xcfg new file mode 100644 index 0000000..4791ed5 --- /dev/null +++ b/src/discord.xcfg @@ -0,0 +1 @@ +True \ No newline at end of file diff --git a/src/main.py b/src/main.py index 5e80ea1..533bd7f 100644 --- a/src/main.py +++ b/src/main.py @@ -11,6 +11,7 @@ ## Other import json import threading +from pypresence import Presence import pywinstyles import webbrowser # Local imports @@ -30,6 +31,9 @@ class TextEditor(wx.Frame): def __init__(self, *args, **kwargs): super(TextEditor, self).__init__(*args, **kwargs) + # Initialize RPC as None by default + self.RPC = None + # Load config values from the xcfg file config = self.load_config("xedix.xcfg") self.active_color = config.get("headerActive", "#EDF0F2") # Default values if not found @@ -43,7 +47,27 @@ def __init__(self, *args, **kwargs): pass current_dir = os.getcwd() + + with open('discord.xcfg', 'r') as file: + presence = file.read() + + if presence == "True": + try: + CLIENT_ID = '1332012158376083528' + self.RPC = Presence(CLIENT_ID) # Store as instance variable + self.RPC.connect() + self.RPC.update( + state="XediX", + details="Idling", + large_image="xedix_logo", + large_text="XediX", + small_text="XediX" + ) + except Exception as e: + print(f"Could not connect to Discord: {e}") + self.RPC = None # Ensure RPC is None if connection fails + # Bind focus events for dynamic color change self.Bind(wx.EVT_ACTIVATE, self.on_activate) @@ -739,6 +763,20 @@ def on_text_change(event): text_area.SetText(content) text_area.SetTabWidth(4) text_area.SetWindowStyleFlag(wx.NO_BORDER) + + # Update Discord RPC only if it's initialized and connected + try: + if self.RPC: + self.RPC.update( + state="XediX", + details=f"Editing {file_name}", + large_image="xedix_logo", + large_text="XediX", + small_text="XediX" + ) + except Exception as e: + print(f"Could not update Discord status: {e}") + self.RPC = None # Reset RPC if connection is lost self.SetStatusText(f" Opened file: {file_name}") @@ -1117,6 +1155,20 @@ def OnSave(self, event): text_area = editor_splitter.GetChildren()[0] # Get the main editor area content = text_area.GetValue() file_name = self.notebook.GetPageText(self.notebook.GetSelection()) + + # Update Discord RPC only if it's initialized and connected + try: + if self.RPC: + self.RPC.update( + state="XediX", + details=f"Editing {file_name}", + large_image="xedix_logo", + large_text="XediX", + small_text="XediX" + ) + except Exception as e: + print(f"Could not update Discord status: {e}") + self.RPC = None # Reset RPC if connection is lost # Add syntax checking for Python files if file_name.endswith('.py'): @@ -1257,4 +1309,4 @@ def main(): if __name__ == '__main__': """Runs the main process.""" - main() + main() \ No newline at end of file diff --git a/src/requirements.txt b/src/requirements.txt index e4520e3..f65bf53 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -6,3 +6,4 @@ pyperclip==1.9.0 psutil==6.1.1 requests==2.32.3 pylint==3.3.3 +pypresence==4.2.0 diff --git a/src/settings.py b/src/settings.py index 57abd67..5a40622 100644 --- a/src/settings.py +++ b/src/settings.py @@ -11,7 +11,6 @@ def __init__(self, *args, **kw): def initUI(self): panel = wx.Panel(self) - vbox = wx.BoxSizer(wx.VERTICAL) # Theme setting @@ -41,24 +40,30 @@ def initUI(self): self.header_inactive_btn = wx.Button(panel, label='Choose Color', size=(100, -1)) hbox3.Add(self.header_inactive_btn, flag=wx.LEFT, border=5) vbox.Add(hbox3, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10) + + # Discord Presence setting + hbox4 = wx.BoxSizer(wx.HORIZONTAL) + st4 = wx.StaticText(panel, label='Use Discord Presence:') + hbox4.Add(st4, flag=wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, border=8) + self.discord_checkbox = wx.CheckBox(panel) + hbox4.Add(self.discord_checkbox) + vbox.Add(hbox4, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10) # Save button - hbox4 = wx.BoxSizer(wx.HORIZONTAL) + hbox5 = wx.BoxSizer(wx.HORIZONTAL) btn_save = wx.Button(panel, label='Save', size=(70, 30)) - hbox4.Add(btn_save) - vbox.Add(hbox4, flag=wx.ALIGN_RIGHT|wx.RIGHT|wx.BOTTOM, border=10) + hbox5.Add(btn_save) + vbox.Add(hbox5, flag=wx.ALIGN_RIGHT|wx.RIGHT|wx.BOTTOM, border=10) panel.SetSizer(vbox) - # Load initial values self.load_settings() - # Bind the save button to the save function btn_save.Bind(wx.EVT_BUTTON, self.on_save) self.header_active_btn.Bind(wx.EVT_BUTTON, self.on_active_color) self.header_inactive_btn.Bind(wx.EVT_BUTTON, self.on_inactive_color) - self.SetSize((500, 250)) + self.SetSize((500, 280)) self.SetTitle('Settings App') self.Centre() @@ -82,6 +87,13 @@ def load_settings(self): except FileNotFoundError: self.header_active_text.SetValue('') self.header_inactive_text.SetValue('') + + try: + with open('discord.xcfg', 'r') as file: + use_discord = file.read().strip() + self.discord_checkbox.SetValue(use_discord.lower() == 'true') + except FileNotFoundError: + self.discord_checkbox.SetValue(True) def on_active_color(self, event): color_dialog = wx.ColourDialog(self) @@ -110,6 +122,9 @@ def on_save(self, event): with open('xedix.xcfg', 'w') as file: file.write(f'headerActive:{header_active_color};\n') file.write(f'headerInactive:{header_inactive_color};\n') + + with open('discord.xcfg', 'w') as file: + file.write(str(self.discord_checkbox.GetValue())) wx.MessageBox('Settings saved successfully', 'Info', wx.OK | wx.ICON_INFORMATION) @@ -117,4 +132,4 @@ def main(): app = wx.App() frame = SettingsApp(None) frame.Show() - app.MainLoop() + app.MainLoop() \ No newline at end of file