Add plugin support for commands
This commit is contained in:
parent
7b0c8351a8
commit
b252d5e62e
@ -62,3 +62,7 @@ func (loader *ComponentLoader) Notify(message string) error {
|
|||||||
loader.bot.Notify(message)
|
loader.bot.Notify(message)
|
||||||
return nil
|
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
24
common/chat_commands.go
Normal 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
|
||||||
|
}
|
@ -21,5 +21,8 @@ type ModuleManager interface {
|
|||||||
CreateEvent(event Event) error
|
CreateEvent(event Event) error
|
||||||
Notify(message string) error
|
Notify(message string) error
|
||||||
|
|
||||||
|
// Commands
|
||||||
|
RegisterCommand(string, ChatCommandConfiguration, func(User, map[string]any) string)
|
||||||
|
|
||||||
RegisterChatSyncModule(ID string, plugin ChatSyncModule) error
|
RegisterChatSyncModule(ID string, plugin ChatSyncModule) error
|
||||||
}
|
}
|
||||||
|
@ -7,29 +7,8 @@ import (
|
|||||||
"github.com/yeslayla/birdbot/common"
|
"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
|
// 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{
|
command := &discordgo.ApplicationCommand{
|
||||||
Name: name,
|
Name: name,
|
||||||
Description: config.Description,
|
Description: config.Description,
|
||||||
@ -60,13 +39,13 @@ func (discord *Discord) RegisterCommand(name string, config CommandConfiguration
|
|||||||
optionsMap := make(map[string]any, len(cmdOptions))
|
optionsMap := make(map[string]any, len(cmdOptions))
|
||||||
for _, opt := range cmdOptions {
|
for _, opt := range cmdOptions {
|
||||||
switch config.Options[opt.Name].Type {
|
switch config.Options[opt.Name].Type {
|
||||||
case CommandTypeString:
|
case common.CommandTypeString:
|
||||||
optionsMap[opt.Name] = opt.StringValue()
|
optionsMap[opt.Name] = opt.StringValue()
|
||||||
case CommandTypeInt:
|
case common.CommandTypeInt:
|
||||||
optionsMap[opt.Name] = opt.IntValue()
|
optionsMap[opt.Name] = opt.IntValue()
|
||||||
case CommandTypeBool:
|
case common.CommandTypeBool:
|
||||||
optionsMap[opt.Name] = opt.BoolValue()
|
optionsMap[opt.Name] = opt.BoolValue()
|
||||||
case CommandTypeFloat:
|
case common.CommandTypeFloat:
|
||||||
optionsMap[opt.Name] = opt.FloatValue()
|
optionsMap[opt.Name] = opt.FloatValue()
|
||||||
default:
|
default:
|
||||||
optionsMap[opt.Name] = opt.Value
|
optionsMap[opt.Name] = opt.Value
|
||||||
|
2
main.go
2
main.go
@ -89,7 +89,7 @@ func main() {
|
|||||||
|
|
||||||
SuccessMessage: cfg.Feedback.SuccessMessage,
|
SuccessMessage: cfg.Feedback.SuccessMessage,
|
||||||
FailureMessage: cfg.Feedback.FailureMessage,
|
FailureMessage: cfg.Feedback.FailureMessage,
|
||||||
}, bot.Session))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(PluginsDirectory); !os.IsNotExist(err) {
|
if _, err := os.Stat(PluginsDirectory); !os.IsNotExist(err) {
|
||||||
|
@ -9,11 +9,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/yeslayla/birdbot/common"
|
"github.com/yeslayla/birdbot/common"
|
||||||
"github.com/yeslayla/birdbot/discord"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type feedbackWebhookModule struct {
|
type feedbackWebhookModule struct {
|
||||||
session *discord.Discord
|
|
||||||
webhookURL string
|
webhookURL string
|
||||||
payloadType string
|
payloadType string
|
||||||
successMessage string
|
successMessage string
|
||||||
@ -27,9 +25,8 @@ type FeedbackWebhookConfiguration struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewFeedbackWebhookComponent creates a new component
|
// 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{
|
m := &feedbackWebhookModule{
|
||||||
session: session,
|
|
||||||
webhookURL: webhookURL,
|
webhookURL: webhookURL,
|
||||||
payloadType: "default",
|
payloadType: "default",
|
||||||
successMessage: "Feedback recieved!",
|
successMessage: "Feedback recieved!",
|
||||||
@ -50,13 +47,13 @@ func NewFeedbackWebhookComponent(webhookURL string, config FeedbackWebhookConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *feedbackWebhookModule) Initialize(birdbot common.ModuleManager) error {
|
func (c *feedbackWebhookModule) Initialize(birdbot common.ModuleManager) error {
|
||||||
c.session.RegisterCommand("feedback", discord.CommandConfiguration{
|
birdbot.RegisterCommand("feedback", common.ChatCommandConfiguration{
|
||||||
Description: "Sends a feedback message",
|
Description: "Sends a feedback message",
|
||||||
EphemeralResponse: true,
|
EphemeralResponse: true,
|
||||||
Options: map[string]discord.CommandOption{
|
Options: map[string]common.ChatCommandOption{
|
||||||
"message": {
|
"message": {
|
||||||
Description: "Content of what you'd like to communicate in your feedback.",
|
Description: "Content of what you'd like to communicate in your feedback.",
|
||||||
Type: discord.CommandTypeString,
|
Type: common.CommandTypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user