-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvocation.go
61 lines (51 loc) · 1.69 KB
/
invocation.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
package harmonia
import (
"github.com/bwmarrin/discordgo"
)
// An Invocation describes an incoming Interaction.
type Invocation struct {
*discordgo.Interaction
Guild *discordgo.Guild
Channel *discordgo.Channel
Author *Author
options []*discordgo.ApplicationCommandInteractionDataOption
// Only when the incoming Interaction is from a SelectMenu component.
Values []string
// Only when the incoming Interaction is from a UserCommand or MessageCommand.
targetID string
}
// GetOptionMap returns a map of options passed through the Invocation.
func (i *Invocation) GetOptionMap() map[string]*discordgo.ApplicationCommandInteractionDataOption {
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(i.options))
for _, opt := range i.options {
optionMap[opt.Name] = opt
}
return optionMap
}
// GetOption returns a specific option from an Invocation.
func (i *Invocation) GetOption(name string) *discordgo.ApplicationCommandInteractionDataOption {
option, ok := i.GetOptionMap()[name]
if !ok {
return nil
}
return option
}
// TargetAuthor takes the targetID from the invocation and returns an Author struct from it.
func (i *Invocation) TargetAuthor(h *Harmonia) (*Author, error) {
if i.Guild != nil {
member, err := h.GuildMember(i.Guild.ID, i.targetID)
if err != nil {
return nil, err
}
return AuthorFromMember(h, member)
}
user, err := h.User(i.targetID)
if err != nil {
return nil, err
}
return AuthorFromUser(user), nil
}
// TargetMessage takes the targetID from the invocation and returns an Message struct from it.
func (i *Invocation) TargetMessage(h *Harmonia) (*discordgo.Message, error) {
return h.ChannelMessage(i.ChannelID, i.targetID)
}