@@ -16,6 +16,57 @@ CoolQ C++ SDK 封装了跟 DLL 接口相关的底层逻辑,包括:
1616
1717并且对外提供了更现代的 C++ 接口,从而为更方便地编写插件提供可能。
1818
19+ ## 示例
20+
21+ ``` cpp
22+ #include " cqsdk/cqsdk.h"
23+
24+ namespace app = cq::app; // 插件本身的生命周期事件和管理
25+ namespace event = cq::event; // 用于注册 QQ 相关的事件处理函数
26+ namespace api = cq::api; // 用于调用酷 Q 提供的接口
27+ namespace logging = cq::logging; // 用于日志
28+ namespace message = cq::message; // 提供封装了的 Message 等类
29+
30+ // 初始化 App Id
31+ CQ_INITIALIZE("com.example.demo");
32+
33+ // 插件入口,在静态成员初始化之后,app::on_initialize 事件发生之前被执行,用于配置 SDK 和注册事件回调
34+ CQ_MAIN {
35+ app::on_enable = [ ] {
36+ // logging、api、dir 等命名空间下的函数只能在事件回调函数内部调用,而不能直接在 CQ_MAIN 中调用
37+ logging::debug(u8"启用", u8"插件已启动");
38+ };
39+
40+ event::on_private_msg = [](const cq::PrivateMessageEvent &e) {
41+ logging::debug(u8"消息", u8"收到私聊消息:" + e.message + u8",发送者:" + std::to_string(e.user_id));
42+
43+ try {
44+ api::send_private_msg(e.user_id, e.message); // echo 回去
45+
46+ api::send_msg(e.target, e.message); // 使用 e.target 指定发送目标
47+
48+ // MessageSegment 类提供一些静态成员函数以快速构造消息段
49+ cq::Message msg = cq::MessageSegment::contact(cq::MessageSegment::ContactType::GROUP, 201865589);
50+ msg.send(e.target); // 使用 Message 类的 send 成员函数
51+ } catch (const cq::exception::ApiError &err) {
52+ // API 调用失败
53+ logging::debug(u8"API", u8"调用失败,错误码:" + std::to_string(err.code));
54+ }
55+
56+ e.block(); // 阻止事件继续传递给其它插件
57+ };
58+
59+ event::on_group_msg = [](const auto &e /* 使用 C++ 的 auto 关键字 */ ) {
60+ const auto memlist = api::get_group_member_list(e.group_id); // 获取数据接口
61+ cq::Message msg = u8"本群一共有 "; // string 到 Message 自动转换
62+ msg += std::to_string(memlist.size()) + u8" 个成员"; // Message 类可以进行加法运算
63+ message::send (e.target, msg); // 使用 message 命名空间的 send 函数
64+ };
65+ }
66+ ```
67+
68+ 更多请看 [ src/demo.cpp] ( src/demo.cpp ) 。
69+
1970## 使用方式
2071
2172### 预备
@@ -172,3 +223,10 @@ git checkout 7578a485b181ded330b87cc72726f01e38ff7ed6 -- ports
172223| on_coolq_exit <------+ |
173224+---------------------------------------+
174225```
226+
227+ ## 应用案例
228+
229+ | 项目地址 | 简介 |
230+ | ------- | --- |
231+ | [ JogleLew/coolq-telegram-bot-x] ( https://github.com/JogleLew/coolq-telegram-bot-x ) | QQ / Telegram 群组消息转发机器人 |
232+ | [ dynilath/coolq-dicebot] ( https://github.com/dynilath/coolq-dicebot ) | 酷Q 骰子机器人 |
0 commit comments