Skip to content

Commit

Permalink
Add prefix check for custom ids and command names
Browse files Browse the repository at this point in the history
Add go doc comments

Signed-off-by: Elias* <[email protected]>
  • Loading branch information
EliasStar committed Jun 6, 2023
1 parent 8439d10 commit 16b5745
Show file tree
Hide file tree
Showing 28 changed files with 479 additions and 113 deletions.
6 changes: 5 additions & 1 deletion internal/bacotell/bacotell.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package bacotell provides the BacoTell entrypoint and the core subsystems of the framework.
package bacotell

import (
Expand All @@ -7,19 +8,21 @@ import (
)

const (
Version = "v0.1.0"
Version = "v0.2.0"

ConfigBotName = "bot_name"
ConfigBotToken = "bot_token"
ConfigPluginDir = "plugin_dir"
)

// InitConfig sets default values for viper config entries.
func InitConfig() {
viper.SetDefault(ConfigBotName, "BacoTell")
viper.SetDefault(ConfigBotToken, "")
viper.SetDefault(ConfigPluginDir, "plugins")
}

// Run starts BacoTell.
func Run() {
initLoggers(hclog.Info)

Expand All @@ -33,6 +36,7 @@ func Run() {
unloadAll()
}

// Debug starts BacoTell in debug mode.
func Debug(token string, reattachConfig *plugin.ReattachConfig) {
initLoggers(hclog.Debug)

Expand Down
11 changes: 11 additions & 0 deletions internal/bacotell/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/viper"
)

// connect starts the Discord bot client and connects to the Discord API.
func connect() {
session, err := discordgo.New("Bot " + viper.GetString(ConfigBotToken))
if err != nil {
Expand Down Expand Up @@ -44,18 +45,25 @@ func connect() {
}
}

// _onConnect gets called when successfully connected to Discord.
func _onConnect(*discordgo.Session, *discordgo.Connect) {
discordLogger.Info("connected to discord gateway")
}

// _onDisconnect gets called when connection to Discord is lost.
func _onDisconnect(*discordgo.Session, *discordgo.Disconnect) {
discordLogger.Info("disconnected from discord gateway")
}

// _onReady gets called when the ready event is received from Discord.
func _onReady(_ *discordgo.Session, evt *discordgo.Ready) {
discordLogger.Info("logged in", "username", evt.User.Username, "tag", evt.User.Discriminator)
}

// _onGuildCreate gets called for every guild the bot is a member of.
//
// It registers all loaded commands to the guild which aren't already registered and updates the ones that are.
// It also deletes commands which are not loaded.
func _onGuildCreate(session *discordgo.Session, guild *discordgo.GuildCreate) {
guildLogger := discordLogger.With("guild", guild.ID)
guildLogger.Info("deploying commands")
Expand Down Expand Up @@ -115,6 +123,9 @@ func _onGuildCreate(session *discordgo.Session, guild *discordgo.GuildCreate) {
}
}

// _onInteractionCreate gets called when a user uses a command, component or modal.
//
// It looks up the requested command, component or modal and calls the respective handler function.
func _onInteractionCreate(session *discordgo.Session, evt *discordgo.InteractionCreate) {
interactionLogger := discordLogger.With("interaction", evt.ID)
interactionLogger.Info("handling interaction", "guild", evt.GuildID, "type", evt.Type)
Expand Down
28 changes: 28 additions & 0 deletions internal/bacotell/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/EliasStar/BacoTell/internal/codec"
common "github.com/EliasStar/BacoTell/pkg/bacotell_common"
Expand All @@ -21,6 +22,7 @@ var (
modals = make(map[string]common.Modal)
)

// HandshakeConfig returns the handshake configuration for the plugin.
func HandshakeConfig() plugin.HandshakeConfig {
return plugin.HandshakeConfig{
ProtocolVersion: 1,
Expand All @@ -29,10 +31,12 @@ func HandshakeConfig() plugin.HandshakeConfig {
}
}

// PluginMap returns the plugin map for the given plugin implementation (may be nil).
func PluginMap(pluginImpl common.Plugin) plugin.PluginSet {
return plugin.PluginSet{bacotellPlugin: codec.NewPlugin(pluginImpl)}
}

// loadAll loads all the plugins found in the plugin directory.
func loadAll() {
pluginDir := viper.GetString(ConfigPluginDir)
absPluginDir, err := filepath.Abs(pluginDir)
Expand Down Expand Up @@ -72,6 +76,7 @@ func loadAll() {
}
}

// loadSingle loads a single plugin using the provided reattach configuration.
func loadSingle(reattachConfig *plugin.ReattachConfig) {
loaderLogger.Debug("loading plugin")
_load(plugin.NewClient(&plugin.ClientConfig{
Expand All @@ -83,6 +88,7 @@ func loadSingle(reattachConfig *plugin.ReattachConfig) {
}))
}

// unloadAll unloads all the loaded plugins.
func unloadAll() {
for _, client := range clients {
client.Kill()
Expand All @@ -91,6 +97,7 @@ func unloadAll() {
clients = nil
}

// _load loads all commands, components, etc. from the provided plugin client.
func _load(client *plugin.Client) {
protocol, err := client.Client()
if err != nil {
Expand Down Expand Up @@ -120,6 +127,12 @@ func _load(client *plugin.Client) {
return
}

if _, ok := clients[id]; ok {
loaderLogger.Warn("plugin with same id is already loaded", "id", id)
client.Kill()
return
}

clients[id] = client
pluginLogger := loaderLogger.With("plugin", id)

Expand All @@ -137,6 +150,11 @@ func _load(client *plugin.Client) {
continue
}

if !strings.HasPrefix(data.Name, id) {
pluginLogger.Warn("command name must be prefixed with the plugin id", "command", data.Name)
continue
}

commands[data.Name] = cmd
}
}
Expand All @@ -155,6 +173,11 @@ func _load(client *plugin.Client) {
continue
}

if !strings.HasPrefix(cid, id) {
pluginLogger.Warn("custom id must be prefixed with the plugin id", "component", cid)
continue
}

components[cid] = cpt
}
}
Expand All @@ -173,6 +196,11 @@ func _load(client *plugin.Client) {
continue
}

if !strings.HasPrefix(cid, id) {
pluginLogger.Warn("custom id must be prefixed with the plugin id", "modal", cid)
continue
}

modals[cid] = mod
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/bacotell/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
discordLogger hclog.Logger
)

// initLoggers initializes all (sub)loggers for the major subsystems
func initLoggers(level hclog.Level) {
logger = hclog.New(&hclog.LoggerOptions{
Name: "bacotell",
Expand Down
Loading

0 comments on commit 16b5745

Please sign in to comment.