Skip to content

Commit

Permalink
hkbot支持词条
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Jun 5, 2024
1 parent aba3ad0 commit 5817d7c
Show file tree
Hide file tree
Showing 8 changed files with 767 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hkbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
第一次运行会自动生成配置文件`config/net.cutereimu.hkbot/HKConfig.yml`,如下:

```yaml
# 是否启用插件
# 是否启用推送
enable: true
# speedrun推送间隔
speedrun_push_delay: 300
Expand All @@ -14,6 +14,9 @@ speedrun_push_qq_group:
- 12345678
# speedrun的API Key
speedrun_api_key: xxxxxxxx
qq:
# 管理员QQ
super_admin_qq: 12345678
```

修改配置文件后重新启动即可
133 changes: 133 additions & 0 deletions hkbot/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package hkbot

import (
. "github.com/CuteReimu/onebot"
"log/slog"
"strconv"
"strings"
)

func init() {
addCmdListener(&delAdmin{})
addCmdListener(&addAdmin{})
addCmdListener(&listAllAdmin{})
}

type delAdmin struct{}

func (d *delAdmin) Name() string {
return "删除管理员"
}

func (d *delAdmin) ShowTips(int64, int64) string {
return "删除管理员 对方QQ号"
}

func (d *delAdmin) CheckAuth(_ int64, senderId int64) bool {
return IsSuperAdmin(senderId)
}

func (d *delAdmin) Execute(_ *GroupMessage, content string) MessageChain {
ss := strings.Split(content, " ")
qqNumbers := make([]int64, 0, len(ss))
for _, s := range ss {
s = strings.TrimSpace(s)
if len(s) == 0 {
continue
}
qq, err := strconv.ParseInt(s, 10, 64)
if err != nil {
slog.Error("parse failed: "+s, "error", err)
return nil
}
if IsSuperAdmin(qq) {
return MessageChain{&Text{Text: "你不能删除自己"}}
}
if !IsAdmin(qq) {
return MessageChain{&Text{Text: s + "并不是管理员"}}
}
qqNumbers = append(qqNumbers, qq)
}
if len(qqNumbers) == 0 {
return nil
}
for _, qq := range qqNumbers {
RemoveAdmin(qq)
}
ret := "已删除管理员"
if len(qqNumbers) == 1 {
ret += ":" + strconv.FormatInt(qqNumbers[0], 10)
}
return MessageChain{&Text{Text: ret}}
}

type addAdmin struct{}

func (a *addAdmin) Name() string {
return "增加管理员"
}

func (a *addAdmin) ShowTips(int64, int64) string {
return "增加管理员 对方QQ号"
}

func (a *addAdmin) CheckAuth(_ int64, senderId int64) bool {
return IsSuperAdmin(senderId)
}

func (a *addAdmin) Execute(_ *GroupMessage, content string) MessageChain {
ss := strings.Split(content, " ")
qqNumbers := make([]int64, 0, len(ss))
for _, s := range ss {
s = strings.TrimSpace(s)
if len(s) == 0 {
continue
}
qq, err := strconv.ParseInt(s, 10, 64)
if err != nil {
slog.Error("parse failed: "+s, "error", err)
return nil
}
if IsSuperAdmin(qq) || IsAdmin(qq) {
return MessageChain{&Text{Text: s + "已经是管理员了"}}
}
qqNumbers = append(qqNumbers, qq)
}
if len(qqNumbers) == 0 {
return nil
}
for _, qq := range qqNumbers {
AddAdmin(qq)
}
ret := "已增加管理员"
if len(qqNumbers) == 1 {
ret += ":" + strconv.FormatInt(qqNumbers[0], 10)
}
return MessageChain{&Text{Text: ret}}
}

type listAllAdmin struct{}

func (l *listAllAdmin) Name() string {
return "查看管理员"
}

func (l *listAllAdmin) ShowTips(int64, int64) string {
return ""
}

func (l *listAllAdmin) CheckAuth(int64, int64) bool {
return true
}

func (l *listAllAdmin) Execute(*GroupMessage, string) MessageChain {
superAdmin := hkConfig.GetInt64("qq.super_admin_qq")
admin := permData.GetIntSlice("admin")
s := make([]string, 0, len(admin)+2)
s = append(s, "管理员列表:")
s = append(s, strconv.FormatInt(superAdmin, 10))
for _, qq := range admin {
s = append(s, strconv.Itoa(qq))
}
return MessageChain{&Text{Text: strings.Join(s, "\n")}}
}
63 changes: 63 additions & 0 deletions hkbot/bots.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package hkbot

import (
"encoding/json"
"github.com/CuteReimu/YinYangJade/iface"
. "github.com/CuteReimu/onebot"
"github.com/go-resty/resty/v2"
"log/slog"
"regexp"
"slices"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -34,6 +36,67 @@ func Init(b *Bot) {
doTimer()
}
}()
go func() {
for range time.Tick(24 * time.Hour) {
B.Run(clearExpiredImages)
}
}()
B.ListenGroupMessage(cmdHandleFunc)
B.ListenGroupMessage(handleDictionary)
}

var cmdMap = make(map[string]iface.CmdHandler)

