added sync hub
This commit is contained in:
parent
991264620a
commit
a20e9eb9f0
8 changed files with 173 additions and 19 deletions
|
@ -12,6 +12,7 @@ import importlib.metadata
|
|||
import logging
|
||||
import modules.keys as k
|
||||
from modules.settings import instance as settings_manager
|
||||
from modules.sync_conenctor import instance as sync_hub
|
||||
|
||||
settings = settings_manager.settings
|
||||
|
||||
|
@ -68,6 +69,7 @@ def check_requirements():
|
|||
"better_profanity": "better-profanity",
|
||||
"dotenv": "python-dotenv",
|
||||
"pil": "pillow",
|
||||
"websocket": "websocket-client"
|
||||
}
|
||||
|
||||
parent_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
@ -260,6 +262,11 @@ def presskey2skip(timeout):
|
|||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
||||
|
||||
def check_synchub():
|
||||
if not sync_hub.connected:
|
||||
logger.warning("Sync hub not connected properly! The bot will not be able to react to messages, or create breaking news unless you disable synchub in settings")
|
||||
else:
|
||||
logger.info("Sync hub is conencted")
|
||||
|
||||
beta = beta
|
||||
|
||||
|
@ -277,6 +284,7 @@ def start_checks():
|
|||
check_memory()
|
||||
check_memoryjson()
|
||||
check_cpu()
|
||||
check_synchub()
|
||||
if os.path.exists(".env"):
|
||||
pass
|
||||
else:
|
||||
|
|
|
@ -9,6 +9,9 @@ logger = logging.getLogger("goober")
|
|||
|
||||
ActivityType = Literal["listening", "playing", "streaming", "competing", "watching"]
|
||||
|
||||
class SyncHub(TypedDict):
|
||||
url: str
|
||||
enabled: bool
|
||||
|
||||
class Activity(TypedDict):
|
||||
content: str
|
||||
|
@ -32,6 +35,7 @@ class BotSettings(TypedDict):
|
|||
misc: MiscBotOptions
|
||||
enabled_cogs: List[str]
|
||||
active_memory: str
|
||||
sync_hub: SyncHub
|
||||
|
||||
|
||||
class SettingsType(TypedDict):
|
||||
|
@ -93,6 +97,15 @@ class Settings:
|
|||
|
||||
del self.settings["bot"]["misc"]["active_song"] # type: ignore
|
||||
|
||||
sync_hub: SyncHub | None = self.settings.get("bot", {}).get("sync_hub")
|
||||
|
||||
if not sync_hub:
|
||||
logger.warning("Adding sync hub settings")
|
||||
self.settings["bot"]["sync_hub"] = {
|
||||
"enabled": True,
|
||||
"url": "ws://goober.frii.site"
|
||||
}
|
||||
|
||||
self.commit()
|
||||
|
||||
def reload_settings(self) -> None:
|
||||
|
|
93
modules/sync_conenctor.py
Normal file
93
modules/sync_conenctor.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
import websocket
|
||||
from modules.settings import instance as settings_manager
|
||||
import logging
|
||||
|
||||
|
||||
logger = logging.getLogger("goober")
|
||||
settings = settings_manager.settings
|
||||
|
||||
class SyncConnector:
|
||||
def __init__(self, url: str):
|
||||
self.connected: bool = True
|
||||
self.url = url
|
||||
self.client: websocket.WebSocket | None = None
|
||||
|
||||
self.try_to_connect()
|
||||
|
||||
def __connect(self) -> bool:
|
||||
try:
|
||||
self.client = websocket.create_connection(self.url)
|
||||
except OSError as e:
|
||||
logger.debug(e)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def try_to_connect(self) -> bool:
|
||||
if self.__connect():
|
||||
logger.info("Connected to sync hub!")
|
||||
self.connected = True
|
||||
else:
|
||||
logger.error("Failed to connect to sync hub.. Disabling for the time being")
|
||||
self.connected = False
|
||||
|
||||
return self.connected
|
||||
|
||||
|
||||
def can_react(self, message_id: int) -> bool:
|
||||
"""
|
||||
Checks if goober can react to a messsage
|
||||
"""
|
||||
|
||||
return self.can_event(message_id, "react")
|
||||
|
||||
|
||||
def can_breaking_news(self, message_id: int) -> bool:
|
||||
"""
|
||||
Checks if goober can send a breaking news alert
|
||||
"""
|
||||
|
||||
return self.can_event(message_id, "breaking_news")
|
||||
|
||||
|
||||
def can_event(self, message_id: int, event: str, retry_depth: int = 0) -> bool:
|
||||
"""
|
||||
Checks if goober can send a breaking news alert
|
||||
"""
|
||||
|
||||
logger.debug(f"Checking {event} for message {message_id}")
|
||||
|
||||
if not settings["bot"]["sync_hub"]["enabled"]:
|
||||
logger.info("Skipping sync hub check")
|
||||
return True
|
||||
|
||||
if retry_depth > 2:
|
||||
logger.error("Too many retries. Returning false")
|
||||
return False
|
||||
|
||||
if not self.client:
|
||||
logger.error("Client no connected")
|
||||
return False
|
||||
|
||||
if not self.connected:
|
||||
logger.warning("Not connected to sync hub.. Returning False to avoid conflicts")
|
||||
return False
|
||||
|
||||
try:
|
||||
self.client.send(f"event={event};ref={message_id}")
|
||||
return self.client.recv() == "unhandled"
|
||||
except ConnectionResetError:
|
||||
logger.error("Connection to sync hub reset! Retrying...")
|
||||
|
||||
if not self.__connect():
|
||||
logger.error("Failed to reconnect to sync hub... Disabling")
|
||||
self.connected = False
|
||||
return False
|
||||
|
||||
logger.info("Managed to reconnect to sync hub! Retrying requests")
|
||||
self.connected = True
|
||||
return self.can_event(message_id, event, retry_depth+1)
|
||||
|
||||
|
||||
|
||||
instance = SyncConnector("ws://localhost:80")
|
Loading…
Add table
Add a link
Reference in a new issue