diff --git a/src/Globals/global_functions.php b/src/Globals/global_functions.php index b3c0385f..55b1747f 100644 --- a/src/Globals/global_functions.php +++ b/src/Globals/global_functions.php @@ -6,7 +6,6 @@ use OneBot\Driver\Coroutine\Adaptive; use OneBot\Driver\Coroutine\CoroutineInterface; use OneBot\Driver\Process\ExecutionResult; use OneBot\V12\Object\MessageSegment; -use OneBot\V12\Object\OneBotEvent; use Psr\Log\LoggerInterface; use ZM\Config\ZMConfig; use ZM\Container\ContainerHolder; @@ -241,10 +240,8 @@ function config(array|string $key = null, mixed $default = null) function bot(): ZM\Context\BotContext { - if (\container()->has('bot.event')) { - /** @var OneBotEvent $bot_event */ - $bot_event = \container()->get('bot.event'); - return new \ZM\Context\BotContext($bot_event->self['user_id'] ?? '', $bot_event->self['platform']); + if (container()->has(ZM\Context\BotContext::class)) { + return container()->get(ZM\Context\BotContext::class); } return new \ZM\Context\BotContext('', ''); } diff --git a/src/ZM/Container/ContainerRegistrant.php b/src/ZM/Container/ContainerRegistrant.php index d3a2cd80..ebc770b5 100644 --- a/src/ZM/Container/ContainerRegistrant.php +++ b/src/ZM/Container/ContainerRegistrant.php @@ -26,8 +26,16 @@ class ContainerRegistrant self::addServices([ OneBotEvent::class => $event, 'bot.event' => DI\get(OneBotEvent::class), - BotContext::class => fn () => bot(), ]); + + if (isset($event->self['platform'])) { + self::addServices([ + BotContext::class => DI\autowire(BotContext::class)->constructor( + $event->self['user_id'] ?? '', + $event->self['platform'], + ), + ]); + } } /**