-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
53 lines (47 loc) · 1.5 KB
/
bot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Doko, MessageType } from "../src/index.js";
import { loadEnv } from "../src/utils/env.js";
loadEnv();
if (!process.env.CLIENT_ID || !process.env.TOKEN) {
throw new Error("need env: CLIENT_ID, TOKEN");
}
// 创建机器人实例
const bot = new Doko({
clientId: process.env.CLIENT_ID,
token: process.env.TOKEN,
});
// 回声姬开关
let echo = false;
bot.hook
// 监听连接事件,由Doko发出,所有Doko事件存放于`doko.*`命名空间之下。
.on("doko.connected", () => {
console.log("doko connected!");
})
.on("channel.message", ({ data: { eventBody }, at }) => {
console.log("receive message", JSON.stringify(eventBody, undefined, 2), at);
})
// 监听特定类型(文本类型)的消息事件
.on("channel.message.text", async (data) => {
const { content } = data.data.eventBody.messageBody;
const matcher = content.match(/^echo (?<mode>on|off)$/); // 如果输入的文本是特定指令,做处理
if (matcher) {
echo = matcher.groups?.mode === "on";
data.response({
messageType: MessageType.Text,
messageBody: {
content: `回声姬模式,${matcher.groups?.mode ?? "off"}!`,
},
});
} else if (echo) {
// 若开关被打开,复读每条文本消息√
data.response(
{
messageType: data.data.eventBody.messageType,
messageBody: data.data.eventBody.messageBody,
},
{
reply: true,
}
);
}
});
await bot.start();