From 306e992789217127f033bf317491623171256a8e Mon Sep 17 00:00:00 2001 From: sunxyw Date: Mon, 4 Apr 2022 00:11:58 +0800 Subject: [PATCH] add all bots proxy for ZMRobot --- src/ZM/API/Proxies/Bot/AbstractBotProxy.php | 74 +++++++++++++++++++++ src/ZM/API/Proxies/Bot/AllBotsProxy.php | 43 ++++++++++++ src/ZM/API/ZMRobot.php | 24 +++++++ 3 files changed, 141 insertions(+) create mode 100644 src/ZM/API/Proxies/Bot/AbstractBotProxy.php create mode 100644 src/ZM/API/Proxies/Bot/AllBotsProxy.php diff --git a/src/ZM/API/Proxies/Bot/AbstractBotProxy.php b/src/ZM/API/Proxies/Bot/AbstractBotProxy.php new file mode 100644 index 00000000..1581aa07 --- /dev/null +++ b/src/ZM/API/Proxies/Bot/AbstractBotProxy.php @@ -0,0 +1,74 @@ +bot = $bot; + } + + /** + * 在传入的机器人实例上调用方法 + * + * @param string $name 方法名 + * @param array $arguments 参数 + * @return mixed + */ + public function __call(string $name, array $arguments) + { + return $this->bot->{$name}(...$arguments); + } + + /** + * 获取传入的机器人实例的属性 + * + * @param string $name 属性名 + * @return mixed + */ + public function __get(string $name) + { + return $this->bot->{$name}; + } + + /** + * 设置传入的机器人实例的属性 + * + * @param string $name 属性名 + * @param mixed $value 属性值 + */ + public function __set(string $name, $value) + { + $this->bot->{$name} = $value; + } + + /** + * 判断传入的机器人实例的属性是否存在 + * + * @param string $name 属性名 + */ + public function __isset(string $name): bool + { + return isset($this->bot->{$name}); + } +} diff --git a/src/ZM/API/Proxies/Bot/AllBotsProxy.php b/src/ZM/API/Proxies/Bot/AllBotsProxy.php new file mode 100644 index 00000000..c9f4ddec --- /dev/null +++ b/src/ZM/API/Proxies/Bot/AllBotsProxy.php @@ -0,0 +1,43 @@ + 返回一个包含所有执行结果的数组,键名为机器人ID + */ + public function __call(string $name, array $arguments) + { + // 如果调用的方法为代理方法,则传入当前代理作为参数 + // 一般此情况代表用户进行嵌套代理,即 `->all()->allGroups()` 等情况 + $reflection = new ReflectionMethod(ZMRobot::class, $name); + if (($return = $reflection->getReturnType()) && $return instanceof ReflectionNamedType && str_contains($return->getName(), 'Proxy')) { + Console::debug("Trying to construct proxy {$name} inside proxy, returning nested proxy."); + // 插入当前代理作为第一个参数 + array_unshift($arguments, $this); + return $this->bot->{$name}(...$arguments); + } + + $result = []; + // 遍历所有机器人实例 + foreach ($this->bot::getAllRobot() as $bot) { + Console::debug("Calling {$name} on bot {$bot->getSelfId()}."); + $result[$bot->getSelfId()] = $bot->{$name}(...$arguments); + } + return $result; + } +} diff --git a/src/ZM/API/ZMRobot.php b/src/ZM/API/ZMRobot.php index 23dd8c6e..2c16dd96 100644 --- a/src/ZM/API/ZMRobot.php +++ b/src/ZM/API/ZMRobot.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace ZM\API; +use ZM\API\Proxies\Bot as Proxies; +use ZM\Console\Console; + /** * Class ZMRobot * @since 1.2 @@ -11,4 +14,25 @@ namespace ZM\API; */ class ZMRobot extends OneBotV11 { + /** + * 获取一个会在所有机器人实例上执行的代理 + */ + public function all(Proxies\AbstractBotProxy $proxy = null): Proxies\AllBotsProxy + { + $bot = $proxy ?: $this; + $bot_id = implode_when_necessary($bot->getSelfId()); + Console::debug("Constructing AllBotsProxy for ZMRobot({$bot_id})"); + return new Proxies\AllBotsProxy($bot); + } + + /** + * 获取一个会在所有群上执行的代理 + */ + public function allGroups(Proxies\AbstractBotProxy $proxy = null): Proxies\AllGroupsProxy + { + $bot = $proxy ?: $this; + $bot_id = implode_when_necessary($bot->getSelfId()); + Console::debug("Constructing AllGroupsProxy for ZMRobot({$bot_id})"); + return new Proxies\AllGroupsProxy($bot); + } }