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