--- title: 从零开始搭建一个群管机器人 tags: bot categories: bot,go date: 2021-12-28 22:05:42 --- # 环境搭建 ## 安装golang + 从golang[中文官网](https://studygolang.com/dl)下载go语言安装包 + 下载完成后解压配置环境变量 命令行输入**go version** ![](https://gitee.com/aabbccddeeeff/img/raw/master//img/202112282212881.png) ## 安装goland + 从goland官网下载goland安装包 + 双击后正常安装就可 # 创建项目 ## 初始化项目 + 打开goland,创建go项目,取名为leafBot-plugin + 初始化项目```go mod init github.com/huoxue1/fan``` + 安装leafbot依赖 ```go get github.com/huoxue1/leafbot``` + 跟项目下创建**main.go**文件 ## 编写main.go文件,初始化leafBot + 在**main.go**中修改package为main + 创建main方法,添加如下代码 ```go // main方法,项目运行起点 func main() { // 创建一个cqhttp的driver driver := cqhttp_default_driver.NewDriver() // 为leafBot加载该驱动 leafbot.LoadDriver(driver) // 初始化leafBot leafbot.InitBots() // 运行cqhttp驱动 driver.Run() } ``` + 一般main.go会自动导入如下依赖 ```go import ( // leafbot核心依赖 "github.com/huoxue1/leafbot" // leafbot的cqhttp与leafbot直接结合所用的依赖 "github.com/huoxue1/leafbot/cqhttp_default_driver" ) ``` + main.go最终结果为 ```go package main import ( "github.com/huoxue1/leafbot" "github.com/huoxue1/leafbot/cqhttp_default_driver" ) func main() { driver := cqhttp_default_driver.NewDriver() leafbot.LoadDriver(driver) leafbot.InitBots() driver.Run() } ``` + 首次运行该项目,输入bot的qq号即可 ![](https://gitee.com/aabbccddeeeff/img/raw/master//img/202112282225028.png) + 再次运行,因为还需要初始化go-cqhttp的配置文件,所有好需要交互 ![](https://gitee.com/aabbccddeeeff/img/raw/master//img/202112282226998.png) + 再次运行即可运行成功,生成二维码后使用手机扫码登录即可 ![](https://gitee.com/aabbccddeeeff/img/raw/master//img/202112282227074.png) # 插件编写 ## 创建插件项目 + 在项目跟目录添加文件夹plugin,在plugin下继续创建文件夹plugin-manager,并且创建manager.go文件 + 在manager.go文件夹中添加如下代码 ```go package group_manager import ( "fmt" "strconv" "github.com/huoxue1/leafbot" "github.com/huoxue1/leafbot/message" ) func init() { manager() } func manager() { // 创建一个leafBot插件,插件名为group-manager plugin := leafbot.NewPlugin("group-manager") // 为plugin添加一个matcher,匹配器为Onstart,即匹配消息开始为升为管理的语句 plugin.OnStart("升为管理", leafbot.Option{ // 添加matcher的权重,权重越低,越先匹配 Weight: 1, // 设置匹配成功后是否匹配其他matcher Block: true, Rules: []leafbot.Rule{}, }).Handle(func(ctx *leafbot.Context) { // 遍历事件中的message for _, v := range ctx.Event.Message { // 判断事件中的消息类型weiat if v.Type == "at" { // 取出at的qq号,并强制转为整形 qq, _ := strconv.Atoi(v.Data["qq"]) // 调用api,为其设置为管理员 ctx.Bot.(leafbot.OneBotApi).SetGroupAdmin(ctx.Event.GroupId, qq, true) // 发送消息,xxx已升为管理员 ctx.Send(message.Text(fmt.Sprintf("%v已经升为管理员", qq))) } } }) // 取消管理 plugin.OnStart("取消管理",leafbot.Option{ Weight: 1, Block: true, }).Handle(func(ctx *leafbot.Context) { for _, v := range ctx.Event.Message { if v.Type == "at" { qq, _ := strconv.Atoi(v.Data["qq"]) ctx.Bot.(leafbot.OneBotApi).SetGroupAdmin(ctx.Event.GroupId, qq, false) ctx.Send(message.Text(fmt.Sprintf("%v已被取消管理员", qq))) } } }) } ``` + 在main.go中导入该插件,即在import中添加 ``` import( "github.com/huoxue1/leafbot" "github.com/huoxue1/leafbot/cqhttp_default_driver" _ "github.com/huoxue1/fan/plugin/group-mamanger" ) ``` + 运行后,在机器人为群主的群里发送,升为管理并且艾特需要升为管理的人即可设置管理 # leafBot解释 leafBot为一个go语言版本实现了onebot11协议的SDK,推荐对接onebot为go-cqhttp leafBot除了Onstart匹配器外,还有匹配器 ```go // 命令匹配 OnCommand(command string, options ...Option) Matcher // 消息匹配 OnMessage(messageType string, options ...Option) Matcher // 请求事件匹配 OnRequest(requestType string, options ...Option) Matcher // 提醒事件匹配 OnNotice(noticeType string, options ...Option) Matcher // 元事件匹配 OnMeta(options ...Option) Matcher // 正则匹配 OnRegex(regexMatcher string, options ...Option) Matcher // 匹配字符串开始 OnStart(start string, options ...Option) Matcher // 匹配字符串结尾 OnEnd(end string, options ...Option) Matcher // 消息完全匹配 OnFullMatch(content string, options ...Option) Matcher // 群组消息完全匹配 OnFullMatchGroup(content string, options ...Option) Matcher // bot连接回调 OnConnect(options ...Option) Matcher // bot断开回调 OnDisConnect(options ...Option) Matcher ``` 更多内容参考[LeafBot](https://github.com/huoxue1/leafbot)