-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
239 lines (203 loc) · 5.16 KB
/
cmd.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
)
// TODO: store in db, used to be a global motd
var motd = ""
// Command main entry for parsing command
func Command(db *sql.DB, channel int64, who string, isAdmin bool, msg string) (result string, err error) {
err = errors.New("execuse me?")
result = ""
msg2 := strings.ReplaceAll(msg, "@ac_dailybot", "")
msg2 = strings.ReplaceAll(msg2, "@testturnipbot", "")
cmdstring := strings.Fields(msg2)
if len(cmdstring) == 0 { // should be never happen
err = nil
return
}
command := cmdstring[0]
lowerCmd := strings.ToLower(command)
param := strings.TrimSpace(msg2[len(lowerCmd):])
adminMark := ""
if isAdmin {
adminMark = "*"
}
log.Printf("command [%s%s] `%s` `%s`", who, adminMark, lowerCmd, param)
var replyAllList = false
var replyAllTurnipList = false
sendMessageType = 0
switch lowerCmd {
case "/motd":
if who == backdoorUser {
log.Printf("set motd = `%s`", param)
motd = param
err = nil
result = ""
}
case "/hl", "/hi":
err = Highlight(db, channel, who, param)
if err == nil {
replyAllList = true
}
case "/hl_remove":
err = Remove(db, channel, who, param, isAdmin, false)
case "/hl_reset":
log.Printf("reset `%s` `%s`", lowerCmd, param)
if who == param {
err = ResetAll(db, channel, isAdmin, false)
} else {
err = errors.New("簽名以重設資料")
}
case "/hl_list":
replyAllList = true
err = nil
case "/turnip", "/posh":
err = Turnip(db, channel, who, strings.ToLower(msg))
if err == nil {
replyAllTurnipList = true
}
case "/turnip_remove":
err = Remove(db, channel, who, param, isAdmin, true)
case "/turnip_reset":
log.Printf("reset `%s` `%s`", lowerCmd, param)
if who == param {
err = ResetAll(db, channel, isAdmin, true)
} else {
err = errors.New("簽名以重設資料")
}
case "/turnip_list":
replyAllTurnipList = true
err = nil
default:
// err = errors.New("what's run?")
log.Printf("receive unknown message: %s", msg)
err = nil
return
}
if err != nil {
return
}
if replyAllList {
result, err = HighlightList(db, channel)
if err == nil {
fmt.Printf("DEBUG: motd `%s` result `%s`", motd, result)
result = motd + "\n" + "=== #動森高光 ===\n" + result
}
sendMessageType = typeMessageHighlight
return
}
if replyAllTurnipList {
result, err = TurnipList(db, channel)
if err == nil {
fmt.Printf("DEBUG: motd `%s` result `%s`", motd, result)
result = motd + "\n" + "=== #動森大頭菜 ===\n" + result
}
sendMessageType = typeMessageTurnip
return
}
return
}
// Highlight my data /hl
func Highlight(db *sql.DB, channelid int64, who string, msg string) (err error) {
err = nil
if msg == "" {
err = errors.New("say something")
return
}
return replaceHLEntry(db, channelid, who, msg)
}
// Remove my data /remove
func Remove(db *sql.DB, channelid int64, who string, user string, isAdmin bool, isTurnip bool) (err error) {
err = nil
if user == "" {
user = who
}
if user != who && isAdmin == false {
log.Printf("normal user can only remove his/her own data")
return errors.New("normal user can only remove his/her own data")
}
if isTurnip {
removeTurnipEntry(db, channelid, user)
return
}
return removeHLEntry(db, channelid, user)
}
// HighlightList all the result in DB
func HighlightList(db *sql.DB, channelid int64) (result string, err error) {
err = nil
result = ""
return queryAllHLEntry(db, channelid)
}
// ResetAll in channel
func ResetAll(db *sql.DB, channelid int64, isAdmin bool, isTurnip bool) (err error) {
err = nil
if isAdmin == false {
return errors.New("只有管理員能重設資料")
}
if isTurnip {
return resetAllTurnipEntry(db, channelid)
}
return resetAllHLEntry(db, channelid)
}
// --- Turnip ---
// Turnip my data /turnip
func Turnip(db *sql.DB, channelid int64, who string, msg string) (err error) {
err = nil
errReturn := errors.New("範例語法: \n/turnip buy 123 sell 101,-,103,")
if msg == "" {
err = errors.New("say something")
return
}
price := new(Price)
price.buy = 0
r := regexp.MustCompile("\\s*buy\\s*(\\d+)\\s*sell(.*)$")
regexResult := r.FindStringSubmatch(msg)
if len(regexResult) == 0 {
return errReturn
}
price.buy, err = strconv.Atoi(regexResult[1])
if err != nil {
return errReturn
}
trim := strings.ReplaceAll(regexResult[2], " ", "")
sell := strings.Split(trim, ",")
for i, s := range sell {
if s == "" {
break
}
if s == "-" {
price.sell[i] = 0
continue
}
p, err := strconv.Atoi(s)
if err != nil {
fmt.Printf("atoi error `%s`\n", s)
return errReturn
}
if p > MaxOfTurnip {
return errReturn
}
price.sell[i] = p
}
fmt.Printf("input: `%s` regex: buy `%d` sell `%d %d %d %d %d %d %d %d %d %d %d %d`\n", msg, price.buy,
price.sell[0], price.sell[1],
price.sell[2], price.sell[3],
price.sell[4], price.sell[5],
price.sell[6], price.sell[7],
price.sell[8], price.sell[9],
price.sell[10], price.sell[11],
)
return replaceTurnipEntry(db, channelid, who, price)
}
// TurnipList all the result in DB
func TurnipList(db *sql.DB, channelid int64) (result string, err error) {
err = nil
result = ""
return queryAllTurnipEntry(db, channelid)
}