-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtodo.go
96 lines (75 loc) · 1.98 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"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) autoCompleteNames(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.AutocompleteInteractionData]) {
t.mu.Lock()
defer t.mu.Unlock()
if len(t.list) == 0 {
w.Autocomplete(corde.NewResp())
return
}
resp := corde.NewResp()
for k := range t.list {
resp.Choice(k, k)
}
w.Autocomplete(resp)
}
func (t *todo) addHandler(ctx context.Context, w corde.ResponseWriter, i *corde.Interaction[corde.SlashCommandInteractionData]) {
value, _ := i.Data.Options.String("value")
name, _ := i.Data.Options.String("name")
user, err := i.Data.OptionsUser("user")
if err != nil {
user = i.Member.User
}
t.mu.Lock()
defer t.mu.Unlock()
t.list[name] = todoItem{
user: user.ID,
value: value,
}
w.Respond(corde.NewResp().Contentf("Successfully added %s", name).Ephemeral())
}
func (t *todo) listHandler(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.SlashCommandInteractionData]) {
t.mu.Lock()
defer t.mu.Unlock()
if len(t.list) == 0 {
w.Respond(corde.NewResp().Content("no todos").Ephemeral())
return
}
i := 1
s := &strings.Builder{}
for k, v := range t.list {
s.WriteString(fmt.Sprintf("%d. %s: %s - %s\n", i, k, v.value, format.User(v.user)))
i++
}
w.Respond(corde.NewEmbed().
Title("Todo list").
Description(s.String()).
Color(0x69b00b),
)
}
func (t *todo) removeHandler(ctx context.Context, w corde.ResponseWriter, i *corde.Interaction[corde.SlashCommandInteractionData]) {
t.mu.Lock()
defer t.mu.Unlock()
name, _ := i.Data.Options.String("name")
if _, ok := t.list[name]; !ok {
w.Respond(corde.NewResp().Contentf("%s not found", name).Ephemeral())
return
}
delete(t.list, name)
w.Respond(corde.NewResp().Contentf("deleted todo %s", name).Ephemeral())
}