|
|
@ -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) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|