bot: do contexts better
This commit is contained in:
parent
85d874b020
commit
07b62886e3
13
bot/bot.go
13
bot/bot.go
|
@ -150,17 +150,17 @@ func (cs *CommandSet) Add(h CommandHandler) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run makes a CommandSet compatible with discordgo event dispatching.
|
// Run executes a single command handler if applicable
|
||||||
func (cs *CommandSet) Run(s *discordgo.Session, msg *discordgo.Message) error {
|
func (cs *CommandSet) Run(ctx context.Context, s *discordgo.Session, msg *discordgo.Message) error {
|
||||||
|
sp, ctx := opentracing.StartSpanFromContext(ctx, "bot.commandset.run")
|
||||||
|
defer sp.Finish()
|
||||||
|
|
||||||
cs.Lock()
|
cs.Lock()
|
||||||
defer cs.Unlock()
|
defer cs.Unlock()
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
sp, ctx := opentracing.StartSpanFromContext(ctx, "CommandSet.Run")
|
|
||||||
defer sp.Finish()
|
|
||||||
|
|
||||||
if strings.HasPrefix(msg.Content, cs.Prefix) {
|
if strings.HasPrefix(msg.Content, cs.Prefix) {
|
||||||
params := strings.Fields(msg.Content)
|
params := strings.Fields(msg.Content)
|
||||||
verb := strings.ToLower(params[0][1:])
|
verb := strings.ToLower(params[0][1:])
|
||||||
|
@ -175,6 +175,7 @@ func (cs *CommandSet) Run(s *discordgo.Session, msg *discordgo.Message) error {
|
||||||
|
|
||||||
cmd, ok := cs.cmds[verb]
|
cmd, ok := cs.cmds[verb]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
sp.LogFields(otlog.Error(ErrNoSuchCommand))
|
||||||
return ErrNoSuchCommand
|
return ErrNoSuchCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue