-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresolved.go
90 lines (80 loc) · 2.15 KB
/
resolved.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
package corde
import (
"fmt"
)
// OptionsUser returns the resolved User for an Option
func (i resolvedInteractionWithOptions) OptionsUser(k string) (User, error) {
var u User
s, err := i.Options.Snowflake(k)
if err != nil {
return u, err
}
u, ok := i.Resolved.Users[s]
if !ok {
return u, fmt.Errorf("no user found for option %q", k)
}
return u, nil
}
// OptionsMember returns the resolved Member (and User) for an Option
func (i resolvedInteractionWithOptions) OptionsMember(k string) (Member, error) {
var m Member
s, err := i.Options.Snowflake(k)
if err != nil {
return m, err
}
m, ok := i.Resolved.Members[s]
if !ok {
return m, fmt.Errorf("no member found for option %q", k)
}
m.User, err = i.OptionsUser(k)
if err != nil {
return m, err
}
return m, nil
}
// OptionsRole returns the resolved Role for an Option
func (i resolvedInteractionWithOptions) OptionsRole(k string) (Role, error) {
var r Role
s, err := i.Options.Snowflake(k)
if err != nil {
return r, err
}
r, ok := i.Resolved.Roles[s]
if !ok {
return r, fmt.Errorf("no role found for option %q", k)
}
return r, nil
}
// OptionsMessage returns the resolved Message for an Option
func (i resolvedInteractionWithOptions) OptionsMessage(k string) (Message, error) {
var m Message
s, err := i.Options.Snowflake(k)
if err != nil {
return m, err
}
m, ok := i.Resolved.Messages[s]
if !ok {
return m, fmt.Errorf("no member message for option %q", k)
}
return m, nil
}
type ResolvedDataConstraint interface {
User | Member | Role | Message
}
// ResolvedData is a generic mapping of Snowflakes to resolved data structs
type ResolvedData[T ResolvedDataConstraint] map[Snowflake]T
// First returns the first resolved data
// ResolvedData is a map (which is unordered), so First
// should only be used when ResolvedData has a single element.
func (r ResolvedData[T]) First() T {
for _, v := range r {
return v
}
return *new(T)
}
type Resolved struct {
Users ResolvedData[User] `json:"users,omitempty"`
Members ResolvedData[Member] `json:"members,omitempty"`
Roles ResolvedData[Role] `json:"roles,omitempty"`
Messages ResolvedData[Message] `json:"messages,omitempty"`
}