Skip to content

Commit cbc3d7c

Browse files
committed
clean up errChannel handling for custom commands
1 parent f1401ea commit cbc3d7c

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

customcommands/bot.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,21 @@ type TriggeredCC struct {
577577
Args []string
578578
}
579579

580+
func getErrorChannel(cmd *models.CustomCommand, tmplCtx *templates.Context) int64 {
581+
cmdGroup := cmd.GetGroup()
582+
errChannel := tmplCtx.CurrentFrame.CS.ID
583+
if cmd.RedirectErrorsChannel != 0 {
584+
errChannel = cmd.RedirectErrorsChannel
585+
} else if cmdGroup != nil && cmdGroup.RedirectErrorsChannel != 0 {
586+
errChannel = cmdGroup.RedirectErrorsChannel
587+
}
588+
channel := tmplCtx.GS.GetChannel(errChannel)
589+
if channel == nil {
590+
return 0
591+
}
592+
return channel.ID
593+
}
594+
580595
func ExecuteCustomCommand(cmd *models.CustomCommand, tmplCtx *templates.Context) error {
581596
defer func() {
582597
if err := recover(); err != nil {
@@ -602,6 +617,7 @@ func ExecuteCustomCommand(cmd *models.CustomCommand, tmplCtx *templates.Context)
602617
"trigger_type": CommandTriggerType(cmd.TriggerType).String(),
603618
"guild": csCop.GuildID,
604619
"channel_name": csCop.Name,
620+
"cc_id": cmd.LocalID,
605621
})
606622

607623
// do not allow concurrent executions of the same custom command, to prevent most common kinds of abuse
@@ -610,16 +626,11 @@ func ExecuteCustomCommand(cmd *models.CustomCommand, tmplCtx *templates.Context)
610626
CCID: cmd.LocalID,
611627
}
612628
lockHandle := CCExecLock.Lock(lockKey, time.Minute, time.Minute*10)
629+
613630
if lockHandle == -1 {
614631
f.Warn("Exceeded max lock attempts for cc")
615-
errChannel := tmplCtx.CurrentFrame.CS.ID
616-
if cmd.RedirectErrorsChannel != 0 {
617-
errChannel = cmd.RedirectErrorsChannel
618-
} else if cmd.R.Group != nil && cmd.R.Group.RedirectErrorsChannel != 0 {
619-
errChannel = cmd.R.Group.RedirectErrorsChannel
620-
}
621-
622-
if cmd.ShowErrors {
632+
errChannel := getErrorChannel(cmd, tmplCtx)
633+
if cmd.ShowErrors && errChannel != 0 {
623634
common.BotSession.ChannelMessageSend(errChannel, fmt.Sprintf("Gave up trying to execute custom command #%d after 1 minute because there is already one or more instances of it being executed.", cmd.LocalID))
624635
}
625636
updatePostCommandRan(cmd, errors.New("Gave up trying to execute, already an existing instance executing"))
@@ -649,14 +660,8 @@ func ExecuteCustomCommand(cmd *models.CustomCommand, tmplCtx *templates.Context)
649660
if err != nil {
650661
logger.WithField("guild", tmplCtx.GS.ID).WithError(err).Error("Error executing custom command")
651662

652-
errChannel := tmplCtx.CurrentFrame.CS.ID
653-
if cmd.RedirectErrorsChannel != 0 {
654-
errChannel = cmd.RedirectErrorsChannel
655-
} else if cmd.R.Group != nil && cmd.R.Group.RedirectErrorsChannel != 0 {
656-
errChannel = cmd.R.Group.RedirectErrorsChannel
657-
}
658-
659-
if cmd.ShowErrors {
663+
errChannel := getErrorChannel(cmd, tmplCtx)
664+
if cmd.ShowErrors && errChannel != 0 {
660665
out += "\nAn error caused the execution of the custom command template to stop:\n"
661666
out += formatCustomCommandRunErr(chanMsg, err)
662667

@@ -814,14 +819,8 @@ func onExecPanic(cmd *models.CustomCommand, err error, tmplCtx *templates.Contex
814819

815820
l.Error("Error executing custom command")
816821

817-
errChannel := tmplCtx.CurrentFrame.CS.ID
818-
if cmd.RedirectErrorsChannel != 0 {
819-
errChannel = cmd.RedirectErrorsChannel
820-
} else if cmd.R.Group != nil && cmd.R.Group.RedirectErrorsChannel != 0 {
821-
errChannel = cmd.R.Group.RedirectErrorsChannel
822-
}
823-
824-
if cmd.ShowErrors {
822+
errChannel := getErrorChannel(cmd, tmplCtx)
823+
if cmd.ShowErrors && errChannel != 0 {
825824
out := "\nAn error caused the execution of the custom command template to stop:\n"
826825
out += "`" + err.Error() + "`"
827826

0 commit comments

Comments
 (0)