update version info

This commit is contained in:
WhatDidYouExpect 2025-03-16 16:57:47 +01:00
parent ef93452d1c
commit 679b1822cd
5 changed files with 78 additions and 1 deletions

4
.gitignore vendored
View file

@ -1,2 +1,4 @@
.env .env
__pycache__ __pycache__
current_version.txt
MEMORY_LOADED

3
bot.py
View file

@ -203,6 +203,9 @@ def check_for_update():
return None, None return None, None
local_version = get_local_version() local_version = get_local_version()
if local_version == "0.0.0":
with open(LOCAL_VERSION_FILE, "w") as f:
f.write(latest_version)
generate_sha256_of_current_file() generate_sha256_of_current_file()
gooberhash = latest_version_info.get("hash") gooberhash = latest_version_info.get("hash")
if gooberhash == currenthash: if gooberhash == currenthash:

72
update.py Normal file
View file

@ -0,0 +1,72 @@
import requests
import os
import argparse
# config
DEFAULT_VERSION_URL = "https://goober.whatdidyouexpect.eu/latest_version.json"
LOCAL_VERSION_FILE = "current_version.txt"
SCRIPT_FILE = "bot.py"
CONFIG_FILE = "config.py"
def get_latest_version_info(version_url):
"""Fetch the latest version information from the server."""
try:
response = requests.get(version_url, timeout=5)
response.raise_for_status() # Will raise HTTPError for bad responses
return response.json()
except requests.RequestException as e:
print(f"Error: Unable to connect to the update server. {e}")
return None
def get_local_version():
"""Read the current version from the local file."""
if os.path.exists(LOCAL_VERSION_FILE):
with open(LOCAL_VERSION_FILE, "r") as f:
return f.read().strip()
return "0.0.0" # Default version if file doesn't exist
def save_local_version(version):
"""Save the current version to the local file."""
with open(LOCAL_VERSION_FILE, "w") as f:
f.write(version)
def download_file(url, destination):
"""Download a file from the given URL and save it to the destination."""
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # Will raise HTTPError for bad responses
with open(destination, "wb") as f:
f.write(response.content)
print(f"Successfully downloaded the file to {destination}.")
except requests.RequestException as e:
print(f"Error: Failed to download the file from {url}. {e}")
def check_for_update(version_url):
"""Check if a new version is available and update the script if needed."""
latest_version_info = get_latest_version_info(version_url)
if not latest_version_info:
print("Could not fetch update information.")
return
latest_version = latest_version_info.get("version")
download_url = latest_version_info.get("download_url")
config_url = latest_version_info.get("config_url")
if not latest_version or not download_url:
print("Error: Invalid version information received from server.")
return
local_version = get_local_version()
if local_version != latest_version:
print(f"New version available: {latest_version} (Current: {local_version})")
print("Downloading the new version...")
download_file(download_url, SCRIPT_FILE)
download_file(config_url, CONFIG_FILE)
save_local_version(latest_version)
print("Update complete! Restart the bot to use the new version.")
else:
print(f"You're using the latest version: {local_version}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Check for updates and download the latest version of the bot.")
parser.add_argument("--host", type=str, help="Custom version URL", default=DEFAULT_VERSION_URL)
args = parser.parse_args()
check_for_update(args.host)