Add plugin support for commands

This commit is contained in:
Layla 2023-06-16 02:08:29 +00:00
parent 7b0c8351a8
commit b252d5e62e
6 changed files with 41 additions and 34 deletions

View File

@ -62,3 +62,7 @@ func (loader *ComponentLoader) Notify(message string) error {
loader.bot.Notify(message)
return nil
}
func (loader *ComponentLoader) RegisterCommand(name string, config common.ChatCommandConfiguration, handler func(common.User, map[string]any) string) {
loader.bot.Session.RegisterCommand(name, config, handler)
}

24
common/chat_commands.go Normal file
View File

@ -0,0 +1,24 @@
package common
import "github.com/bwmarrin/discordgo"
type CommandOptionType uint64
const (
CommandTypeString CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionString)
CommandTypeInt CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionInteger)
CommandTypeBool CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionBoolean)
CommandTypeFloat CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionNumber)
)
type ChatCommandConfiguration struct {
Description string
EphemeralResponse bool
Options map[string]ChatCommandOption
}
type ChatCommandOption struct {
Description string
Type CommandOptionType
Required bool
}

View File

@ -21,5 +21,8 @@ type ModuleManager interface {
CreateEvent(event Event) error
Notify(message string) error
// Commands
RegisterCommand(string, ChatCommandConfiguration, func(User, map[string]any) string)
RegisterChatSyncModule(ID string, plugin ChatSyncModule) error
}

View File

@ -7,29 +7,8 @@ import (
"github.com/yeslayla/birdbot/common"
)
type CommandConfiguration struct {
Description string
EphemeralResponse bool
Options map[string]CommandOption
}
type CommandOption struct {
Description string
Type CommandOptionType
Required bool
}
type CommandOptionType uint64
const (
CommandTypeString CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionString)
CommandTypeInt CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionInteger)
CommandTypeBool CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionBoolean)
CommandTypeFloat CommandOptionType = CommandOptionType(discordgo.ApplicationCommandOptionNumber)
)
// RegisterCommand creates an new command that can be used to interact with bird bot
func (discord *Discord) RegisterCommand(name string, config CommandConfiguration, handler func(common.User, map[string]any) string) {
func (discord *Discord) RegisterCommand(name string, config common.ChatCommandConfiguration, handler func(common.User, map[string]any) string) {
command := &discordgo.ApplicationCommand{
Name: name,
Description: config.Description,
@ -60,13 +39,13 @@ func (discord *Discord) RegisterCommand(name string, config CommandConfiguration
optionsMap := make(map[string]any, len(cmdOptions))
for _, opt := range cmdOptions {
switch config.Options[opt.Name].Type {
case CommandTypeString:
case common.CommandTypeString:
optionsMap[opt.Name] = opt.StringValue()
case CommandTypeInt:
case common.CommandTypeInt:
optionsMap[opt.Name] = opt.IntValue()
case CommandTypeBool:
case common.CommandTypeBool:
optionsMap[opt.Name] = opt.BoolValue()
case CommandTypeFloat:
case common.CommandTypeFloat:
optionsMap[opt.Name] = opt.FloatValue()
default:
optionsMap[opt.Name] = opt.Value

View File

@ -89,7 +89,7 @@ func main() {
SuccessMessage: cfg.Feedback.SuccessMessage,
FailureMessage: cfg.Feedback.FailureMessage,
}, bot.Session))
}))
}
if _, err := os.Stat(PluginsDirectory); !os.IsNotExist(err) {

View File

@ -9,11 +9,9 @@ import (
"net/http"
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/discord"
)
type feedbackWebhookModule struct {
session *discord.Discord
webhookURL string
payloadType string
successMessage string
@ -27,9 +25,8 @@ type FeedbackWebhookConfiguration struct {
}
// NewFeedbackWebhookComponent creates a new component
func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfiguration, session *discord.Discord) common.Module {
func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfiguration) common.Module {
m := &feedbackWebhookModule{
session: session,
webhookURL: webhookURL,
payloadType: "default",
successMessage: "Feedback recieved!",
@ -50,13 +47,13 @@ func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfig
}
func (c *feedbackWebhookModule) Initialize(birdbot common.ModuleManager) error {
c.session.RegisterCommand("feedback", discord.CommandConfiguration{
birdbot.RegisterCommand("feedback", common.ChatCommandConfiguration{
Description: "Sends a feedback message",
EphemeralResponse: true,
Options: map[string]discord.CommandOption{
Options: map[string]common.ChatCommandOption{
"message": {
Description: "Content of what you'd like to communicate in your feedback.",
Type: discord.CommandTypeString,
Type: common.CommandTypeString,
Required: true,
},
},