Skip to content

Commit

Permalink
fix(bot): 🐛 fix fastapi lib loading issue (fix #161)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jan 15, 2024
1 parent eb2d2c6 commit 27738a6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bot/bot/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
18 changes: 6 additions & 12 deletions bot/bot/fastapi_manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions bot/bot/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
Expand Down

0 comments on commit 27738a6

Please sign in to comment.