Commit d4a8ea8b by HanSon

增加 onceHandler

1 parent 1bf3ea16
...@@ -44,6 +44,19 @@ function reply($str) ...@@ -44,6 +44,19 @@ function reply($str)
} }
$groupMap = [
'vbot 测试群' => 1,
];
$robot->server->setOnceHandler(function () use ($groupMap) {
group()->reset(group()->map(function ($group, $key) use ($groupMap) {
if (isset($groupMap[$group['NickName']])){
$group['id'] = $groupMap[$group['NickName']];
}
return $group;
})->toArray());
});
$robot->server->setMessageHandler(function ($message) use ($path) { $robot->server->setMessageHandler(function ($message) use ($path) {
/** @var $message Message */ /** @var $message Message */
...@@ -63,8 +76,10 @@ $robot->server->setMessageHandler(function ($message) use ($path) { ...@@ -63,8 +76,10 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// 群组@我回复 // 群组@我回复
} elseif ($message->fromType === 'Group') { } elseif ($message->fromType === 'Group') {
if (str_contains($message->content, '设置群名称') && $message->from['Alias'] === 'hanson1994') { if (str_contains($message->content, '设置群名称')) {
group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content)); if (isset($message->sender['Alias']) && $message->sender['Alias'] === 'hanson1994') {
group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content));
}
} }
if ($message->isAt) { if ($message->isAt) {
...@@ -197,7 +212,7 @@ $robot->server->setMessageHandler(function ($message) use ($path) { ...@@ -197,7 +212,7 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
return $message->content; return $message->content;
} elseif ($message->action === 'RENAME') { } elseif ($message->action === 'RENAME') {
// \Hanson\Vbot\Support\Console::log($message->from['NickName'] . ' 改名为 ' . $message->rename); // \Hanson\Vbot\Support\Console::log($message->from['NickName'] . ' 改名为 ' . $message->rename);
if ($message->rename !== 'vbot 测试群'){ if (isset($message->from['id']) && $message->from['id'] == 1 && $message->rename !== 'vbot 测试群') {
group()->setGroupName($message->from['UserName'], 'vbot 测试群'); group()->setGroupName($message->from['UserName'], 'vbot 测试群');
return '行不改名,坐不改姓!'; return '行不改名,坐不改姓!';
} }
......
...@@ -32,6 +32,16 @@ class Group extends Collection ...@@ -32,6 +32,16 @@ class Group extends Collection
} }
/** /**
* 重置数据
*
* @param $array
*/
public function reset($array)
{
static::$instance = new Group($array);
}
/**
* 判断是否群组 * 判断是否群组
* *
* @param $userName * @param $userName
...@@ -62,6 +72,21 @@ class Group extends Collection ...@@ -62,6 +72,21 @@ class Group extends Collection
} }
/** /**
* 根据通讯录中的昵称获取通讯对象
*
* @param $nickname
* @return mixed
*/
public function getUsernameByNickname($nickname)
{
return $this->search(function($item, $key) use ($nickname){
if($item['NickName'] === $nickname){
return true;
}
});
}
/**
* 创建群聊天 * 创建群聊天
* *
* @param array $contacts * @param array $contacts
......
...@@ -32,6 +32,8 @@ class MessageHandler ...@@ -32,6 +32,8 @@ class MessageHandler
private $exceptionHandler; private $exceptionHandler;
private $onceHandler;
private $sync; private $sync;
private $messageFactory; private $messageFactory;
...@@ -117,10 +119,29 @@ class MessageHandler ...@@ -117,10 +119,29 @@ class MessageHandler
} }
/** /**
* 执行一次的处理器
*
* @param Closure $closure
* @throws \Exception
*/
public function setOnceHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('exit handler must be a closure!');
}
$this->onceHandler = $closure;
}
/**
* 轮询消息API接口 * 轮询消息API接口
*/ */
public function listen() public function listen()
{ {
if($this->onceHandler instanceof Closure){
call_user_func_array($this->onceHandler, []);
}
while (true){ while (true){
if($this->customHandler instanceof Closure){ if($this->customHandler instanceof Closure){
call_user_func_array($this->customHandler, []); call_user_func_array($this->customHandler, []);
......
...@@ -339,4 +339,9 @@ class Server ...@@ -339,4 +339,9 @@ class Server
{ {
MessageHandler::getInstance()->setExceptionHandler($closure); MessageHandler::getInstance()->setExceptionHandler($closure);
} }
public function setOnceHandler(\Closure $closure)
{
MessageHandler::getInstance()->setOnceHandler($closure);
}
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!