Merge pull request #211 from zhamao-robot/fix-bot-context-not-sync

修复 BotContext 不同步问题
This commit is contained in:
Jerry 2022-12-30 11:22:43 +08:00 committed by GitHub
commit 8473a1152d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -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('', '');
}

View File

@ -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'],
),
]);
}
}
/**