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