From 27738a68833051de821928febac110252b532e63 Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Mon, 15 Jan 2024 10:42:10 +0000 Subject: [PATCH] =?UTF-8?q?fix(bot):=20=F0=9F=90=9B=20fix=20fastapi=20lib?= =?UTF-8?q?=20loading=20issue=20(fix=20#161)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/bot/event_handler.py | 2 +- bot/bot/fastapi_manager.py | 18 ++++++------------ bot/bot/plugin.py | 22 ++++++++++++++++++++-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/bot/bot/event_handler.py b/bot/bot/event_handler.py index 9f45105..3a88b0e 100644 --- a/bot/bot/event_handler.py +++ b/bot/bot/event_handler.py @@ -71,4 +71,4 @@ def on_player_left(server: PluginServerInterface, player: str): @staticmethod @event_listener(MCDRPluginEvents.PLUGIN_UNLOADED) def on_unload(server: PluginServerInterface): - plugin.fastapi_manager.unload() + plugin.unload_fastapi_manager() diff --git a/bot/bot/fastapi_manager.py b/bot/bot/fastapi_manager.py index a2dd9ad..f27b7c0 100644 --- a/bot/bot/fastapi_manager.py +++ b/bot/bot/fastapi_manager.py @@ -1,14 +1,9 @@ from typing import TYPE_CHECKING, List, Dict +from fastapi import HTTPException from pydantic import BaseModel, Field, conlist from mcdreforged.api.types import PluginServerInterface -# fast api -try: - from fastapi import HTTPException -except ImportError: - HTTPException = None - from bot.bot import Bot from bot.exceptions import * from bot.location import Location @@ -60,12 +55,11 @@ class FastAPIManager: def __init__(self, plugin: 'Plugin'): self.__plugin: 'Plugin' = plugin - # check fast api loaded - if self.__plugin.fastapi_mcdr is None: - self.__logger.debug( - "FastAPI MCDR is not loaded, will not register APIs." - ) - elif self.__plugin.fastapi_mcdr.is_ready(): + # check fast api ready + if ( + self.__plugin.fastapi_mcdr is not None and + self.__plugin.fastapi_mcdr.is_ready() + ): self.__register_apis(self.__plugin.server) # register event listener diff --git a/bot/bot/plugin.py b/bot/bot/plugin.py index 2bad637..a9fb2b1 100644 --- a/bot/bot/plugin.py +++ b/bot/bot/plugin.py @@ -3,11 +3,15 @@ from bot.constants import CONFIG_FILE_NAME from bot.config import Config from bot.bot_manager import BotManager -from bot.fastapi_manager import FastAPIManager from bot.command_handler import CommandHandler from bot.event_handler import EventHandler from bot.location import Location +try: + from bot.fastapi_manager import FastAPIManager +except ImportError: + FastAPIManager = None + class Plugin: def __init__(self, server: PluginServerInterface, prev_module): @@ -22,10 +26,24 @@ def __init__(self, server: PluginServerInterface, prev_module): self.__check_config() self.__bot_manager = BotManager(self, prev_module) - self.__fastapi_manager = FastAPIManager(self) + self.__fastapi_manager = None + self.load_fastapi_manager() self.__command_handler = CommandHandler(self) self.__event_handler = EventHandler(self) + def load_fastapi_manager(self): + if FastAPIManager is not None: + self.__fastapi_manager = FastAPIManager(self) + else: + self.server.logger.debug( + "FastAPI library is not installed, " + "will not register APIs with FastAPI MCDR." + ) + + def unload_fastapi_manager(self): + if self.__fastapi_manager is not None: + self.__fastapi_manager.unload() + @property def fastapi_mcdr(self): return self.__server.get_plugin_instance('fastapi_mcdr')