mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 00:55:35 +08:00
add some example function
This commit is contained in:
27
src/cqbot/mods/Entertain.php
Normal file
27
src/cqbot/mods/Entertain.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,67 @@ class Example extends ModBase
|
|||||||
case "ping":
|
case "ping":
|
||||||
$this->reply("pong");
|
$this->reply("pong");
|
||||||
return true;
|
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":
|
case "test":
|
||||||
$this->reply("Hello world");
|
$this->reply("Hello world");
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
35
src/cqbot/mods/Help.php
Normal file
35
src/cqbot/mods/Help.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,7 +27,9 @@ class Scheduler
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function tick($id, $tick_time) {
|
public function tick($id, $tick_time) {
|
||||||
|
//900秒执行一次文件保存功能
|
||||||
if($tick_time - $this->framework->run_time % 900 == 0) CQUtil::saveAllFiles();
|
if($tick_time - $this->framework->run_time % 900 == 0) CQUtil::saveAllFiles();
|
||||||
|
|
||||||
//这里添加计时器上处理的内容
|
//这里添加计时器上处理的内容
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user