-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoe.go
211 lines (187 loc) · 4.36 KB
/
joe.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
package main
import (
"fmt"
"math/rand"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/go-joe/file-memory"
"github.com/go-joe/joe"
"github.com/go-joe/slack-adapter"
"github.com/pkg/errors"
)
type walbot struct {
*joe.Bot
}
func main() {
rand.Seed(time.Now().UnixNano())
slackToken := os.Getenv("SLACK_TOKEN")
var j *joe.Bot
if slackToken != "" {
j = joe.New("walbot",
slack.Adapter(slackToken, slack.WithListenPassive()),
file.Memory("walbot.json"))
} else {
j = joe.New("walbot",
file.Memory("walbot.json"))
}
b := &walbot{Bot: j}
thinges := []string{}
ok, err := b.Store.Get("thinges", &thinges)
if err != nil {
panic(err)
}
err = b.Store.Set("thinges", nil)
if err != nil {
panic(err)
}
if ok {
for _, thinge := range thinges {
b.makeThinge(thinge)
}
}
go setupDongerlist(b)
b.lispBang(`list-thinges`, func(msg joe.Message) error {
thinges := []string{}
ok, err := b.Store.Get("thinges", &thinges)
if err != nil {
return err
}
if !ok {
msg.Respond("no thinges defined yet... wat!")
return nil
}
sort.Strings(thinges)
msg.Respond(fmt.Sprintf("known thinges:\n%s", strings.Join(thinges, "\n")))
return nil
})
b.lispBang(`magic8ball .*\?`, randomizer(magic8ball))
b.lispBang(`make-thinge (.+)`, b.MakeThinge)
b.lispBang(`overlord`, randomizer(overlord))
b.lispBang(`ferengi`, randomizer(ferengi))
b.lispBang(`rand ([0-9]+)(?: ([0-9]+))?`, roll)
b.lispBang(`roll ([0-9]+)(?: ([0-9]+))?`, roll)
err = b.Run()
if err != nil {
b.Logger.Fatal(err.Error())
}
}
func roll(msg joe.Message) error {
max, err := strconv.Atoi(msg.Matches[0])
if err != nil {
msg.Respond("could not parse stop value")
return err
}
if max < 1 {
msg.Respond("max must be greater than 0")
return nil
}
count := 1
if msg.Matches[1] != "" {
count, err = strconv.Atoi(msg.Matches[1])
if err != nil {
msg.Respond("could not parse count value")
return err
}
}
for i := 0; i < count; i++ {
msg.Respond("%d", rand.Intn(max)+1)
}
return nil
}
func randomizer(items []string) func(joe.Message) error {
n := int64(len(items))
return func(msg joe.Message) error {
msg.Respond("%s", items[rand.Int63n(n)])
return nil
}
}
var errThingeExists = fmt.Errorf("thinge already exists")
func (b *walbot) MakeThinge(msg joe.Message) error {
thinge := msg.Matches[0]
resp, err := b.makeThinge(thinge)
if errors.Is(err, errThingeExists) {
msg.Respond(fmt.Sprintf("thinge %q already exists", thinge))
return nil
}
msg.Respond(resp)
return err
}
func (b *walbot) makeThinge(t string) (string, error) {
thinges := []string{}
_, err := b.Store.Get("thinges", &thinges)
if err != nil {
return "", errors.New("error getting thinges")
}
for _, v := range thinges {
if v == t {
return "", errThingeExists
}
}
thinges = append(thinges, t)
b.Store.Set("thinges", thinges)
b.lispBang(t+`-add \s*(.+)\s*`, func(msg joe.Message) error {
thinge := []string{}
_, err := b.Store.Get("thinge."+t, &thinge)
if err != nil {
return errors.New("error getting thinges")
}
thinge = append(thinge, msg.Matches[0])
err = b.Store.Set("thinge."+t, thinge)
if err != nil {
return errors.New("error saving thinge")
}
msg.Respond("%s added", t)
return nil
})
b.lispBang(t+`-del \s*(.+)\s*`, func(msg joe.Message) error {
thinge := []string{}
_, err := b.Store.Get("thinge."+t, &thinge)
if err != nil {
return errors.New("error getting thinges")
}
found := false
i := 0
v := ""
for i, v = range thinge {
if v == msg.Matches[0] {
found = true
break
}
}
if found {
thinge[i] = thinge[len(thinge)-1]
thinge[len(thinge)-1] = ""
thinge = thinge[:len(thinge)-1]
err := b.Store.Set("thinge."+t, thinge)
if err != nil {
return errors.New("error saving thinge")
}
msg.Respond("%s removed", t)
} else {
msg.Respond("%s not found", t)
}
return nil
})
b.lispBang(t, func(msg joe.Message) error {
thinge := []string{}
_, err := b.Store.Get("thinge."+t, &thinge)
if err != nil {
return errors.New("error getting thinges")
}
n := int64(len(thinge))
if n == 0 {
msg.Respond("awww 💩, no %ss saved yet", t)
return nil
}
msg.Respond("%s", thinge[rand.Int63n(n)])
return nil
})
return "thinge created", nil
}
func (b *walbot) lispBang(pattern string, fn func(joe.Message) error) {
b.Respond(`!`+pattern, fn)
b.Respond(`\(`+pattern+`\)`, fn)
}