Add updater.py
This commit is contained in:
+162
@@ -0,0 +1,162 @@
|
||||
import requests
|
||||
from datetime import datetime, timedelta
|
||||
import pytz
|
||||
|
||||
# -----------------------------
|
||||
# CONFIGURATION
|
||||
# -----------------------------
|
||||
WEBHOOK_URL = ""
|
||||
MODE = "create" # "create" for first run, then "edit"
|
||||
MESSAGE_ID = ""
|
||||
|
||||
# -----------------------------
|
||||
# DATE CALCULATION
|
||||
# -----------------------------
|
||||
def next_first_thursday():
|
||||
tz = pytz.timezone("Europe/London")
|
||||
|
||||
today = datetime.now(tz)
|
||||
year = today.year
|
||||
month = today.month + 1
|
||||
|
||||
if month == 13:
|
||||
month = 1
|
||||
year += 1
|
||||
|
||||
first_day = tz.localize(datetime(year, month, 1))
|
||||
weekday = first_day.weekday() # Monday=0, Thursday=3
|
||||
|
||||
days_until_thursday = (3 - weekday) % 7
|
||||
first_thursday = first_day + timedelta(days=days_until_thursday)
|
||||
|
||||
# Rust wipe time: ALWAYS 19:00 UK time (GMT/BST auto-adjusts)
|
||||
event_time = first_thursday.replace(hour=19, minute=0, second=0, microsecond=0)
|
||||
return event_time
|
||||
|
||||
# -----------------------------
|
||||
# DYNAMIC COLOUR LOGIC
|
||||
# -----------------------------
|
||||
def get_dynamic_color(event_time):
|
||||
now = datetime.now(event_time.tzinfo)
|
||||
days_left = (event_time - now).days
|
||||
|
||||
if days_left > 7:
|
||||
return 0x2ecc71 # green
|
||||
elif days_left >= 3:
|
||||
return 0xf1c40f # yellow
|
||||
else:
|
||||
return 0xe74c3c # red
|
||||
|
||||
# -----------------------------
|
||||
# PROGRESS BAR (EMOJI)
|
||||
# -----------------------------
|
||||
def build_progress_bar(event_time):
|
||||
now = datetime.now(event_time.tzinfo)
|
||||
|
||||
# Find last wipe (previous first Thursday)
|
||||
month = event_time.month - 1
|
||||
year = event_time.year
|
||||
if month == 0:
|
||||
month = 12
|
||||
year -= 1
|
||||
|
||||
first_day = datetime(year, month, 1, tzinfo=event_time.tzinfo)
|
||||
weekday = first_day.weekday()
|
||||
days_until_thursday = (3 - weekday) % 7
|
||||
last_wipe = first_day + timedelta(days=days_until_thursday)
|
||||
last_wipe = last_wipe.replace(hour=19, minute=0, second=0)
|
||||
|
||||
# Progress calculation
|
||||
total = (event_time - last_wipe).total_seconds()
|
||||
elapsed = (now - last_wipe).total_seconds()
|
||||
pct = max(0, min(1, elapsed / total))
|
||||
|
||||
# Bar settings
|
||||
segments = 10
|
||||
filled = int(pct * segments)
|
||||
empty = segments - filled
|
||||
|
||||
# Colour selection (matches embed)
|
||||
days_left = (event_time - now).days
|
||||
if days_left > 7:
|
||||
emoji = "🟩"
|
||||
elif days_left >= 3:
|
||||
emoji = "🟨"
|
||||
else:
|
||||
emoji = "🟥"
|
||||
|
||||
bar = emoji * filled + "⬜" * empty
|
||||
percent_text = f"{int(pct * 100)}%"
|
||||
|
||||
return f"{bar} {percent_text}"
|
||||
|
||||
# -----------------------------
|
||||
# SEND OR EDIT EMBED
|
||||
# -----------------------------
|
||||
def create_message(unix_timestamp, event_time):
|
||||
color = get_dynamic_color(event_time)
|
||||
progress = build_progress_bar(event_time)
|
||||
|
||||
data = {
|
||||
"embeds": [
|
||||
{
|
||||
"title": "Next server wipe:",
|
||||
"description": (
|
||||
f"<t:{unix_timestamp}:F>\n"
|
||||
f"Relative: <t:{unix_timestamp}:R>\n\n"
|
||||
f"{progress}"
|
||||
),
|
||||
"color": color,
|
||||
"footer": {
|
||||
"text": f"Last updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
r = requests.post(WEBHOOK_URL, json=data)
|
||||
print("Response:", r.text)
|
||||
|
||||
def edit_message(unix_timestamp, event_time):
|
||||
if not MESSAGE_ID:
|
||||
print("ERROR: MESSAGE_ID is empty. Run in create mode first.")
|
||||
return
|
||||
|
||||
color = get_dynamic_color(event_time)
|
||||
progress = build_progress_bar(event_time)
|
||||
|
||||
data = {
|
||||
"embeds": [
|
||||
{
|
||||
"title": "Next server wipe:",
|
||||
"description": (
|
||||
f"<t:{unix_timestamp}:F>\n"
|
||||
f"Relative: <t:{unix_timestamp}:R>\n\n"
|
||||
f"{progress}"
|
||||
),
|
||||
"color": color,
|
||||
"footer": {
|
||||
"text": f"Last updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
edit_url = f"{WEBHOOK_URL}/messages/{MESSAGE_ID}"
|
||||
r = requests.patch(edit_url, json=data)
|
||||
print("Edit status:", r.status_code, r.text)
|
||||
|
||||
# -----------------------------
|
||||
# MAIN
|
||||
# -----------------------------
|
||||
if __name__ == "__main__":
|
||||
event_dt = next_first_thursday()
|
||||
unix_ts = int(event_dt.astimezone(pytz.utc).timestamp())
|
||||
|
||||
if MODE == "create":
|
||||
create_message(unix_ts, event_dt)
|
||||
elif MODE == "edit":
|
||||
edit_message(unix_ts, event_dt)
|
||||
else:
|
||||
print("Invalid MODE. Use 'create' or 'edit'.")
|
||||
Reference in New Issue
Block a user