minya_hotomoe/minya.py

104 lines
3 KiB
Python
Raw Normal View History

2023-09-08 02:49:37 +02:00
from pyfiglet import Figlet
2023-10-03 04:01:27 +02:00
f = Figlet(font="small")
2023-09-08 02:49:37 +02:00
import os
import importlib.util
import re
import inspect
from dotenv import load_dotenv
from module_interface import ModuleInterface, ModuleManager
import asyncio
from aiohttp import ClientWebSocketResponse
from mipac.models.notification import NotificationNote
from mipa.ext.commands.bot import Bot
2023-10-03 04:01:27 +02:00
2023-09-08 02:49:37 +02:00
def load_modules():
module_dir = "modules"
modules = []
for filename in os.listdir(module_dir):
if filename.endswith(".py"):
module_name = os.path.splitext(filename)[0]
module_path = os.path.join(module_dir, filename)
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for attribute_name in dir(module):
attribute = getattr(module, attribute_name)
2023-10-03 04:01:27 +02:00
if (
inspect.isclass(attribute)
and issubclass(attribute, ModuleInterface)
and attribute != ModuleInterface
):
2023-09-08 02:49:37 +02:00
modules.append(attribute())
return modules
2023-10-03 04:01:27 +02:00
2023-09-08 02:49:37 +02:00
class MiNya(Bot):
def __init__(self):
super().__init__()
self.__modules = []
self.__manager = ModuleManager(self)
async def _connect_channel(self):
2023-10-03 04:01:27 +02:00
await self.router.connect_channel(["main"])
2023-09-08 02:49:37 +02:00
async def on_reconnect(self, ws: ClientWebSocketResponse):
await self._connect_channel()
async def on_ready(self, ws: ClientWebSocketResponse):
await self._connect_channel()
print("[Main] Bot System Ready.")
print("")
print("[Main] Loading Modules...")
print("")
self.__modules = load_modules()
print("")
print(f"[Main] Loaded Modules : {len(self.__modules)}")
print("")
print("[Main] Registering Modules...")
2023-10-03 04:01:27 +02:00
2023-09-08 02:49:37 +02:00
for module in self.__modules:
self.__manager.register_module(module)
2023-10-02 07:36:36 +02:00
for module in self.__modules:
module.module_ready()
2023-09-08 02:49:37 +02:00
print("")
2023-10-03 04:01:27 +02:00
2023-09-08 02:49:37 +02:00
async def on_mention(self, notice: NotificationNote):
2023-10-03 04:01:27 +02:00
print("[Main] Got Mentioned!")
2023-09-08 02:49:37 +02:00
for _, module in self.__manager.modules.items():
2023-10-03 04:01:27 +02:00
if module.regex_pattern and re.match(
module.regex_pattern, notice.note.content
):
2023-09-08 02:49:37 +02:00
print("[Main] Regex Matching Module found : ", module.name)
if await module.execute_module(notice.note):
break
def main():
2023-10-03 04:01:27 +02:00
print(f.renderText("MiNya V1"))
2023-09-08 02:49:37 +02:00
print('"MIsskey Networked Yet Ambitious"')
2023-10-03 04:01:27 +02:00
print("")
2023-09-08 02:49:37 +02:00
print("[Main] Minya V1 Bootstap...")
print("[Main] Loading settings...")
load_dotenv()
2023-10-03 04:01:27 +02:00
instance = os.environ.get("BOT_INSTANCE")
key = os.environ.get("BOT_KEY")
2023-09-08 02:49:37 +02:00
if not (instance and key):
print("[MAIN] ERR! please check .env or os environment!")
return
bot = MiNya()
2023-10-03 04:01:27 +02:00
asyncio.run(bot.start(f"wss://{instance}/streaming", key))
2023-09-08 02:49:37 +02:00
if __name__ == "__main__":
main()