forked from quackduck/devzat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
289 lines (269 loc) · 8.32 KB
/
util.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"bytes"
"crypto/sha256"
_ "embed"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/acarl005/stripansi"
markdown "github.com/quackduck/go-term-markdown"
)
var (
art = getASCIIArt()
admins = getAdmins()
)
func getAdmins() map[string]string {
data, err := ioutil.ReadFile("admins.json")
if err != nil {
fmt.Println("Error reading admins.json:", err, ". Make an admins.json file to add admins.")
return nil
}
var adminsList map[string]string // id to info
err = json.Unmarshal(data, &adminsList)
if err != nil {
fmt.Println("Error in admins.json formatting.")
return nil
}
return adminsList
}
func getASCIIArt() string {
b, _ := ioutil.ReadFile("art.txt")
if b == nil {
return "sowwy, no art was found, please slap your developer and tell em to add an art.txt file"
}
return string(b)
}
func printUsersInRoom(r *room) string {
names := ""
admins := ""
for _, us := range r.users {
if auth(us) {
admins += us.name + " "
continue
}
names += us.name + " "
}
if len(names) > 0 {
names = names[:len(names)-1] // cut extra space at the end
}
names = "[" + names + "]"
if len(admins) > 0 {
admins = admins[:len(admins)-1]
}
admins = "[" + admins + "]"
return names + " Admins: " + admins
}
func lenString(a string) int {
return len([]rune(stripansi.Strip(a)))
}
func autogenCommands(cmds []cmd) string {
b := new(bytes.Buffer)
w := tabwriter.NewWriter(b, 0, 0, 2, ' ', 0)
for _, cmd := range cmds {
w.Write([]byte(" " + cmd.name + "\t" + cmd.argsInfo + "\t_" + cmd.info + "_ \n")) //nolint:errcheck // bytes.Buffer is never going to err out
}
w.Flush()
return b.String()
}
// check if a user is an admin
func auth(u *user) bool {
_, ok := admins[u.id]
return ok
}
// removes arrows, spaces and non-ascii-printable characters
func cleanName(name string) string {
s := ""
name = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(
strings.TrimSpace(strings.Split(name, "\n")[0]), // use one trimmed line
"<-", ""),
"->", ""),
" ", "-")
if len([]rune(name)) > 27 {
name = string([]rune(name)[:27])
}
for i := 0; i < len(name); i++ {
if 33 <= name[i] && name[i] <= 126 { // ascii printables only: '!' to '~'
s += string(name[i])
}
}
return s
}
func printPrettyDuration(d time.Duration) string {
s := strings.TrimSpace(strings.TrimSuffix(d.Round(time.Minute).String(), "0s"))
if s == "" { // we cut off the seconds so if there's nothing in the string it means it was made of only seconds.
s = "< 1m"
}
return s
}
func mdRender(a string, beforeMessageLen int, lineWidth int) string {
if strings.Contains(a, "![") && strings.Contains(a, "](") {
lineWidth = int(math.Min(float64(lineWidth/2), 200)) // max image width is 200
}
md := string(markdown.Render(a, lineWidth-(beforeMessageLen), 0))
md = strings.TrimSuffix(md, "\n")
split := strings.Split(md, "\n")
for i := range split {
if i == 0 {
continue // the first line will automatically be padded
}
split[i] = strings.Repeat(" ", beforeMessageLen) + split[i]
}
if len(split) == 1 {
return md
}
return strings.Join(split, "\n")
}
// Returns true and the user with the same name if the username is taken, false and nil otherwise
func userDuplicate(r *room, a string) (*user, bool) {
for i := range r.users {
if stripansi.Strip(r.users[i].name) == stripansi.Strip(a) {
return r.users[i], true
}
}
return nil, false
}
func saveBans() {
f, err := os.Create("bans.json")
if err != nil {
l.Println(err)
return
}
j := json.NewEncoder(f)
j.SetIndent("", " ")
err = j.Encode(bans)
if err != nil {
rooms["#main"].broadcast(devbot, "error saving bans: "+err.Error())
l.Println(err)
return
}
f.Close()
}
func readBans() {
f, err := os.Open("bans.json")
if err != nil && !os.IsNotExist(err) { // if there is an error and it is not a "file does not exist" error
l.Println(err)
return
}
err = json.NewDecoder(f).Decode(&bans)
if err != nil {
rooms["#main"].broadcast(devbot, "error reading bans: "+err.Error())
l.Println(err)
return
}
f.Close()
}
func findUserByName(r *room, name string) (*user, bool) {
r.usersMutex.Lock()
defer r.usersMutex.Unlock()
for _, u := range r.users {
if stripansi.Strip(u.name) == name {
return u, true
}
}
return nil, false
}
func remove(s []*user, a *user) []*user {
for j := range s {
if s[j] == a {
return append(s[:j], s[j+1:]...)
}
}
return s
}
func devbotChat(room *room, line string) {
if strings.Contains(line, "devbot") {
if strings.Contains(line, "how are you") || strings.Contains(line, "how you") {
devbotRespond(room, []string{"How are _you_",
"Good as always lol",
"Ah the usual, solving quantum gravity :smile:",
"Howdy?",
"Thinking about intergalactic cows",
"Could maths be different in other universes?",
""}, 99)
return
}
if strings.Contains(line, "thank") {
devbotRespond(room, []string{"you're welcome",
"no problem",
"yeah dw about it",
":smile:",
"no worries",
"you're welcome man!",
"lol"}, 93)
return
}
if strings.Contains(line, "good") || strings.Contains(line, "cool") || strings.Contains(line, "awesome") || strings.Contains(line, "amazing") {
devbotRespond(room, []string{"Thanks haha", ":sunglasses:", ":smile:", "lol", "haha", "Thanks lol", "yeeeeeeeee"}, 93)
return
}
if strings.Contains(line, "bad") || strings.Contains(line, "idiot") || strings.Contains(line, "stupid") {
devbotRespond(room, []string{"what an idiot, bullying a bot", ":(", ":angry:", ":anger:", ":cry:", "I'm in the middle of something okay", "shut up", "Run ./help, you need it."}, 60)
return
}
if strings.Contains(line, "shut up") {
devbotRespond(room, []string{"NO YOU", "You shut up", "what an idiot, bullying a bot"}, 90)
return
}
devbotRespond(room, []string{"Hi I'm devbot", "Hey", "HALLO :rocket:", "Yes?", "Devbot to the rescue!", ":wave:"}, 90)
}
if line == "./help" || line == "/help" || strings.Contains(line, "help me") {
devbotRespond(room, []string{"Run help to get help!",
"Looking for help?",
"See available commands with cmds or see help with help :star:"}, 100)
}
if line == "easter" {
devbotRespond(room, []string{"eggs?", "bunny?"}, 100)
}
if strings.Contains(line, "rm -rf") {
devbotRespond(room, []string{"rm -rf you", "I've heard rm -rf / can really free up some space!\n\n you should try it on your computer", "evil"}, 100)
return
}
if strings.Contains(line, "where") && strings.Contains(line, "repo") {
devbotRespond(room, []string{"The repo's at github.com/quackduck/devzat!", ":star: github.com/quackduck/devzat :star:", "# github.com/quackduck/devzat"}, 100)
}
if strings.Contains(line, "rocket") || strings.Contains(line, "spacex") || strings.Contains(line, "tesla") {
devbotRespond(room, []string{"Doge to the mooooon :rocket:",
"I should have bought ETH before it :rocket:ed to the :moon:",
":rocket:",
"I like rockets",
"SpaceX",
"Elon Musk OP"}, 80)
}
if strings.Contains(line, "elon") {
devbotRespond(room, []string{"When something is important enough, you do it even if the odds are not in your favor. - Elon",
"I do think there is a lot of potential if you have a compelling product - Elon",
"If you're trying to create a company, it's like baking a cake. You have to have all the ingredients in the right proportion. - Elon",
"Patience is a virtue, and I'm learning patience. It's a tough lesson. - Elon"}, 75)
}
if !strings.Contains(line, "start") && strings.Contains(line, "star") {
devbotRespond(room, []string{"Someone say :star:?",
"If you like Devzat, give it a star at github.com/quackduck/devzat!",
":star: github.com/quackduck/devzat", ":star:"}, 90)
}
if strings.Contains(line, "cool project") || strings.Contains(line, "this is cool") || strings.Contains(line, "this is so cool") {
devbotRespond(room, []string{"Thank you :slight_smile:!",
" If you like Devzat, do give it a star at github.com/quackduck/devzat!",
"Star Devzat here: github.com/quackduck/devzat"}, 90)
}
}
func devbotRespond(room *room, messages []string, chance int) {
if chance == 100 || chance > rand.Intn(100) {
go func() {
time.Sleep(time.Second / 2)
pick := messages[rand.Intn(len(messages))]
room.broadcast(devbot, pick)
}()
}
}
func shasum(s string) string {
h := sha256.Sum256([]byte(s))
return hex.EncodeToString(h[:])
}