-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
100 lines (79 loc) · 2.72 KB
/
context.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
package discog
import (
"errors"
"github.com/bwmarrin/discordgo"
)
// Represents callback ctx for Slash Command Handlers
type Ctx struct {
Session *discordgo.Session // Client Session
Interaction *discordgo.InteractionCreate // Interaction Create struct
interactionResponse *discordgo.InteractionResponse
}
// Creates a new Ctx for the given Discord session and interaction.
func newCtx(s *discordgo.Session, i *discordgo.InteractionCreate) *Ctx {
interactionResponse := &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{},
}
return &Ctx{Session: s, Interaction: i, interactionResponse: interactionResponse}
}
// Sends the response to Discord.
func (c *Ctx) Send() error {
return c.Session.InteractionRespond(c.Interaction.Interaction, c.interactionResponse)
}
// Sets the title for the response.
func (c *Ctx) SetTitle(title string) *Ctx {
c.interactionResponse.Data.Title = title
return c
}
// Sets the Interaction response type .
func (c *Ctx) SetResponseType(responseType discordgo.InteractionResponseType) *Ctx {
c.interactionResponse.Type = responseType
return c
}
// Adds a customId to the interaction response
func (c *Ctx) SetCustomID(id string) *Ctx {
c.interactionResponse.Data.CustomID = id
return c
}
// Sets the content of the interaction response.
func (c *Ctx) SetContent(content string) *Ctx {
c.interactionResponse.Data.Content = content
return c
}
// Sets the Text-To-Speech as true
func (c *Ctx) SetTTS() *Ctx {
c.interactionResponse.Data.TTS = true
return c
}
// Adds Files to the interaction response.
func (c *Ctx) SetFiles(files []*discordgo.File) *Ctx {
c.interactionResponse.Data.Files = append(c.interactionResponse.Data.Files, files...)
return c
}
// Flags the interaction response as ephemeral.
func (c *Ctx) FlagEphemeral() *Ctx {
c.interactionResponse.Data.Flags = discordgo.MessageFlagsEphemeral
return c
}
// Adds message flags to the response.
func (c *Ctx) FlagReply(flag discordgo.MessageFlags) *Ctx {
c.interactionResponse.Data.Flags = flag
return c
}
// Adds the components to the interaction response.
func (c *Ctx) SetComponents(actionRows []Component) (*Ctx, error) {
c.interactionResponse.Data.Components = []discordgo.MessageComponent{}
for _, row := range actionRows {
messageComponent, ok := row.GetComponent().(discordgo.ActionsRow)
if !ok {
return nil, errors.New("Must be ActionRow")
}
c.interactionResponse.Data.Components = append(c.interactionResponse.Data.Components, messageComponent)
}
return c, nil
}
// Sets message embeds to the interaction response.
func (c *Ctx) SetMessageEmbeds(embeds []*discordgo.MessageEmbed) {
c.interactionResponse.Data.Embeds = embeds
}