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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue