From f97ff2064f152e8e32c1d4c648ed9f0875004b7f Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 25 Jun 2025 18:48:27 -0400 Subject: [PATCH] adding log level option --- README.md | 2 +- bolt.go | 21 +++++++++++++++++---- option.go | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2b2adf1..8ec5051 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ import ( func main() { //bolt defaults the command indicator to '.' however that can be changed with the options //Example: bolt.New(bolt.WithIndicator('!')) would support commands like !ping - b := bolt.New() + b := bolt.New(bolt.WithLogLevel(bolt.LogLevelCmd)) b.AddCommands( // .ping can be run at any time by anyone and will respond with 'pong' diff --git a/bolt.go b/bolt.go index 152405c..2b633d5 100644 --- a/bolt.go +++ b/bolt.go @@ -28,6 +28,7 @@ type bolt struct { *dg.Session //holds discordgo internals commands map[string]Command //maps trigger phrase to command struct for fast lookup indicator string //the indicator used to detect whether a message is a command + logLvl LogLevel //determines how much the bot logs } type Bolt interface { @@ -63,13 +64,14 @@ func New(opts ...Option) Bolt { b := &bolt{ Session: bot, commands: make(map[string]Command, 0), + logLvl: LogLevelAll, } //set default command indicator b.indicator = "." //apply options to bolt - for _, o := range opts { - o(b) + for _, opt := range opts { + opt(b) } return b @@ -130,11 +132,17 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) { msg.Content = "GIF/IMAGE" } - //log message - log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content) + if b.logLvl == LogLevelAll { + //log message + log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content) + } //the bot will ignore it's own messages to prevent command loops if msg.Author.ID == s.State.User.ID { + if b.logLvl == LogLevelCmd { + //log commands + log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content) + } return } @@ -151,6 +159,11 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) { // parses command from message and handles timeout checks, role checks, and command execution. All command responses are sent back to Discord func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Guild, channel *dg.Channel, lg int) error { + if b.logLvl == LogLevelCmd { + //log commands + log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content) + } + words := strings.Split(msg.Content, " ") run, ok := b.commands[words[0][lg:]] if !ok { diff --git a/option.go b/option.go index b2c1979..9cac768 100644 --- a/option.go +++ b/option.go @@ -2,9 +2,24 @@ package bolt type Option func(b *bolt) +type LogLevel int + +const ( + LogLevelAll LogLevel = iota //logs all messages, and errors + LogLevelCmd LogLevel = iota //log only commands and responses, and errors + LogLevelErr LogLevel = iota //logs only errors +) + // sets the substring that must be present at the beginning of the message to indicate a command func WithIndicator(i string) Option { return func(b *bolt) { b.indicator = i } } + +// sets the log level to determine how much bolt logs +func WithLogLevel(lvl LogLevel) Option { + return func(b *bolt) { + b.logLvl = lvl + } +} -- 2.47.2