# 起步
让我们开始使用go-Pichubot吧!
# 安装
使用 Golang 自带的包管理,可以免去您clone代码的繁琐。
go get github.com/0ojixueseno0/go-Pichubot
1
# Hello World
// A Example with PichuBot
package main
import (
Pichubot "github.com/0ojixueseno0/go-Pichubot"
)
func action(eventinfo Pichubot.MessagePrivate) {
if eventinfo.Sender.UserID == 12345678 {
Pichubot.SendPrivateMsg("Hello World!", eventinfo.Sender.UserID) // 发送私聊消息
}
}
func main() {
// 向监听器里添加函数
Pichubot.Listeners.OnPrivateMsg = append(Pichubot.Listeners.OnPrivateMsg, action)
//初始化一个Bot 并更改配置
Bot := Pichubot.NewBot()
Bot.Config = Pichubot.Config{
Loglvl: Pichubot.LOGGER_LEVEL_INFO,
Host: "0.0.0.0:6700",
MasterQQ: "12345678",
Path: "/",
MsgAwait: true
}
Bot.Run()
}
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
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
开启MiraiGo/CQHttp(或其他OneBot协议下的QQ机器人)后,运行Demo程序
使用你的QQ向机器人发送一条消息,机器人就会回复你:Hello World!
# 快速上手
# 初始化一个皮丘Bot
Bot := Pichubot.NewBot() // 将Bot定义为Pichubot对象
Bot.Config = Pichubot.Config{
Loglvl: Pichubot.LOGGER_LEVEL_INFO, // 设置控制台与日志文件打印/保存的最低级别日志
Host: "0.0.0.0:6700", // 对应机器人程序的WebSocket地址
MasterQQ: "12345678", // 机器人主人的QQ号码(目前没用)
Path: "/" // websocket请求的路径
MsgAwait: true
}
Bot.Run() // 运行Bot
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 向事件池/监听器添加函数
每当PichuBot接收到来自机器人的数据包后将会进行解析,并调用对应事件池内的所有函数
使用 append() 方法,向指定数组内添加函数
func action(...){...}
...
Pichubot.Listeners.OnPrivateMsg = append(Pichubot.Listeners.OnPrivateMsg, action)
1
2
3
2
3
# 让机器人执行你要的操作
使用封装好的函数,即可快捷执行相应的功能
例:使用 Pichubot.SendPrivateMsg
方法让机器人发送一条私聊消息
Pichubot.SendPrivateMsg("Hello World!", 12345678)
// -> string int64 -> map[string]interface{} error
1
2
2
# 进阶操作
# 长事件(Long Event)
使用 Pichubot.Event...
方法建立长事件
例:复读机功能
// 省略初始化机器人 以及添加函数至事件池
func repeater(eventinfo Pichubot.MessageGroup) {
if eventinfo.Message == "复读机" {
// 新建长事件方法
event := Pichubot.NewEvent(eventinfo.Sender.UserID, eventinfo.GroupID, "fdj")
defer event.Close()
Pichubot.SendGroupMsg("我,就是人类的本质", eventinfo.GroupID)
for {
r := <-*event.Channel // 持续获取传入channel的值(string)
if r == "关闭复读机" {
Pichubot.SendGroupMsg("复读机已关闭", eventinfo.GroupID)
break
} else {
Pichubot.SendGroupMsg(r, eventinfo.GroupID)
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
longEvents
函数用于判断消息是否符合事件条件,符合则向事件频道传参
func longEvents(eventinfo Pichubot.MessageGroup) {
for _, value := range Pichubot.LongEvents { // 匹配事件池内所有事件
if value.GroupID == eventinfo.GroupID { // 匹配事件发生的群号
switch value.EventKey {
case "fdj":
*value.Channel <- eventinfo.Message // 传入数据(string)
}
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
将以上两个函数导入群聊事件池,在群聊内发送“复读机”即可开启复读机功能
# 新建长事件方法
event := Pichubot.NewEvent(eventinfo.Sender.UserID, eventinfo.GroupID, "key") // 新建事件
defer event.Close() // 延迟关闭事件
1
2
2
Pichubot.NewEvent(userid int64, groupid int64, key string) LongEvent {...}
Key | Type | Value |
---|---|---|
userid | 传入触发事件的用户QQ | int64 |
groupid | 传入触发事件的群号(私聊事件可传0) | int64 |
key | 事件key,用于判断事件 | string |