/
todo.go
77 lines (62 loc) · 1.48 KB
/
todo.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
package main
import (
"fmt"
"strings"
"sync"
"github.com/Karitham/corde"
"github.com/Karitham/corde/format"
)
type todo struct {
mu sync.Mutex
list map[string]todoItem
}
type todoItem struct {
user corde.Snowflake
value string
}
func (t *todo) addHandler(w corde.ResponseWriter, i *corde.Interaction) {
value := i.Data.Options.String("value")
name := i.Data.Options.String("name")
user := i.Data.Options.Snowflake("user")
if user == 0 {
user = i.User.ID
}
t.mu.Lock()
defer t.mu.Unlock()
t.list[name] = todoItem{
user: user,
value: value,
}
w.Respond(corde.NewResp().Contentf("Sucessfully added %s", name).Ephemeral().B())
}
func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.Interaction) {
t.mu.Lock()
defer t.mu.Unlock()
if len(t.list) == 0 {
w.Respond(corde.NewResp().Content("no todos").Ephemeral().B())
return
}
w.Respond(corde.NewResp().
Embeds(corde.NewEmbed().
Title("Todo list").
// build todo list description
Description(func() string {
s, i := &strings.Builder{}, 1
for k, v := range t.list {
s.WriteString(fmt.Sprintf("%d. %s: %s - %s\n", i, k, v.value, format.User(v.user.String())))
i++
}
return s.String()
}()).
Color(0x69b00b).
B(),
).B(),
)
}
func (t *todo) removeHandler(w corde.ResponseWriter, i *corde.Interaction) {
t.mu.Lock()
defer t.mu.Unlock()
name := i.Data.Options.String("name")
delete(t.list, name)
w.Respond(corde.NewResp().Content("deleted todo").Ephemeral().B())
}