bot: do contexts better

This commit is contained in:
Cadey Ratio 2017-08-29 10:23:53 -07:00
parent 85d874b020
commit 07b62886e3
No known key found for this signature in database
GPG Key ID: D607EE27C2E7F89A
1 changed files with 7 additions and 6 deletions

View File

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