Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨feat: added discord presence #63

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/discord.xcfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
True
54 changes: 53 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Other
import json
import threading
from pypresence import Presence
import pywinstyles
import webbrowser
# Local imports
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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}")

Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -1257,4 +1309,4 @@ def main():

if __name__ == '__main__':
"""Runs the main process."""
main()
main()
1 change: 1 addition & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pyperclip==1.9.0
psutil==6.1.1
requests==2.32.3
pylint==3.3.3
pypresence==4.2.0
31 changes: 23 additions & 8 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def __init__(self, *args, **kw):

def initUI(self):
panel = wx.Panel(self)

vbox = wx.BoxSizer(wx.VERTICAL)

# Theme setting
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -110,11 +122,14 @@ 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)

def main():
app = wx.App()
frame = SettingsApp(None)
frame.Show()
app.MainLoop()
app.MainLoop()
Loading