adding Context
- converting messages and methods into one context per event
This commit is contained in:
29
bolt.go
29
bolt.go
@@ -33,8 +33,7 @@ type bolt struct {
|
||||
pool chan struct{}
|
||||
maxRoutines int
|
||||
msgHandlerf Payload
|
||||
admin bool
|
||||
tools AdminToolBox
|
||||
// admin bool
|
||||
}
|
||||
|
||||
type Bolt interface {
|
||||
@@ -64,12 +63,12 @@ func New(opts ...Option) (Bolt, error) {
|
||||
}
|
||||
|
||||
b := &bolt{
|
||||
Session: bot,
|
||||
commands: make(map[string]Command, 0),
|
||||
logLvl: LogLevelAll,
|
||||
indicator: DEFAULT_INDICATOR,
|
||||
wg: sync.WaitGroup{},
|
||||
admin: false,
|
||||
Session: bot,
|
||||
commands: make(map[string]Command, 0),
|
||||
logLvl: LogLevelAll,
|
||||
indicator: DEFAULT_INDICATOR,
|
||||
wg: sync.WaitGroup{},
|
||||
// admin: false,
|
||||
maxRoutines: DEFAULT_MAX_GOROUTINES,
|
||||
}
|
||||
|
||||
@@ -80,7 +79,7 @@ func New(opts ...Option) (Bolt, error) {
|
||||
|
||||
//options can change these fields so we must create post-opts
|
||||
b.pool = make(chan struct{}, b.maxRoutines)
|
||||
b.tools = NewToolbox(b)
|
||||
// b.tools = NewToolbox(b)
|
||||
|
||||
return b, nil
|
||||
}
|
||||
@@ -173,7 +172,7 @@ func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
|
||||
ChannelID: channel.ID,
|
||||
Server: server.Name,
|
||||
ServerID: server.ID,
|
||||
sesh: b,
|
||||
// sesh: b,
|
||||
}
|
||||
|
||||
w := strings.Fields(msg.Content)
|
||||
@@ -240,7 +239,10 @@ func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
|
||||
|
||||
func (b *bolt) handleMessage(event *Message) error {
|
||||
if b.msgHandlerf != nil {
|
||||
return b.msgHandlerf(event, b.tools)
|
||||
return b.msgHandlerf(&Context{
|
||||
Message: event,
|
||||
bolt: b,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -277,7 +279,10 @@ func (b *bolt) handleCommand(msg *Message, lg int) error {
|
||||
}
|
||||
}
|
||||
|
||||
err = run.Payload(msg, b.tools)
|
||||
err = run.Payload(&Context{
|
||||
Message: msg,
|
||||
bolt: b,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("encountered an error while handling command (%s): %e", msg.Words[0], err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user