From 9b59cec22aef09711621ef70c5cf0ac6370967b9 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Mon, 4 Apr 2022 00:12:28 +0800 Subject: [PATCH] add all groups proxy for ZMRobot --- src/ZM/API/Proxies/Bot/AllGroupsProxy.php | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/ZM/API/Proxies/Bot/AllGroupsProxy.php diff --git a/src/ZM/API/Proxies/Bot/AllGroupsProxy.php b/src/ZM/API/Proxies/Bot/AllGroupsProxy.php new file mode 100644 index 00000000..afc1048c --- /dev/null +++ b/src/ZM/API/Proxies/Bot/AllGroupsProxy.php @@ -0,0 +1,43 @@ + 返回一个包含所有执行结果的数组,键名为群号 + */ + public function __call(string $name, array $arguments) + { + // 如果调用的方法并非群组方法,则直接返回输出 + // 因为目前所有群组方法都是以 `group_id` 作为第一个参数,故以此来判断 + $reflection = new \ReflectionMethod(ZMRobot::class, $name); + if (!$reflection->getNumberOfParameters() || $reflection->getParameters()[0]->getName() !== 'group_id') { + Console::warning("Trying to call non-group method {$name} on AllGroupsProxy, skipped."); + return $this->bot->{$name}(...$arguments); + } + + $result = []; + // 获取并遍历所有群组 + $groups = $this->bot->getGroupList()['data']; + foreach ($groups as $group) { + $arguments[0] = $group['group_id']; + $bot_id = implode_when_necessary($this->bot->getSelfId()); + Console::debug("Calling {$name} on group {$group['group_id']} on bot {$bot_id}."); + // 在群组上调用方法 + $result[$group['group_id']] = $this->bot->{$name}(...$arguments); + } + return $result; + } +}