-
Notifications
You must be signed in to change notification settings - Fork 0
进阶文档
LiangXiang Shen edited this page Mar 6, 2019
·
12 revisions
前面已经基本了解了 modules 和 plugins,接着继续说相关的使用
所有的 plugin 均应继承 kjBot\Framework\Plugin 类
namespace kjBotModule\kjBot_Dev\Demo;
use kjBot\Framework\Plugin;
use kjBot\Framework\Message;
use kjBot\Framework\Event\MessageEvent;
class Main extends Plugin{}在 framework/Plugin.php 中可以看到,有两个变量和一个方法
namespace kjBot\Framework;
abstract class Plugin{
public $handleDepth = 0; //该数值决定了要捕获事件的深度,可能的值为[0-3]
public $handleQueue = false; //声明是否要捕获消息队列
private function beforePostMessage(array &$messageQueue){}
}handleDepth 为捕获事件的深度,决定了事件产生时将要调用的方法名
$handleDepth |
方法名 |
|---|---|
| 0 | handle |
| 1 | {$post_type} |
| 2 | {$post_type}_{{$post_type}_type} |
| 3 | {$post_type}_{{$post_type}_type}_{$sub_type} |
方法必须且只能接受一个参数,传入的参数为事件的实例
示例(命名空间及use已省略):
// Demo1
class Main extends Plugin {
public $handleDepth = 0;
public function handle($event) {}
}
// Demo2
class Main extends Plugin {
public $handleDepth = 1;
public function message($event) {
//捕获post_type为message的消息
}
}
// Demo3
class Main extends Plugin {
public $handleDepth = 2;
public function message_group($event) {
//捕获post_type为message、message_type为group的消息
}
}
// Demo4
class Main extends Plugin {
public $handleDepth = 3;
public function message_group_normal($event) {
//捕获post_type为message、message_type为group、sub_type为normal的消息
}
}如果在捕获处理某事件时需要使用到 CoolQ 实例,需要声明常量 const cq_{方法名} = true;,并在方法名前添加 coolq_ 以注明,此时该方法第一参数仍然传入事件实例,第二参数为 CoolQ 实例。
class Main extends Plugin {
public $handleDepth = 3;
const cq_message_group_normal = true; //声明方法需要 CoolQ 实例
public function coolq_message_group_normal($event, kjBot\SDK\CoolQ $cq) {
//捕获post_type为message、message_type为group、sub_type为normal的消息
}
}
// 或者
class Main extends Plugin {
public $handleDepth = 3;
public function message_group_normal($event, kjBot\SDK\CoolQ $cq) {
//捕获post_type为message、message_type为group、sub_type为normal的消息
global $kjBot;
$kjBot->getCoolQ()->getStrangerInfo(123456); //使用 CoolQ 实例,比如获取 qq 号为123456的信息
}
}