From 07b62886e30c8f4ae4c32c12b378603ade90fa75 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Tue, 29 Aug 2017 10:23:53 -0700 Subject: [PATCH] bot: do contexts better --- bot/bot.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index d6e6369..154bc39 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -150,17 +150,17 @@ func (cs *CommandSet) Add(h CommandHandler) error { return nil } -// Run makes a CommandSet compatible with discordgo event dispatching. -func (cs *CommandSet) Run(s *discordgo.Session, msg *discordgo.Message) error { +// Run executes a single command handler if applicable +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() defer cs.Unlock() - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := context.WithCancel(ctx) defer cancel() - sp, ctx := opentracing.StartSpanFromContext(ctx, "CommandSet.Run") - defer sp.Finish() - if strings.HasPrefix(msg.Content, cs.Prefix) { params := strings.Fields(msg.Content) 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] if !ok { + sp.LogFields(otlog.Error(ErrNoSuchCommand)) return ErrNoSuchCommand }