Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/pen-fun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"os"

"github.com/Neon-Genesis-Linux/pen-bot/internal/community"
"github.com/Neon-Genesis-Linux/pen-bot/internal/config"
"github.com/Neon-Genesis-Linux/pen-bot/internal/core"
_ "github.com/Neon-Genesis-Linux/pen-bot/internal/logger"
"github.com/disgoorg/disgo/events"
)

func main() {
community.Register()
config.Register()
if err := core.Start(context.Background(), os.Getenv("BOT_TOKEN"), onMessageCreate); err != nil {
slog.Error("failed to start pen-fun bot", slog.Any("error", err))
}
Expand Down
6 changes: 3 additions & 3 deletions internal/community/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package community

import (
"github.com/Neon-Genesis-Linux/pen-bot/internal/core"
"github.com/disgoorg/disgo/discord"
"github.com/Neon-Genesis-Linux/pen-bot/internal/messaging"
"github.com/disgoorg/disgo/events"
)

Expand All @@ -13,9 +13,9 @@ func Register() {
}

func handlePing(event *events.MessageCreate) {
_, _ = event.Client().Rest.CreateMessage(event.ChannelID, discord.NewMessageCreate().WithContent("pong"))
_ = messaging.SendReply(event, "pong")
}

func handlePong(event *events.MessageCreate) {
_, _ = event.Client().Rest.CreateMessage(event.ChannelID, discord.NewMessageCreate().WithContent("ping"))
_ = messaging.SendReply(event, "ping")
}
46 changes: 46 additions & 0 deletions internal/config/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config

import (
"os"
"strings"

"github.com/Neon-Genesis-Linux/pen-bot/internal/core"
"github.com/Neon-Genesis-Linux/pen-bot/internal/messaging"
"github.com/disgoorg/disgo/events"
)

const ownerEnv = "BOT_OWNER_ID"

// Register registers configuration commands with the bot.
func Register() {
core.RegisterCommandPath([]string{"config", "prefix", "set"}, handlePrefixSet)
}

func handlePrefixSet(event *events.MessageCreate, args []string) {
if !isOwner(event) {
_ = messaging.SendReply(event, "Unauthorized: only bot owner can run config commands.")
return
}

if len(args) < 1 {
_ = messaging.SendReply(event, "Usage: "+core.GetBotPrefix()+"config prefix set <newprefix>")
return
}

newPrefix := strings.TrimSpace(args[0])
if newPrefix == "" {
_ = messaging.SendReply(event, "Prefix cannot be empty.")
return
}

core.SetBotPrefix(newPrefix)
_ = messaging.Send(event, "Command prefix set to `"+newPrefix+"`.")
}

func isOwner(event *events.MessageCreate) bool {
ownerID := os.Getenv(ownerEnv)
if ownerID == "" {
return false
}
return event.Message.Author.ID.String() == ownerID
}
107 changes: 107 additions & 0 deletions internal/core/cmdHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package core

import (
"strings"
"sync"

"github.com/disgoorg/disgo/events"
)

type MessageCommandHandler func(event *events.MessageCreate, args []string)

type commandNode struct {
handler MessageCommandHandler
children map[string]*commandNode
}

var (
rootCommand = &commandNode{children: make(map[string]*commandNode)}
registryMutex sync.RWMutex
botPrefix = "!" // configurable command prefix
)

// SetBotPrefix updates the command prefix at runtime.
func SetBotPrefix(prefix string) {
botPrefix = prefix
}

// GetBotPrefix returns the active command prefix.
func GetBotPrefix() string {
return botPrefix
}

// RegisterCommand registers a top-level command handler.
func RegisterCommand(command string, handler func(*events.MessageCreate)) {
RegisterCommandPath([]string{command}, func(event *events.MessageCreate, args []string) {
handler(event)
})
}

// RegisterCommandPath registers a handler for a path of nested command names.
func RegisterCommandPath(path []string, handler MessageCommandHandler) {
if len(path) == 0 || handler == nil {
return
}

registryMutex.Lock()
defer registryMutex.Unlock()

node := rootCommand
for _, segment := range path {
if node.children == nil {
node.children = make(map[string]*commandNode)
}
child, exists := node.children[segment]
if !exists {
child = &commandNode{children: make(map[string]*commandNode)}
node.children[segment] = child
}
node = child
}
node.handler = handler
}

// RegisterSubcommand registers a nested handler under the given parent path.
func RegisterSubcommand(parent []string, command string, handler MessageCommandHandler) {
path := append(append([]string(nil), parent...), command)
RegisterCommandPath(path, handler)
}

// DispatchCommand dispatches a prefix-based message command to the deepest registered handler.
func DispatchCommand(event *events.MessageCreate) {
if event.Message.Author.Bot {
return
}
content := strings.TrimSpace(event.Message.Content)
if !strings.HasPrefix(content, botPrefix) {
return
}
words := strings.Fields(strings.TrimPrefix(content, botPrefix))
if len(words) == 0 {
return
}

registryMutex.RLock()
defer registryMutex.RUnlock()

node := rootCommand
var lastHandler MessageCommandHandler
var lastMatch int
for i, word := range words {
child, exists := node.children[word]
if !exists {
break
}
node = child
if node.handler != nil {
lastHandler = node.handler
lastMatch = i + 1
}
}

if lastHandler != nil {
lastHandler(event, words[lastMatch:])
}
}

// Slash commands in Disgo are handled separately via application command registration and interaction event listeners.
43 changes: 0 additions & 43 deletions internal/core/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"log/slog"
"os"
"os/signal"
"strings"
"sync"
"syscall"

"github.com/disgoorg/disgo"
Expand All @@ -15,47 +13,6 @@ import (
"github.com/disgoorg/disgo/gateway"
)

var (
commandRegistry = make(map[string]func(*events.MessageCreate))
registryMutex sync.RWMutex
botPrefix = "!" // configurable command prefix
)

// SetBotPrefix sets the command prefix (default is "!")
func SetBotPrefix(prefix string) {
botPrefix = prefix
}

// RegisterCommand registers a command handler
func RegisterCommand(command string, handler func(*events.MessageCreate)) {
registryMutex.Lock()
defer registryMutex.Unlock()
commandRegistry[command] = handler
}

// DispatchCommand dispatches a message to the appropriate command handler
func DispatchCommand(event *events.MessageCreate) {
if event.Message.Author.Bot {
return
}
content := strings.TrimSpace(event.Message.Content)
if !strings.HasPrefix(content, botPrefix) {
return
}
parts := strings.Fields(content)
if len(parts) == 0 {
return
}
// Remove prefix from command to match registry key
command := strings.TrimPrefix(parts[0], botPrefix)
registryMutex.RLock()
handler, exists := commandRegistry[command]
registryMutex.RUnlock()
if exists {
handler(event)
}
}

func Start(ctx context.Context, token string, listener func(*events.MessageCreate)) error {
slog.Info("starting pen bot...")
slog.Info("disgo version", slog.String("version", disgo.Version))
Expand Down
51 changes: 51 additions & 0 deletions internal/messaging/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package messaging

import (
"log/slog"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
)

// SendOptions configures message sending behavior
type SendOptions struct {
Reply bool // If true, reply to the triggering message
}

// SendMessage sends a message with optional reply.
// Errors are logged but not returned to user.
// Returns error for caller's information if needed.
func SendMessage(event *events.MessageCreate, content string, opts *SendOptions) error {
if opts == nil {
opts = &SendOptions{}
}

builder := discord.NewMessageCreate().WithContent(content)

if opts.Reply {
messageID := event.Message.ID
builder = builder.WithMessageReference(&discord.MessageReference{
MessageID: &messageID,
})
}

_, err := event.Client().Rest.CreateMessage(event.ChannelID, builder)
if err != nil {
slog.Error("failed to send message",
slog.String("channel_id", event.ChannelID.String()),
slog.String("content", content),
slog.Any("error", err),
)
}
return err
}

// Send sends a simple message (no reply)
func Send(event *events.MessageCreate, content string) error {
return SendMessage(event, content, nil)
}

// SendReply sends a message as a reply to the user's message
func SendReply(event *events.MessageCreate, content string) error {
return SendMessage(event, content, &SendOptions{Reply: true})
}