add some example function

This commit is contained in:
jerry 2018-07-17 14:35:17 +08:00
parent a60be2351d
commit a728017273
4 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<?php
/**
* 这是一个示例模块php文件你可以直接复制此文件中的代码
* 然后修改其class的名字注意要和.php文件的文件名相同
* 例如新建一个Mailer模块则Mailer模块的文件名字为
* Mailer.php
* 然后更改class Entertain为class Mailer即可
* 功能在execute中编写即可
* 如果需要写判断所有文本的功能则直接在__construct的parent::__construct下方编写自己的内容即可
*/
class Entertain extends ModBase
{
public function __construct(CQBot $main, $data, bool $mod_cmd = false) { parent::__construct($main, $data, $mod_cmd); }
public function execute($it) {
switch ($it[0]) {
case "你好":
$this->reply("你好我是CQBot");
return true;
case "robot":
$this->reply("机器人!\n机器人!");
return true;
}
return false;
}
}

View File

@ -19,6 +19,67 @@ class Example extends ModBase
case "ping":
$this->reply("pong");
return true;
case "王境泽动图":
$msg_help = "【王境泽动图帮助】";
$msg_help .= "\n用法1\n王境泽动图 第一句 第二句 第三句 第四句";
$msg_help .= "\n[如果需要输入空格的话,用下面的方法]";
$msg_help .= "\n王境泽动图 多行\n第一句\n第二句\n第三句\n第四句";
$api = "https://sorry.xuty.tk/api/wangjingze/make";
if (strstr($it[1], "\n") === false && count($it) < 5) {
$this->reply($msg_help);
return true;
}
array_shift($it);
if (mb_substr($it[0], 0, 2) == "多行") {
$ms = implode(" ", $it);
$ms = mb_substr($ms, 2);
$ms = trim($ms);
$ms = explode("\n", $ms);
if (count($ms) < 4) {
$this->reply($msg_help);
return true;
}
$content = [
"3" => $ms[3],
"2" => $ms[2],
"1" => $ms[1],
"0" => $ms[0]
];
} elseif (count($it) >= 4) {
$content = [
"3" => $it[3],
"0" => $it[0],
"1" => $it[1],
"2" => $it[2]
];
} else {
$this->reply($msg_help);
return true;
}
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-Type: application/json; charset=utf-8', 'content' => json_encode($content, JSON_UNESCAPED_UNICODE)));
$context = stream_context_create($opts);
$this->reply("正在生成,请稍等");
$result = file_get_contents($api, false, $context);
if ($result == false) {
$this->reply("抱歉,请求失败,请过一会儿再试吧~");
return true;
}
$result = "https://sorry.xuty.tk" . $result;
$this->reply("[CQ:image,file=" . $result . "]");
return true;
case "随机数":
if (!isset($it[1]) || !isset($it[2])) {
$this->reply("用法: 随机数 开始整数 结束整数");
return true;
}
$c1 = intval($it[1]);
$c2 = intval($it[2]);
if ($c1 > $c2) {
$this->reply("随机数范围错误!应该从小的一方到大的一方!例如:\n随机数 1 99");
return true;
}
$this->reply("生成的随机数是 " . mt_rand($c1, $c2));
return true;
case "test":
$this->reply("Hello world");
return true;

35
src/cqbot/mods/Help.php Normal file
View File

@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: jerry
* Date: 2018/7/17
* Time: 2:21 PM
*/
class Help extends ModBase
{
public function __construct(CQBot $main, $data, bool $mod_cmd = false) { parent::__construct($main, $data, $mod_cmd); }
public function execute($it) {
switch ($it[0]) {
case "帮助":
$msg = "「机器人帮助」";
$msg .= "\n王境泽动图:用来生成表情包";
$msg .= "\ntest测试机器人回复是否正常";
$msg .= "\n随机数:生成一个随机数";
$this->reply($msg);
return true;
case "如何增加机器人功能":
$msg = "机器人功能是在框架中src/cqbot/mods/xxx.php文件中编写的。";
$msg .= "CQBot采用关键词系统你可以直接像现有源码一样添加case在switch里面";
$msg .= "也可以自己新建一个任意名称的Mod名称例如Entertain.php你可以在里面编写娱乐功能。";
$msg .= "你可以直接复制框架中Entertain.php文件的内容进行编辑。";
$msg .= "你也可以在tasks/Scheduler.php中tick函数里添加自己的定时执行的功能。";
$msg .= "预先封装好的机器人函数均在CQUtil类中只需直接使用静态方法调用即可";
$msg .= "更多示例功能会逐渐添加到框架中,记得更新哦~";
$this->reply($msg);
return true;
}
return false;
}
}

View File

@ -27,7 +27,9 @@ class Scheduler
}
public function tick($id, $tick_time) {
//900秒执行一次文件保存功能
if($tick_time - $this->framework->run_time % 900 == 0) CQUtil::saveAllFiles();
//这里添加计时器上处理的内容
}
}