From de04d6069bbb4c77cd50db0d958763aa8e626645 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Mon, 20 Jul 2020 20:40:55 +0200 Subject: [PATCH] simply and hopefully fix channel leave/join --- internal/bot/bot.go | 59 +++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 083c2ed..02c12db 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -6,9 +6,8 @@ import ( "strings" "time" - "github.com/gissleh/irc" - "git.aiterp.net/rpdata/logbot3/internal/models" "git.aiterp.net/rpdata/logbot3/internal/models/channels" + "github.com/gissleh/irc" ) var botKey = "git.aiterp.net/rpdata/logbot.Bot.key" @@ -118,45 +117,37 @@ func (bot *Bot) runCommands(commands []string, target irc.Target, replacers map[ func (bot *Bot) loop() { bot.loopCtx, bot.loopCancel = context.WithCancel(bot.ctx) - channelChanges, err := channels.SubscribeLogged(bot.ctx) - if err != nil { - log.Println("Failed to get channel changes:", err) - return - } + ticker := time.NewTicker(time.Second * 10) + defer ticker.Stop() for { select { - case channel := <-channelChanges: + case <-ticker.C: { - channels := []models.Channel{channel} - deadline := time.After(time.Second * 1) - buffering := true - for buffering { - select { - case channel := <-channelChanges: - channels = append(channels, channel) - case <-deadline: - buffering = false - } + loggedChannels, err := channels.ListLogged(bot.ctx) + if err != nil { + log.Println("Failed to list logged channels:", err) + continue + } + loggedMap := make(map[string]bool) + for _, channel := range loggedChannels { + loggedMap[channel.Name] = true + } + clientMap := make(map[string]bool) + for _, channel := range bot.client.Channels() { + clientMap[channel.Name()] = true } - decisions := make(map[string]bool) - for _, channel := range channels { - decisions[channel.Name] = channel.Logged + joins := make([]string, 0, len(loggedMap)) + parts := make([]string, 0, len(clientMap)) + for key := range loggedMap { + if !clientMap[key] { + joins = append(joins, key) + } } - joins := make([]string, 0, len(decisions)) - parts := make([]string, 0, len(decisions)) - for channelName, logged := range decisions { - if logged { - if bot.client.Channel(channelName) != nil { - continue - } - joins = append(joins, channelName) - } else { - if bot.client.Channel(channelName) == nil { - continue - } - parts = append(parts, channelName) + for key := range clientMap { + if !loggedMap[key] { + parts = append(parts, key) } }