Skip to content

Commit

Permalink
add guild scoping [#11]
Browse files Browse the repository at this point in the history
  • Loading branch information
zekro committed Aug 16, 2022
1 parent af5b3b9 commit 5f75d54
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ type Command interface {
Run(ctx *Ctx) (err error)
}

// GuildScopedCommand can be implemented by your
// commands to scope them to specific guilds.
//
// The command then will be only registered on
// the guild returned by the Guild method.
type GuildScopedCommand interface {
Guild() string
}

func toApplicationCommand(c Command) *discordgo.ApplicationCommand {
switch cm := c.(type) {
case UserCommand:
Expand Down
7 changes: 6 additions & 1 deletion ken.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,16 @@ func (k *Ken) onReady(s *discordgo.Session, e *discordgo.Ready) {
)

for name, cmd := range k.cmds {
guildId := ""
if gsc, ok := cmd.(GuildScopedCommand); ok {
guildId = gsc.Guild()
}

if _, ok := k.idcache[name]; ok {
acmd := toApplicationCommand(cmd)
update = append(update, acmd)
} else {
ccmd, err = s.ApplicationCommandCreate(e.User.ID, "", toApplicationCommand(cmd))
ccmd, err = s.ApplicationCommandCreate(e.User.ID, guildId, toApplicationCommand(cmd))
if err != nil {
k.opt.OnSystemError("command registration", err)
} else {
Expand Down

0 comments on commit 5f75d54

Please sign in to comment.