adding log level option (#1)

Reviewed-on: #1
Co-authored-by: jake <jake.young.dev@gmail.com>
Co-committed-by: jake <jake.young.dev@gmail.com>
This commit is contained in:
2025-06-25 22:51:32 +00:00
committed by jake
parent 5f72f58c74
commit dd20b73b76
3 changed files with 33 additions and 5 deletions

21
bolt.go
View File

@@ -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 {