From 17e6b2160e11ec465502d7d20d26e9062c3b2e6c Mon Sep 17 00:00:00 2001 From: zekro Date: Tue, 16 Aug 2022 20:57:26 +0000 Subject: [PATCH] add example for guild scoping [#11] --- examples/guildscoped/commands/guild1.go | 47 +++++++++++++++++++++++++ examples/guildscoped/commands/guild2.go | 47 +++++++++++++++++++++++++ examples/guildscoped/main.go | 46 ++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 examples/guildscoped/commands/guild1.go create mode 100644 examples/guildscoped/commands/guild2.go create mode 100644 examples/guildscoped/main.go diff --git a/examples/guildscoped/commands/guild1.go b/examples/guildscoped/commands/guild1.go new file mode 100644 index 0000000..8d031c0 --- /dev/null +++ b/examples/guildscoped/commands/guild1.go @@ -0,0 +1,47 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken" +) + +type Guild1Command struct{} + +var ( + _ ken.SlashCommand = (*Guild1Command)(nil) + _ ken.GuildScopedCommand = (*Guild1Command)(nil) +) + +func (c *Guild1Command) Name() string { + return "guild1" +} + +func (c *Guild1Command) Guild() string { + return "362162947738566657" +} + +func (c *Guild1Command) Description() string { + return "Basic Test Command - guild1" +} + +func (c *Guild1Command) Version() string { + return "1.0.0" +} + +func (c *Guild1Command) Type() discordgo.ApplicationCommandType { + return discordgo.ChatApplicationCommand +} + +func (c *Guild1Command) Options() []*discordgo.ApplicationCommandOption { + return []*discordgo.ApplicationCommandOption{} +} + +func (c *Guild1Command) Run(ctx *ken.Ctx) (err error) { + err = ctx.Respond(&discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "This command is only available on guild 1.", + }, + }) + return +} diff --git a/examples/guildscoped/commands/guild2.go b/examples/guildscoped/commands/guild2.go new file mode 100644 index 0000000..47e9a7d --- /dev/null +++ b/examples/guildscoped/commands/guild2.go @@ -0,0 +1,47 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken" +) + +type Guild2Command struct{} + +var ( + _ ken.SlashCommand = (*Guild1Command)(nil) + _ ken.GuildScopedCommand = (*Guild2Command)(nil) +) + +func (c *Guild2Command) Name() string { + return "guild2" +} + +func (c *Guild2Command) Guild() string { + return "526196711962705925" +} + +func (c *Guild2Command) Description() string { + return "Basic Test Command - guild2" +} + +func (c *Guild2Command) Version() string { + return "1.0.0" +} + +func (c *Guild2Command) Type() discordgo.ApplicationCommandType { + return discordgo.ChatApplicationCommand +} + +func (c *Guild2Command) Options() []*discordgo.ApplicationCommandOption { + return []*discordgo.ApplicationCommandOption{} +} + +func (c *Guild2Command) Run(ctx *ken.Ctx) (err error) { + err = ctx.Respond(&discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "This command is only available on guild 2.", + }, + }) + return +} diff --git a/examples/guildscoped/main.go b/examples/guildscoped/main.go new file mode 100644 index 0000000..157264d --- /dev/null +++ b/examples/guildscoped/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "os" + "os/signal" + "syscall" + + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken" + "github.com/zekrotja/ken/examples/guildscoped/commands" + "github.com/zekrotja/ken/store" +) + +func must(err error) { + if err != nil { + panic(err) + } +} + +func main() { + token := os.Getenv("TOKEN") + + session, err := discordgo.New("Bot " + token) + if err != nil { + panic(err) + } + defer session.Close() + + k, err := ken.New(session, ken.Options{ + CommandStore: store.NewDefault(), + }) + must(err) + + must(k.RegisterCommands( + new(commands.Guild1Command), + new(commands.Guild2Command), + )) + + defer k.Unregister() + + must(session.Open()) + + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc +}