Skip to content

Commit

Permalink
add response policy
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Feb 17, 2022
1 parent 51a350d commit 1bce276
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ken.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ func (k *Ken) onInteractionCreate(s *discordgo.Session, e *discordgo.Interaction
ctx.Command = cmd
ctx.Ephemeral = false

if rpCmd, ok := cmd.(ResponsePolicyCommand); ok {
ctx.Ephemeral = rpCmd.ResponsePolicy().Ephemeral
}

if ch.Type == discordgo.ChannelTypeDM || ch.Type == discordgo.ChannelTypeGroupDM {
if dmCmd, ok := cmd.(DmCapable); !ok || !dmCmd.IsDmCapable() {
k.opt.OnCommandError(ErrNotDMCapable, ctx)
Expand Down
36 changes: 36 additions & 0 deletions responsepolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ken

// ResponsePolicy describes rules for context
// followups and responses.
type ResponsePolicy struct {
// When set to true, the command response will
// only be received by the sender of the command.
//
// This sets the `Ephemeral` flag of the `Context`
// to true before any middleware is invoked. So,
// you are able to modify the ephemeral flag either
// in your middleware or directly in your command
// logic, if you desire.
Ephemeral bool
}

// ResponsePolicyCommand defines a command which
// provides a ResponsePolicy.
type ResponsePolicyCommand interface {
ResponsePolicy() ResponsePolicy
}

// EphemeralCommand can be added to your command
// to make all command responses ephemeral.
// This means, that all responses to the command
// from the bot will only be received by the sender
// of the command.
type EphemeralCommand struct{}

var _ ResponsePolicyCommand = (*EphemeralCommand)(nil)

func (EphemeralCommand) ResponsePolicy() ResponsePolicy {
return ResponsePolicy{
Ephemeral: true,
}
}

0 comments on commit 1bce276

Please sign in to comment.