This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit.go
223 lines (199 loc) · 5.23 KB
/
edit.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
package main
import (
"fmt"
"io"
"strconv"
"strings"
"github.com/chzyer/readline"
"github.com/fatih/color"
"github.com/legolord208/stdutil"
)
func doEdit() {
loadRules()
colorBack := color.New(color.BgBlack, color.FgWhite)
READLINE, err := readline.New("")
if err != nil {
stdutil.PrintErr("Could not init readline library.", nil)
return
}
colorBack.Println("Edit mode.")
colorBack.Println("To start normal mode, do 'DiscordAR run <token>'")
fmt.Println()
fmt.Println("Get started! Create rule with 'new'. List rules with 'rules'. Edit rules with 'edit'")
for i := 0; i < 3; i++ {
fmt.Println()
}
for {
READLINE.SetPrompt("")
line := rlWrapper(READLINE.Readline())
switch line {
case "exit":
return
case "new":
fmt.Println()
fmt.Println("What will somebody type to trigger this reply?")
READLINE.SetPrompt(colorBack.Sprint("Message:"))
msg := rlWrapper(READLINE.Readline())
fmt.Println()
fmt.Println("Will this be exact? (say 'true' or 'false')")
var exact bool
for {
READLINE.SetPrompt(colorBack.Sprint("Exact:"))
exactStr := rlWrapper(READLINE.Readline())
if exactStr == "true" {
exact = true
} else if exactStr == "false" {
exact = false
} else {
fmt.Println("Invalid response.")
continue
}
break
}
fmt.Println()
fmt.Println("What is the reply?")
READLINE.SetPrompt(colorBack.Sprint("Reply:"))
reply := rlWrapperRaw(READLINE.Readline())
rules = append(rules, rule{
Msg: msg,
Exact: exact,
Reply: reply,
})
if saveRules() {
fmt.Println("Saved rule!")
}
case "rules":
for i, rule := range rules {
fmt.Println("Rule #" + strconv.Itoa(i+1))
fmt.Println("\tMessage: " + rule.Msg)
fmt.Println("\tExact: " + strconv.FormatBool(rule.Exact))
fmt.Println("\tReply: " + rule.Reply)
fmt.Println("\tOnly from filter: " + strings.Join(rule.From, ", "))
fmt.Println("\tNot from filter: " + strings.Join(rule.NotFrom, ", "))
fmt.Println("\tOnly in channel filter: " + strings.Join(rule.InChannel, ", "))
fmt.Println("\tNot in channel filter: " + strings.Join(rule.NotInChannel, ", "))
}
case "edit":
fmt.Println("Select a rule.")
fmt.Println()
for i, rule := range rules {
fmt.Println(strconv.Itoa(i+1) + ". " + rule.Msg)
}
fmt.Println()
READLINE.SetPrompt(colorBack.Sprint("Rule:"))
ruleStr := rlWrapper(READLINE.Readline())
ruleNr, err := strconv.Atoi(ruleStr)
if err != nil {
stdutil.PrintErr("Not a number", nil)
continue
}
ruleNr--
if ruleNr < 0 || ruleNr >= len(rules) {
stdutil.PrintErr("Rule does not exist", nil)
continue
}
rule := rules[ruleNr]
deleted := false
property_loop:
for {
fmt.Println()
fmt.Println("Select property to edit")
fmt.Println()
fmt.Println("1. Message")
fmt.Println("2. Exact")
fmt.Println("3. Reply")
fmt.Println("4. Only from filter")
fmt.Println("5. Not from filter")
fmt.Println("6. Only in channel filter")
fmt.Println("7. Not in channel filter")
fmt.Println("8. Cancel")
fmt.Println("9. DELETE!")
fmt.Println()
READLINE.SetPrompt(colorBack.Sprint("Property:"))
prop := rlWrapper(READLINE.Readline())
switch prop {
case "1":
READLINE.SetPrompt(colorBack.Sprint("Message:"))
rule.Msg = rlWrapper(READLINE.Readline())
case "2":
rule.Exact = !rule.Exact
fmt.Println("Exact toggled. Now: " + strconv.FormatBool(rule.Exact))
case "3":
READLINE.SetPrompt(colorBack.Sprint("Reply:"))
rule.Reply = rlWrapperRaw(READLINE.Readline())
case "4":
arrEdit(&rule.From, READLINE, colorBack)
case "5":
arrEdit(&rule.NotFrom, READLINE, colorBack)
case "6":
arrEdit(&rule.InChannel, READLINE, colorBack)
case "7":
arrEdit(&rule.NotInChannel, READLINE, colorBack)
case "8":
break property_loop
case "9":
rules = append(rules[:ruleNr], rules[ruleNr+1:]...)
deleted = true
break property_loop
}
}
if !deleted {
rules[ruleNr] = rule
}
if saveRules() {
fmt.Println("Saved!")
}
default:
fmt.Println("Unknown command.")
continue
}
for i := 0; i < 3; i++ {
fmt.Println()
}
}
}
func rlWrapper(line string, err error) string {
return strings.ToLower(strings.TrimSpace(rlWrapperRaw(line, err)))
}
func rlWrapperRaw(line string, err error) string {
switch err {
case nil:
return line
default:
stdutil.PrintErr("Couldn't read line", err)
fallthrough
case io.EOF:
fallthrough
case readline.ErrInterrupt:
exit()
return ""
}
}
func arrEdit(arr *[]string, READLINE *readline.Instance, colorBack *color.Color) {
fmt.Println("Existing:")
for _, item := range *arr {
fmt.Println("\t" + item)
}
fmt.Println()
fmt.Println("Now, write IDs like this:")
fmt.Println("+ID to add.")
fmt.Println("-ID to remove.")
fmt.Println("Example: +123454321")
fmt.Println()
READLINE.SetPrompt(colorBack.Sprint("ID:"))
id := rlWrapper(READLINE.Readline())
if strings.HasPrefix(id, "+") {
*arr = append(*arr, id[1:])
} else if strings.HasPrefix(id, "-") {
remove := id[1:]
var arr2 []string
for _, item := range *arr {
if item != remove {
arr2 = append(arr2, item)
}
}
*arr = arr2
} else {
stdutil.PrintErr("Invalid.", nil)
}
}