func cmdHandleFunc(message *GroupMessage) bool {
if !slices.Contains(hkConfig.GetIntSlice("speedrun_push_qq_group"), int(message.GroupId)) {
return true
}
chain := message.Message
if len(chain) == 0 {
return true
}
if at, ok := chain[0].(*At); ok && at.QQ == strconv.FormatInt(B.QQ, 10) {
chain = chain[1:]
if len(chain) > 0 {
if text, ok := chain[0].(*Text); ok && len(strings.TrimSpace(text.Text)) == 0 {
chain = chain[1:]
}
}
if len(chain) == 0 {
chain = append(chain, &Text{Text: "查看帮助"})
}
}
var cmd, content string
if len(chain) == 1 {
if text, ok := chain[0].(*Text); ok {
arr := strings.SplitN(strings.TrimSpace(text.Text), " ", 2)
cmd = strings.TrimSpace(arr[0])
if len(arr) > 1 {
content = strings.TrimSpace(arr[1])
}
}
}
if len(cmd) == 0 {
return true
}
if h, ok := cmdMap[cmd]; ok {
if h.CheckAuth(message.GroupId, message.Sender.UserId) {
groupMsg := h.Execute(message, content)
if len(groupMsg) > 0 {
sendGroupMessage(message, groupMsg...)
}
return true
}
}
return true
}

func addCmdListener(handler iface.CmdHandler) {
name := handler.Name()
if _, ok := cmdMap[name]; ok {
panic("repeat command: " + name)
}
cmdMap[name] = handler
}

var re = regexp.MustCompile("<.*?>")
Expand Down
84 changes: 84 additions & 0 deletions hkbot/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package hkbot

import (
. "github.com/CuteReimu/onebot"
"math/rand/v2"
"slices"
"strconv"
"strings"
)

func init() {
addCmdListener(&showTips{})
addCmdListener(&ping{})
addCmdListener(&roll{})
}

type showTips struct{}

func (t *showTips) Name() string {
return "查看帮助"
}

func (t *showTips) ShowTips(int64, int64) string {
return ""
}

func (t *showTips) CheckAuth(int64, int64) bool {
return true
}

func (t *showTips) Execute(msg *GroupMessage, _ string) MessageChain {
var ret []string
for _, h := range cmdMap {
if h.CheckAuth(msg.GroupId, msg.Sender.UserId) {
if tip := h.ShowTips(msg.GroupId, msg.Sender.UserId); len(tip) > 0 {
ret = append(ret, tip)
}
}
}
slices.Sort(ret)
return MessageChain{&Text{Text: "你可以使用以下功能:\n" + strings.Join(ret, "\n")}}
}

type ping struct{}

func (p *ping) Name() string {
return "ping"
}

func (p *ping) ShowTips(int64, int64) string {
return ""
}

func (p *ping) CheckAuth(int64, int64) bool {
return true
}

func (p *ping) Execute(_ *GroupMessage, content string) MessageChain {
if len(content) == 0 {
return MessageChain{&Text{Text: "pong"}}
}
return nil
}

type roll struct{}

func (r *roll) Name() string {
return "roll"
}

func (r *roll) ShowTips(int64, int64) string {
return ""
}

func (r *roll) CheckAuth(int64, int64) bool {
return true
}

func (r *roll) Execute(message *GroupMessage, content string) MessageChain {
if len(content) == 0 {
replyGroupMessage(true, message, &Text{Text: "roll: " + strconv.Itoa(rand.IntN(100))}) //nolint:gosec
}
return nil
}
19 changes: 19 additions & 0 deletions hkbot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
var (
hkConfig = viper.New()
hkData = viper.New()
permData = viper.New()
qunDb = viper.New()
)

func initConfig() {
Expand All @@ -26,6 +28,7 @@ func initConfig() {
hkConfig.SetDefault("speedrun_push_delay", int64(300))
hkConfig.SetDefault("speedrun_push_qq_group", []int64{12345678})
hkConfig.SetDefault("speedrun_api_key", "abcdefjhijk")
hkConfig.SetDefault("qq.super_admin_qq", int64(12345678))
_ = hkConfig.SafeWriteConfigAs(filepath.Join("config", "net.cutereimu.hkbot", "HKConfig.yml"))
if err := hkConfig.ReadInConfig(); err != nil {
panic(err)
Expand All @@ -38,4 +41,20 @@ func initConfig() {
if err := hkData.ReadInConfig(); err != nil {
panic(err)
}

permData.AddConfigPath(filepath.Join("data", "net.cutereimu.hkbot"))
permData.SetConfigName("PermData")
permData.SetConfigType("yml")
_ = permData.SafeWriteConfigAs(filepath.Join("data", "net.cutereimu.hkbot", "PermData.yml"))
if err := permData.ReadInConfig(); err != nil {
panic(err)
}

qunDb.AddConfigPath(filepath.Join("data", "net.cutereimu.hkbot"))
qunDb.SetConfigName("QunDb")
qunDb.SetConfigType("yml")
_ = qunDb.SafeWriteConfigAs(filepath.Join("data", "net.cutereimu.hkbot", "QunDb.yml"))
if err := qunDb.ReadInConfig(); err != nil {
panic(err)
}
}
Loading

0 comments on commit 5817d7c

Please sign in to comment.