Commit d4a8ea8b by HanSon

增加 onceHandler

1 parent 1bf3ea16
......@@ -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) {
/** @var $message Message */
......@@ -63,8 +76,10 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
// 群组@我回复
} elseif ($message->fromType === 'Group') {
if (str_contains($message->content, '设置群名称') && $message->from['Alias'] === 'hanson1994') {
group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content));
if (str_contains($message->content, '设置群名称')) {
if (isset($message->sender['Alias']) && $message->sender['Alias'] === 'hanson1994') {
group()->setGroupName($message->from['UserName'], str_replace('设置群名称', '', $message->content));
}
}
if ($message->isAt) {
......@@ -197,7 +212,7 @@ $robot->server->setMessageHandler(function ($message) use ($path) {
return $message->content;
} elseif ($message->action === '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 测试群');
return '行不改名,坐不改姓!';
}
......
......@@ -32,6 +32,16 @@ class Group extends Collection
}
/**
* 重置数据
*
* @param $array
*/
public function reset($array)
{
static::$instance = new Group($array);
}
/**
* 判断是否群组
*
* @param $userName
......@@ -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
......
......@@ -32,6 +32,8 @@ class MessageHandler
private $exceptionHandler;
private $onceHandler;
private $sync;
private $messageFactory;
......@@ -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接口
*/
public function listen()
{
if($this->onceHandler instanceof Closure){
call_user_func_array($this->onceHandler, []);
}
while (true){
if($this->customHandler instanceof Closure){
call_user_func_array($this->customHandler, []);
......
......@@ -339,4 +339,9 @@ class Server
{
MessageHandler::getInstance()->setExceptionHandler($closure);
}
public function setOnceHandler(\Closure $closure)
{
MessageHandler::getInstance()->setOnceHandler($closure);
}
}
\ 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!