Skip to content

Commit

Permalink
make ken instance in ctx public
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Oct 23, 2021
1 parent ab0fe10 commit 65f2cbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
27 changes: 14 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
type Ctx struct {
ObjectMap

k *Ken
responded bool

// Ken keeps a reference to the main Ken instance.
Ken *Ken
// Session holds the discordgo session instance.
Session *discordgo.Session
// Event provides the InteractionCreate event
Expand All @@ -38,7 +39,7 @@ func (c *Ctx) Respond(r *discordgo.InteractionResponse) (err error) {
return
}
var self *discordgo.User
if self, err = c.k.opt.State.SelfUser(c.Session); err != nil {
if self, err = c.Ken.opt.State.SelfUser(c.Session); err != nil {
return
}
_, err = c.Session.InteractionResponseEdit(self.ID, c.Event.Interaction, &discordgo.WebhookEdit{
Expand All @@ -62,7 +63,7 @@ func (c *Ctx) Respond(r *discordgo.InteractionResponse) (err error) {
// embed payload as passed.
func (c *Ctx) RespondEmbed(emb *discordgo.MessageEmbed) (err error) {
if emb.Color <= 0 {
emb.Color = c.k.opt.EmbedColors.Default
emb.Color = c.Ken.opt.EmbedColors.Default
}
return c.Respond(&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Expand All @@ -81,7 +82,7 @@ func (c *Ctx) RespondError(content, title string) (err error) {
return c.RespondEmbed(&discordgo.MessageEmbed{
Description: content,
Title: title,
Color: c.k.opt.EmbedColors.Error,
Color: c.Ken.opt.EmbedColors.Error,
})
}

Expand All @@ -97,7 +98,7 @@ func (c *Ctx) FollowUp(wait bool, data *discordgo.WebhookParams) (fum *FollowUpM
s: c.Session,
i: c.Event.Interaction,
}
fum.self, fum.Error = c.k.opt.State.SelfUser(c.Session)
fum.self, fum.Error = c.Ken.opt.State.SelfUser(c.Session)
if fum.Error != nil {
return
}
Expand All @@ -109,7 +110,7 @@ func (c *Ctx) FollowUp(wait bool, data *discordgo.WebhookParams) (fum *FollowUpM
// embed payload as passed.
func (c *Ctx) FollowUpEmbed(emb *discordgo.MessageEmbed) (fum *FollowUpMessage) {
if emb.Color <= 0 {
emb.Color = c.k.opt.EmbedColors.Default
emb.Color = c.Ken.opt.EmbedColors.Default
}
return c.FollowUp(true, &discordgo.WebhookParams{
Embeds: []*discordgo.MessageEmbed{
Expand All @@ -125,7 +126,7 @@ func (c *Ctx) FollowUpError(content, title string) (fum *FollowUpMessage) {
return c.FollowUpEmbed(&discordgo.MessageEmbed{
Description: content,
Title: title,
Color: c.k.opt.EmbedColors.Error,
Color: c.Ken.opt.EmbedColors.Error,
})
}

Expand All @@ -146,22 +147,22 @@ func (c *Ctx) Defer() (err error) {
// dependency provider, if available. When no object was found in
// either of both maps, nil is returned.
func (c *Ctx) Get(key string) (v interface{}) {
if v = c.ObjectMap.Get(key); v == nil && c.k.opt.DependencyProvider != nil {
v = c.k.opt.DependencyProvider.Get(key)
if v = c.ObjectMap.Get(key); v == nil && c.Ken.opt.DependencyProvider != nil {
v = c.Ken.opt.DependencyProvider.Get(key)
}
return
}

// Channel tries to fetch the channel object from the contained
// channel ID using the specified state manager.
func (c *Ctx) Channel() (*discordgo.Channel, error) {
return c.k.opt.State.Channel(c.Session, c.Event.ChannelID)
return c.Ken.opt.State.Channel(c.Session, c.Event.ChannelID)
}

// Channel tries to fetch the guild object from the contained
// guild ID using the specified state manager.
func (c *Ctx) Guild() (*discordgo.Guild, error) {
return c.k.opt.State.Guild(c.Session, c.Event.GuildID)
return c.Ken.opt.State.Guild(c.Session, c.Event.GuildID)
}

// User returns the User object of the executor either from
Expand Down Expand Up @@ -224,11 +225,11 @@ func (c *Ctx) HandleSubCommands(handler ...SubCommandHandler) (err error) {
continue
}

ctx := c.k.subCtxPool.Get().(*SubCommandCtx)
ctx := c.Ken.subCtxPool.Get().(*SubCommandCtx)
ctx.Ctx = c
ctx.SubCommandName = h.Name
err = h.Run(ctx)
c.k.subCtxPool.Put(ctx)
c.Ken.subCtxPool.Put(ctx)
break
}
return
Expand Down
2 changes: 1 addition & 1 deletion ken.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (k *Ken) onInteractionCreate(s *discordgo.Session, e *discordgo.Interaction
defer k.ctxPool.Put(ctx)
ctx.Purge()
ctx.responded = false
ctx.k = k
ctx.Ken = k
ctx.Session = s
ctx.Event = e
ctx.Command = cmd
Expand Down
6 changes: 3 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (o *CommandOption) ChannelValue(ctx *Ctx) *discordgo.Channel {
return &discordgo.Channel{ID: chanID}
}

ch, err := ctx.k.opt.State.Channel(ctx.Session, chanID)
ch, err := ctx.Ken.opt.State.Channel(ctx.Session, chanID)
if err != nil {
return &discordgo.Channel{ID: chanID}
}
Expand All @@ -91,7 +91,7 @@ func (o *CommandOption) RoleValue(ctx *Ctx) *discordgo.Role {
return &discordgo.Role{ID: roleID}
}

role, err := ctx.k.opt.State.Role(ctx.Session, ctx.Event.GuildID, roleID)
role, err := ctx.Ken.opt.State.Role(ctx.Session, ctx.Event.GuildID, roleID)
if err != nil {
return &discordgo.Role{ID: roleID}
}
Expand All @@ -112,7 +112,7 @@ func (o *CommandOption) UserValue(ctx *Ctx) *discordgo.User {
return &discordgo.User{ID: userID}
}

user, err := ctx.k.opt.State.User(ctx.Session, userID)
user, err := ctx.Ken.opt.State.User(ctx.Session, userID)
if err != nil {
return &discordgo.User{ID: userID}
}
Expand Down

0 comments on commit 65f2cbe

Please sign in to comment.