[core] Fix: send-message function passed to plugin was nil

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-12-10 02:28:13 +01:00
parent 8c32889584
commit 7e2b83fc0a
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
2 changed files with 17 additions and 6 deletions

View File

@ -13,7 +13,6 @@ import (
"sync"
"time"
"github.com/go-irc/irc"
"github.com/gofrs/uuid/v3"
"github.com/gorilla/mux"
"github.com/pkg/errors"
@ -55,8 +54,6 @@ var (
runID = uuid.Must(uuid.NewV4()).String()
externalHTTPAvailable bool
sendMessage func(m *irc.Message) error
store = newStorageFile(false)
twitchClient *twitch.Client
twitchEventSubClient *twitch.EventSubClient
@ -249,7 +246,6 @@ func main() {
case <-ircDisconnected:
if ircHdl != nil {
sendMessage = nil
ircHdl.Close()
}
@ -258,11 +254,9 @@ func main() {
}
go func() {
sendMessage = ircHdl.SendMessage
if err := ircHdl.Run(); err != nil {
log.WithError(err).Error("IRC run exited unexpectedly")
}
sendMessage = nil
time.Sleep(ircReconnectDelay)
ircDisconnected <- struct{}{}
}()

View File

@ -4,9 +4,11 @@ import (
"fmt"
"net/http"
"github.com/go-irc/irc"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/go_helpers/v2/backoff"
"github.com/Luzifer/go_helpers/v2/str"
"github.com/Luzifer/twitch-bot/internal/actors/ban"
"github.com/Luzifer/twitch-bot/internal/actors/delay"
@ -25,6 +27,8 @@ import (
"github.com/Luzifer/twitch-bot/twitch"
)
const ircHandleWaitRetries = 10
var (
corePluginRegistrations = []plugins.RegisterFunc{
// Actors
@ -105,3 +109,16 @@ func getRegistrationArguments() plugins.RegistrationArguments {
SendMessage: sendMessage,
}
}
func sendMessage(m *irc.Message) error {
if err := backoff.NewBackoff().WithMaxIterations(ircHandleWaitRetries).Retry(func() error {
if ircHdl == nil {
return errors.New("irc handle not available")
}
return nil
}); err != nil {
return errors.Wrap(err, "waiting for IRC connection")
}
return ircHdl.SendMessage(m)
}