mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 00:25:35 +08:00
refactor and add some customizable context functions
This commit is contained in:
@@ -13,7 +13,7 @@ class BotConnectContext
|
|||||||
{
|
{
|
||||||
use BotActionTrait;
|
use BotActionTrait;
|
||||||
|
|
||||||
private ?array $self = null;
|
protected ?array $self = null;
|
||||||
|
|
||||||
public function __construct(private int $flag, private int $fd)
|
public function __construct(private int $flag, private int $fd)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ class BotContext implements ContextInterface
|
|||||||
use BotActionTrait;
|
use BotActionTrait;
|
||||||
|
|
||||||
/** @var array<string, array<string, BotContext>> 记录机器人的上下文列表 */
|
/** @var array<string, array<string, BotContext>> 记录机器人的上下文列表 */
|
||||||
private static array $bots = [];
|
protected static array $bots = [];
|
||||||
|
|
||||||
/** @var null|string[] 记录当前上下文绑定的机器人 */
|
/** @var null|string[] 记录当前上下文绑定的机器人 */
|
||||||
private ?array $self;
|
protected ?array $self;
|
||||||
|
|
||||||
/** @var array 如果是 BotCommand 匹配的上下文,这里会存放匹配到的参数 */
|
/** @var array 如果是 BotCommand 匹配的上下文,这里会存放匹配到的参数 */
|
||||||
private array $params = [];
|
protected array $params = [];
|
||||||
|
|
||||||
public function __construct(string $bot_id, string $platform)
|
public function __construct(string $bot_id, string $platform)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,9 +38,26 @@ class BotMap
|
|||||||
*/
|
*/
|
||||||
private static array $bot_fds = [];
|
private static array $bot_fds = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存机器人自定义的上下文
|
||||||
|
*/
|
||||||
|
private static array $custom_contexts = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存 BotConnectContext 上下文对象的
|
||||||
|
*
|
||||||
|
* @var array<int, array<int, BotConnectContext>>
|
||||||
|
*/
|
||||||
|
private static array $connect_contexts = [];
|
||||||
|
|
||||||
|
public static function setCustomConnectContext(int $flag, int $fd, BotConnectContext $context): void
|
||||||
|
{
|
||||||
|
self::$connect_contexts[$flag][$fd] = $context;
|
||||||
|
}
|
||||||
|
|
||||||
public static function getConnectContext(int $flag, int $fd): BotConnectContext
|
public static function getConnectContext(int $flag, int $fd): BotConnectContext
|
||||||
{
|
{
|
||||||
return new BotConnectContext($flag, $fd);
|
return self::$connect_contexts[$flag][$fd] ?? new BotConnectContext($flag, $fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,6 +100,10 @@ class BotMap
|
|||||||
|
|
||||||
public static function unregisterBotByFd(int $flag, int $fd): void
|
public static function unregisterBotByFd(int $flag, int $fd): void
|
||||||
{
|
{
|
||||||
|
// 注销 connect 上下文
|
||||||
|
unset(self::$connect_contexts[$flag][$fd]);
|
||||||
|
|
||||||
|
// 注销 bot 上下文
|
||||||
$unreg_list = [];
|
$unreg_list = [];
|
||||||
foreach (self::$bot_fds as $platform => $bots) {
|
foreach (self::$bot_fds as $platform => $bots) {
|
||||||
foreach ($bots as $bot_id => $bot_fd) {
|
foreach ($bots as $bot_id => $bot_fd) {
|
||||||
@@ -112,14 +133,14 @@ class BotMap
|
|||||||
}
|
}
|
||||||
// 有,那就通过事件本身的 self 字段来获取一下
|
// 有,那就通过事件本身的 self 字段来获取一下
|
||||||
$self = $event->self;
|
$self = $event->self;
|
||||||
return self::$bot_ctx_cache[$self['platform']][$self['user_id']] = new BotContext($self['user_id'], $self['platform']);
|
return self::$bot_ctx_cache[$self['platform']][$self['user_id']] = new (self::$custom_contexts[$self['platform']][$self['user_id']] ?? BotContext::class)($self['user_id'], $self['platform']);
|
||||||
}
|
}
|
||||||
// 传入的 platform 为空,但 ID 不为空,那么就模糊搜索一个平台的 ID 下的机器人 ID 返回
|
// 传入的 platform 为空,但 ID 不为空,那么就模糊搜索一个平台的 ID 下的机器人 ID 返回
|
||||||
if ($platform === '') {
|
if ($platform === '') {
|
||||||
foreach (self::$bot_fds as $platform => $bot_ids) {
|
foreach (self::$bot_fds as $platform => $bot_ids) {
|
||||||
foreach ($bot_ids as $id => $fd_map) {
|
foreach ($bot_ids as $id => $fd_map) {
|
||||||
if ($id === $bot_id) {
|
if ($id === $bot_id) {
|
||||||
return self::$bot_ctx_cache[$platform][$id] = new BotContext($id, $platform);
|
return self::$bot_ctx_cache[$platform][$id] = new (self::$custom_contexts[$platform][$id] ?? BotContext::class)($id, $platform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,6 +149,11 @@ class BotMap
|
|||||||
if (!isset(self::$bot_fds[$platform][$bot_id])) {
|
if (!isset(self::$bot_fds[$platform][$bot_id])) {
|
||||||
throw new OneBot12Exception('未找到 ' . $platform . ' 平台下 ID 为 ' . $bot_id . ' 的机器人');
|
throw new OneBot12Exception('未找到 ' . $platform . ' 平台下 ID 为 ' . $bot_id . ' 的机器人');
|
||||||
}
|
}
|
||||||
return self::$bot_ctx_cache[$platform][$bot_id] = new BotContext($bot_id, $platform);
|
return self::$bot_ctx_cache[$platform][$bot_id] = new (self::$custom_contexts[$platform][$bot_id] ?? BotContext::class)($bot_id, $platform);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setCustomContext(string|int $bot_id, string $platform, string $context_class = BotContext::class): void
|
||||||
|
{
|
||||||
|
self::$custom_contexts[$platform][$bot_id] = $context_class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user