This commit is contained in:
@@ -15,7 +15,8 @@ class MatrixBot:
|
||||
__AUTHOR__ = 'anima'
|
||||
__VERSION__ = '0.6.3'
|
||||
def __init__(self, config_file: str = 'bot.conf', session_file: str = 'session.tmp') -> None:
|
||||
"""
|
||||
"""Initialize the MatrixBot instance.
|
||||
|
||||
Args:
|
||||
config_file (str, optional): path to config file
|
||||
session_file (str, optional): path to session file
|
||||
@@ -36,7 +37,11 @@ class MatrixBot:
|
||||
exit(1)
|
||||
|
||||
def _load_config(self) -> bool:
|
||||
"""load given config file"""
|
||||
"""Load given config file.
|
||||
|
||||
Returns:
|
||||
bool: True if config file was loaded successfully
|
||||
"""
|
||||
if not os.path.exists(self.config_file):
|
||||
return False
|
||||
logging.info(f'load config from file {self.config_file}')
|
||||
@@ -52,7 +57,7 @@ class MatrixBot:
|
||||
return True
|
||||
|
||||
def _save_session(self) -> bool:
|
||||
"""save session data as file
|
||||
"""Save session data as file.
|
||||
|
||||
Returns:
|
||||
bool: True if file was saved
|
||||
@@ -68,10 +73,10 @@ class MatrixBot:
|
||||
return True
|
||||
|
||||
def _load_session(self) -> bool:
|
||||
"""load session date from file
|
||||
"""Load session date from file.
|
||||
|
||||
Returns:
|
||||
bool: True if file was found and load
|
||||
bool: True if file was found and loaded
|
||||
"""
|
||||
if not os.path.exists(self.session_file):
|
||||
return False
|
||||
@@ -84,7 +89,7 @@ class MatrixBot:
|
||||
return True
|
||||
|
||||
async def _login(self) -> bool:
|
||||
"""restore login with session file or run full login process
|
||||
"""Restore login with session file or run full login process.
|
||||
|
||||
Returns:
|
||||
bool: True when bot has a valid login
|
||||
@@ -106,20 +111,20 @@ class MatrixBot:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _find_skills(self):
|
||||
"""search all modules in skill folder and init these"""
|
||||
def _find_skills(self) -> None:
|
||||
"""Search all modules in skill folder and init these."""
|
||||
skills_path = pathlib.Path(__file__).parent / "skills"
|
||||
for module in skills_path.iterdir():
|
||||
if module.is_dir() and (module / "__init__.py").exists():
|
||||
importlib.import_module(f"skills.{module.name}")
|
||||
|
||||
async def _activate_skills(self):
|
||||
"""activate all skills which defined in config file"""
|
||||
async def _activate_skills(self) -> None:
|
||||
"""Activate all skills which defined in config file."""
|
||||
for skill in self._config['skills'].keys():
|
||||
await self.load_skill(skill_name=skill, config=self._config['skills'][skill])
|
||||
|
||||
async def load_skill(self, skill_name: str, config: dict = None) -> bool:
|
||||
"""load skill from skill registry
|
||||
"""Load skill from skill registry.
|
||||
|
||||
Args:
|
||||
skill_name (str): name of skill (same as classname)
|
||||
@@ -136,24 +141,26 @@ class MatrixBot:
|
||||
self.skills.append(skill)
|
||||
return True
|
||||
|
||||
def unload_skill(self):
|
||||
def unload_skill(self) -> None:
|
||||
"""Unload a skill."""
|
||||
pass
|
||||
|
||||
async def send_status(self, message: str) -> bool:
|
||||
"""send a message to a predefined room
|
||||
"""Send a message to a predefined room.
|
||||
|
||||
Args:
|
||||
message (str): message content
|
||||
|
||||
Returns:
|
||||
bool: True if message send
|
||||
bool: True if message sent
|
||||
"""
|
||||
if self.silence:
|
||||
return False
|
||||
await self.helper.send_message(f"{self._config['general']['status_room']}:{self.domain}", message)
|
||||
return True
|
||||
|
||||
async def run(self):
|
||||
async def run(self) -> None:
|
||||
"""Run the bot."""
|
||||
if await self._login():
|
||||
del self.__password
|
||||
await self.send_status(f"{self.botname} Online!")
|
||||
|
||||
Reference in New Issue
Block a user