Skip to content

Commit

Permalink
Show time studied in Rich Presence
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed Apr 4, 2024
1 parent 39e17f7 commit 90ca437
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 4 additions & 2 deletions pomodorodiscord/src/pomodoro_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def initialize_state(self, config):
self.start_time_timestamp = None
self.end_time_timestamp = None
self.session_counter = 0
self.seconds_studied = 0

def initialize_rpc(self):
"""Start the rich presence in a separate thread"""
Expand All @@ -79,11 +80,11 @@ def initialize_rpc(self):
def update_rpc(self):
while True:
if self.break_running:
self.rpc.break_state(self.session_counter, self.start_time_timestamp, self.end_time_timestamp)
self.rpc.break_state(self.seconds_studied, self.start_time_timestamp, self.end_time_timestamp)
elif self.running:
self.rpc.running_state(self.session_counter + 1, self.start_time_timestamp, self.end_time_timestamp)
else:
self.rpc.default_state()
self.rpc.default_state(self.end_time_timestamp)

# Discord-imposed rate limit
time.sleep(15)
Expand Down Expand Up @@ -111,6 +112,7 @@ def update_timer(self):
self.remaining_time -= 1
if not self.break_running:
self.track_second()
self.seconds_studied += 1
minutes, seconds = divmod(self.remaining_time, 60)
self.timer_display.configure(text=f"{minutes:02d}:{seconds:02d}")
self.next_timer_update = self.after(1000, self.update_timer)
Expand Down
19 changes: 15 additions & 4 deletions pomodorodiscord/src/richpresence.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import pypresence
from datetime import datetime

CLIENT_ID = '1215345125002059836'


class RichPresence(pypresence.Presence):
def __init__(self):
super().__init__(client_id=CLIENT_ID)
self.launch_time = datetime.now().timestamp()
# Can only update every 15 seconds
try:
self.connect()
self.default_state()
except Exception as e:
print(f"Failed to connect to Discord: {e}")

def default_state(self):
self.update(state="Idling", details=None, large_image="graytomato",
def format_time(self, seconds_studied):
total_seconds = seconds_studied
total_hours = seconds_studied / 3600

if total_hours < 1:
return f"{total_seconds // 60} minute{'s' if total_seconds // 60 != 1 else ''}"
else:
return f"{total_hours:.1f} hours"

def default_state(self, start_time=None):
self.update(state="Idling", details=None, start=start_time or self.launch_time, large_image="graytomato",
large_text="github.com/freeram/pomodoro-discord")

def running_state(self, session, start_time, end_time):
self.update(state=f"Session {session}", details="Studying", start=start_time,
end=end_time, large_image="tomato", large_text="github.com/freeram/pomodoro-discord")

def break_state(self, complete, start_time, end_time):
self.update(state=f"On break. Sessions completed: {complete}", details=None, start=start_time,
def break_state(self, seconds_studied, start_time, end_time):
self.update(state=f"Time studied: {self.format_time(seconds_studied)}", details="On break", start=start_time,
end=end_time, large_image="greentomato", large_text="github.com/freeram/pomodoro-discord")

0 comments on commit 90ca437

Please sign in to comment.