-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
126 lines (99 loc) · 2.87 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package bot
import (
discord "github.com/bwmarrin/discordgo"
)
type OptionsMap = map[string]*discord.ApplicationCommandInteractionDataOption
type Context struct {
*discord.Session
Caller *Command
Interaction *discord.Interaction
Options OptionsMap
handlers []Handler
}
func makeOptionMap(options []*discord.ApplicationCommandInteractionDataOption) (m OptionsMap) {
m = make(OptionsMap, len(options))
for _, option := range options {
m[option.Name] = option
}
return
}
func NewContext(s *discord.Session, caller *Command, i *discord.Interaction, parent *discord.ApplicationCommandInteractionDataOption, handlers []Handler) *Context {
options := i.ApplicationCommandData().Options
if parent != nil {
options = parent.Options
}
return &Context{
Session: s,
Caller: caller,
Interaction: i,
Options: makeOptionMap(options),
handlers: handlers,
}
}
func (ctx *Context) Respond(response *discord.InteractionResponse) error {
return ctx.Session.InteractionRespond(ctx.Interaction, response)
}
func (ctx *Context) Edit(content string) error {
_, err := ctx.Session.InteractionResponseEdit(ctx.Interaction, &discord.WebhookEdit{
Content: &content,
})
return err
}
func (ctx *Context) Response() (*discord.Message, error) {
return ctx.Session.InteractionResponse(ctx.Interaction)
}
func (ctx *Context) Next() {
if ctx.handlers == nil || len(ctx.handlers) == 0 {
return
}
handler := ctx.handlers[0]
ctx.handlers = ctx.handlers[1:]
handler.HandleCommand(ctx)
}
type MessageContext struct {
*discord.Session
Caller *Command
Message *discord.Message
handlers []MessageHandler
}
func NewMessageContext(s *discord.Session, caller *Command, m *discord.Message, handlers []MessageHandler) *MessageContext {
return &MessageContext{
Session: s,
Caller: caller,
Message: m,
handlers: handlers,
}
}
func (ctx *MessageContext) Reply(content string) (m *discord.Message, err error) {
m, err = ctx.Session.ChannelMessageSendReply(
ctx.Message.ChannelID,
content,
ctx.Message.Reference(),
)
return
}
func (ctx *MessageContext) EmbedReply(embed *discord.MessageEmbed) (m *discord.Message, err error) {
m, err = ctx.Session.ChannelMessageSendEmbedReply(
ctx.Message.ChannelID,
embed,
ctx.Message.Reference(),
)
return
}
func (ctx *MessageContext) AddReaction(emojiID string) error {
return ctx.Session.MessageReactionAdd(ctx.Message.ChannelID, ctx.Message.ID, emojiID)
}
func (ctx *MessageContext) RemoveReaction(emojiID string) error {
return ctx.Session.MessageReactionsRemoveEmoji(ctx.Message.ChannelID, ctx.Message.ID, emojiID)
}
func (ctx *MessageContext) ChannelTyping() error {
return ctx.Session.ChannelTyping(ctx.Message.ChannelID)
}
func (ctx *MessageContext) Next() {
if ctx.handlers == nil || len(ctx.handlers) == 0 {
return
}
handler := ctx.handlers[0]
ctx.handlers = ctx.handlers[1:]
handler.HandleMessageCommand(ctx)
}