2025-06-20 23:48:10 +02:00
|
|
|
import requests
|
|
|
|
import os
|
|
|
|
import modules.globalvars as gv
|
|
|
|
from modules.translations import *
|
|
|
|
from modules.markovmemory import get_file_info
|
|
|
|
|
2025-06-22 19:07:41 +02:00
|
|
|
# Ping the server to check if it's alive and send some info
|
2025-06-20 23:48:10 +02:00
|
|
|
def ping_server():
|
|
|
|
if gv.ALIVEPING == "false":
|
2025-06-22 19:07:41 +02:00
|
|
|
# If pinging is disabled, print message and set environment variable
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{gv.YELLOW}{get_translation(gv.LOCALE, 'pinging_disabled')}{RESET}")
|
|
|
|
os.environ['gooberauthenticated'] = 'No'
|
|
|
|
return
|
2025-06-22 19:07:41 +02:00
|
|
|
# Get server alert message
|
2025-06-20 23:48:10 +02:00
|
|
|
goobres = requests.get(f"{gv.VERSION_URL}/alert")
|
|
|
|
print(f"{get_translation(gv.LOCALE, 'goober_server_alert')}{goobres.text}")
|
2025-06-22 19:07:41 +02:00
|
|
|
# Gather file info for payload
|
2025-06-20 23:48:10 +02:00
|
|
|
file_info = get_file_info(gv.MEMORY_FILE)
|
|
|
|
payload = {
|
|
|
|
"name": gv.NAME,
|
|
|
|
"memory_file_info": file_info,
|
|
|
|
"version": gv.local_version,
|
|
|
|
"slash_commands": gv.slash_commands_enabled,
|
|
|
|
"token": gv.gooberTOKEN
|
|
|
|
}
|
|
|
|
try:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Send ping to server
|
2025-06-20 23:48:10 +02:00
|
|
|
response = requests.post(gv.VERSION_URL+"/ping", json=payload)
|
|
|
|
if response.status_code == 200:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Success: print message and set environment variable
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{gv.GREEN}{get_translation(gv.LOCALE, 'goober_ping_success').format(NAME=gv.NAME)}{RESET}")
|
|
|
|
os.environ['gooberauthenticated'] = 'Yes'
|
|
|
|
else:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Failure: print error and set environment variable
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{RED}{get_translation(gv.LOCALE, 'goober_ping_fail')} {response.status_code}{RESET}")
|
|
|
|
os.environ['gooberauthenticated'] = 'No'
|
|
|
|
except Exception as e:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Exception: print error and set environment variable
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{RED}{get_translation(gv.LOCALE, 'goober_ping_fail2')} {str(e)}{RESET}")
|
|
|
|
os.environ['gooberauthenticated'] = 'No'
|
|
|
|
|
2025-06-22 19:07:41 +02:00
|
|
|
# Check if a given name is available for registration
|
2025-06-20 23:48:10 +02:00
|
|
|
def is_name_available(NAME):
|
|
|
|
if os.getenv("gooberTOKEN"):
|
2025-06-22 19:07:41 +02:00
|
|
|
# If token is already set, skip check
|
2025-06-20 23:48:10 +02:00
|
|
|
return
|
|
|
|
try:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Send request to check name availability
|
2025-06-20 23:48:10 +02:00
|
|
|
response = requests.post(f"{gv.VERSION_URL}/check-if-available", json={"name": NAME}, headers={"Content-Type": "application/json"})
|
|
|
|
if response.status_code == 200:
|
|
|
|
data = response.json()
|
|
|
|
return data.get("available", False)
|
|
|
|
else:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Print error if request failed
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{get_translation(gv.LOCALE, 'name_check')}", response.json())
|
|
|
|
return False
|
|
|
|
except Exception as e:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Print exception if request failed
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{get_translation(gv.LOCALE, 'name_check2')}", e)
|
|
|
|
return False
|
|
|
|
|
2025-06-22 19:07:41 +02:00
|
|
|
# Register a new name with the server
|
2025-06-20 23:48:10 +02:00
|
|
|
def register_name(NAME):
|
|
|
|
try:
|
|
|
|
if gv.ALIVEPING == False:
|
2025-06-22 19:07:41 +02:00
|
|
|
# If pinging is disabled, do nothing
|
2025-06-20 23:48:10 +02:00
|
|
|
return
|
2025-06-22 19:07:41 +02:00
|
|
|
# Check if the name is available
|
2025-06-20 23:48:10 +02:00
|
|
|
if not is_name_available(NAME):
|
|
|
|
if os.getenv("gooberTOKEN"):
|
|
|
|
return
|
2025-06-22 19:07:41 +02:00
|
|
|
# Name taken: print error and exit
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{RED}{get_translation(gv.LOCALE, 'name_taken')}{RESET}")
|
|
|
|
quit()
|
2025-06-22 19:07:41 +02:00
|
|
|
# Register the name
|
2025-06-20 23:48:10 +02:00
|
|
|
response = requests.post(f"{gv.VERSION_URL}/register", json={"name": NAME}, headers={"Content-Type": "application/json"})
|
|
|
|
if response.status_code == 200:
|
|
|
|
data = response.json()
|
|
|
|
token = data.get("token")
|
|
|
|
if not os.getenv("gooberTOKEN"):
|
2025-06-22 19:07:41 +02:00
|
|
|
# Print instructions to add token and exit
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{gv.GREEN}{get_translation(gv.LOCALE, 'add_token').format(token=token)} gooberTOKEN=<token>.{gv.RESET}")
|
|
|
|
quit()
|
|
|
|
else:
|
|
|
|
print(f"{gv.GREEN}{gv.RESET}")
|
|
|
|
return token
|
|
|
|
else:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Print error if registration failed
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{gv.RED}{get_translation(gv.LOCALE, 'token_exists').format()}{RESET}", response.json())
|
|
|
|
return None
|
|
|
|
except Exception as e:
|
2025-06-22 19:07:41 +02:00
|
|
|
# Print exception if registration failed
|
2025-06-20 23:48:10 +02:00
|
|
|
print(f"{gv.RED}{get_translation(gv.LOCALE, 'registration_error').format()}{RESET}", e)
|
|
|
|
return None
|
|
|
|
|
2025-06-22 19:07:41 +02:00
|
|
|
# Attempt to register the name at module load
|
2025-06-20 23:48:10 +02:00
|
|
|
register_name(gv.NAME)
|