Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
113fcbf2d1 | |||
dd20b73b76 | |||
5f72f58c74 |
@ -29,10 +29,6 @@ type Payload func(msg Message) (string, error)
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.jakeyoungdev.com/jake/bolt"
|
"code.jakeyoungdev.com/jake/bolt"
|
||||||
@ -42,7 +38,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
//bolt defaults the command indicator to '.' however that can be changed with the options
|
//bolt defaults the command indicator to '.' however that can be changed with the options
|
||||||
//Example: bolt.New(bolt.WithIndicator('!')) would support commands like !ping
|
//Example: bolt.New(bolt.WithIndicator('!')) would support commands like !ping
|
||||||
b := bolt.New()
|
b := bolt.New(bolt.WithLogLevel(bolt.LogLevelCmd))
|
||||||
|
|
||||||
b.AddCommands(
|
b.AddCommands(
|
||||||
// .ping can be run at any time by anyone and will respond with 'pong'
|
// .ping can be run at any time by anyone and will respond with 'pong'
|
||||||
|
22
bolt.go
22
bolt.go
@ -28,6 +28,7 @@ type bolt struct {
|
|||||||
*dg.Session //holds discordgo internals
|
*dg.Session //holds discordgo internals
|
||||||
commands map[string]Command //maps trigger phrase to command struct for fast lookup
|
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
|
indicator string //the indicator used to detect whether a message is a command
|
||||||
|
logLvl LogLevel //determines how much the bot logs
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bolt interface {
|
type Bolt interface {
|
||||||
@ -63,13 +64,14 @@ func New(opts ...Option) Bolt {
|
|||||||
b := &bolt{
|
b := &bolt{
|
||||||
Session: bot,
|
Session: bot,
|
||||||
commands: make(map[string]Command, 0),
|
commands: make(map[string]Command, 0),
|
||||||
|
logLvl: LogLevelAll,
|
||||||
}
|
}
|
||||||
//set default command indicator
|
//set default command indicator
|
||||||
b.indicator = "."
|
b.indicator = "."
|
||||||
|
|
||||||
//apply options to bolt
|
//apply options to bolt
|
||||||
for _, o := range opts {
|
for _, opt := range opts {
|
||||||
o(b)
|
opt(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
return b
|
return b
|
||||||
@ -130,11 +132,17 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
|||||||
msg.Content = "GIF/IMAGE"
|
msg.Content = "GIF/IMAGE"
|
||||||
}
|
}
|
||||||
|
|
||||||
//log message
|
if b.logLvl == LogLevelAll {
|
||||||
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
|
//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
|
//the bot will ignore it's own messages to prevent command loops
|
||||||
if msg.Author.ID == s.State.User.ID {
|
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
|
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
|
// 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 {
|
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, " ")
|
words := strings.Split(msg.Content, " ")
|
||||||
run, ok := b.commands[words[0][lg:]]
|
run, ok := b.commands[words[0][lg:]]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -180,6 +193,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
|
|||||||
//run command payload
|
//run command payload
|
||||||
res, err := run.Payload(Message{
|
res, err := run.Payload(Message{
|
||||||
Author: msg.Author.Username,
|
Author: msg.Author.Username,
|
||||||
|
ID: msg.Author.ID,
|
||||||
Words: words,
|
Words: words,
|
||||||
Content: msg.Content,
|
Content: msg.Content,
|
||||||
Channel: channel.Name,
|
Channel: channel.Name,
|
||||||
|
@ -17,6 +17,7 @@ type Payload func(msg Message) (string, error)
|
|||||||
// message information passed to payload function
|
// message information passed to payload function
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Author string //username of message author
|
Author string //username of message author
|
||||||
|
ID string //discord ID of message author
|
||||||
Words []string //words from message split on whitespace
|
Words []string //words from message split on whitespace
|
||||||
Content string //entire message content
|
Content string //entire message content
|
||||||
Channel string //message channel
|
Channel string //message channel
|
||||||
|
15
option.go
15
option.go
@ -2,9 +2,24 @@ package bolt
|
|||||||
|
|
||||||
type Option func(b *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
|
// sets the substring that must be present at the beginning of the message to indicate a command
|
||||||
func WithIndicator(i string) Option {
|
func WithIndicator(i string) Option {
|
||||||
return func(b *bolt) {
|
return func(b *bolt) {
|
||||||
b.indicator = i
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user