Skip to content

Commit

Permalink
更改配置文件增加机器人昵称
Browse files Browse the repository at this point in the history
命令设置only_to_me之后支持通过昵称唤醒
更改了only_tome的实现
  • Loading branch information
huoxue1 committed Sep 9, 2021
1 parent 9e7234c commit ea25cb9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions config/default_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# bot账号配置,现已不用配置,可自动获取
bots: []
# 机器人的昵称,可配置多个
nick_name:
- "小风"
# bot超级管理员账户
admin: 0
# bot运行地址,若和gocq在同一台机器则只需要填写127.0.0.1即可,否则填写0.0.0.0,gocq配置你的公网地址
Expand Down
14 changes: 14 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,29 @@ func checkOnlyTome(event *Event, state *State) {
if event.Message[0].Type == "at" && event.Message[0].Data["qq"] == strconv.Itoa(event.SelfId) {
event.Message = event.Message[1:]
state.Data["only_tome"] = true
return
}
for _, segment := range event.Message {
if segment.Type == "at" && segment.Data["qq"] == strconv.Itoa(event.SelfId) {
state.Data["only_tome"] = true
return
}
}
for _, name := range DefaultConfig.NickName {
if event.Message[0].Type == "text" && strings.HasPrefix(event.Message[0].Data["text"], name) {
state.Data["only_tome"] = true
text := strings.TrimLeft(event.Message[0].Data["text"], name)
event.Message[0].Data["text"] = text
return
}
}
if event.MessageType == "private" {
state.Data["only_tome"] = true
}
state.Data["only_tome"] = false

return

}

/**
Expand Down
1 change: 1 addition & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Bot struct {

type Config struct {
Bots []*Bot `json:"bots" yaml:"bots" hjson:"bots"`
NickName []string `json:"nick_name" yaml:"nick_name" hjson:"nick_name"`
Admin int `json:"admin" yaml:"admin" hjson:"admin"`
Host string `json:"host" yaml:"host" hjson:"host"`
Port int `json:"port" yaml:"port" hjson:"port"`
Expand Down
13 changes: 3 additions & 10 deletions rule.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package leafBot

import "strconv"

type (
Rule func(Event, *Bot, *State) bool
)
Expand All @@ -14,16 +12,11 @@ type (
* @return bool 返回是否验证通过该rule
* example
*/
func OnlyToMe(event Event, _ *Bot, _ *State) bool {
if event.MessageType == "private" {
func OnlyToMe(event Event, _ *Bot, state *State) bool {
b := state.Data["only_tome"].(bool)
if b {
return true
}
msg := event.GetMsg()
for _, segment := range msg {
if segment.Type == "at" && segment.Data["qq"] == strconv.Itoa(event.SelfId) {
return true
}
}

return false
}
Expand Down

0 comments on commit ea25cb9

Please sign in to comment.