forked from zu1k/tg-keyword-reply-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence.go
executable file
·97 lines (90 loc) · 2.01 KB
/
persistence.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"regexp"
"strconv"
"strings"
"tg-keyword-reply-bot/common"
"tg-keyword-reply-bot/db"
)
const addText = "格式要求:\r\n" +
"`/add 关键字===回复内容`\r\n\r\n" +
"例如:\r\n" +
"`/add 机场===https://jiji.cool`\r\n" +
"就会添加一条规则, 关键词是机场, 回复内容是网址"
const delText = "格式要求:\r\n" +
"`/del 关键字`\r\n\r\n" +
"例如:\r\n" +
"`/del 机场`\r\n" +
"就会删除一条规则,机器人不再回复机场关键词"
/**
* 添加规则
*/
func addRule(gid int64, rule string) {
rules := common.AllGroupRules[gid]
r := strings.Split(rule, "===")
keys, value := r[0], r[1]
if strings.Contains(keys, "||") {
ks := strings.Split(keys, "||")
for _, key := range ks {
_addOneRule(key, value, rules)
}
} else {
_addOneRule(keys, value, rules)
}
db.UpdateGroupRule(gid, rules.String())
}
/**
* 给addRule使用的辅助方法
*/
func _addOneRule(key string, value string, rules common.RuleMap) {
key = strings.Replace(key, " ", "", -1)
rules[key] = value
}
/**
* 删除规则
*/
func delRule(gid int64, key string) {
rules := common.AllGroupRules[gid]
delete(rules, key)
db.UpdateGroupRule(gid, rules.String())
}
/**
* 获取一个群组所有规则的列表
*/
func getRuleList(gid int64) []string {
kvs := common.AllGroupRules[gid]
str := ""
var strs []string
num := 1
group := 0
for k, v := range kvs {
str += "\r\n\r\n规则" + strconv.Itoa(num) + ":\r\n`" + k + "` => `" + v + "` "
num++
group++
if group == 10 {
group = 0
strs = append(strs, str)
str = ""
}
}
strs = append(strs, str)
return strs
}
/**
* 查询是否包含相应的自动回复规则
*/
func findKey(gid int64, input string) string {
kvs := common.AllGroupRules[gid]
for keyword, reply := range kvs {
if strings.HasPrefix(keyword, "re:") {
keyword = keyword[3:]
match, _ := regexp.MatchString(keyword, input)
if match {
return reply
}
} else if strings.Contains(input, keyword) {
return reply
}
}
return ""
}