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

Added status and ping for the last time score was posted (Sourcery refactored) #18

Closed
wants to merge 2 commits into from
Closed
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
31 changes: 27 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
from json import loads
from os import environ
from typing import Optional
Expand Down Expand Up @@ -41,6 +41,8 @@ class Client(Bot):
CHANNEL_ID: int
ROLE_ID: int
session: ClientSession
last_score_time: datetime = None
score_status_message = None

def __init__(self):
channel_id = environ.get("CHANNEL_ID")
Expand Down Expand Up @@ -69,6 +71,27 @@ async def ping_beatleader(self):
self.SERVER_OK = False
await self.send_ping_alert(1, resp)

@tasks.loop(seconds=10)
async def check_score_delay(self):
channel = await self.fetch_channel(self.CHANNEL_ID)
if not channel:
return

if self.last_score_time:
time_diff = datetime.utcnow() - self.last_score_time
status_text = f"Last score was posted {time_diff.total_seconds() // 60} minutes and {time_diff.total_seconds() % 60} seconds ago."
else:
status_text = "Waiting for the first score to be posted."

if self.score_status_message:
await self.score_status_message.edit(content=status_text)
else:
self.score_status_message = await channel.send(status_text)

if time_diff and time_diff > timedelta(minutes=1):
await channel.send(f"<@&{self.ROLE_ID}> More than a minute has passed since the last score was posted.")
self.score_status_message = None

async def send_ping_alert(self, type: int, resp: Optional[ClientResponse] = None):
channel = await self.fetch_channel(self.CHANNEL_ID)
if channel is not None and isinstance(channel, TextChannel):
Expand Down Expand Up @@ -106,6 +129,7 @@ async def connect_to_beatleader(self):
score_add()
user_add(score["player"]["id"])
map_add(score["leaderboard"]["song"]["id"])
self.last_score_time = datetime.utcnow()

except ConnectionClosedOK as e:
self.WS_CONNECTED = False
Expand All @@ -116,11 +140,9 @@ async def connect_to_beatleader(self):
self.WS_CONNECTED = False
if "cloudflare" in str(e):
await self.send_websocket_alert(3, e)
continue
else:
await self.send_websocket_alert(2, e)
continue

continue
except Exception as e:
self.WS_CONNECTED = False
await self.send_websocket_alert(2, e)
Expand All @@ -130,6 +152,7 @@ async def on_ready(self):
self.session = ClientSession()
self.ping_beatleader.start()
self.update_status.start()
self.check_score_delay.start()

await self.connect_to_beatleader()

Expand Down
Loading