Merge pull request #177 from gempir/continous-joins

join continously to rejoin some unbanned channels
This commit is contained in:
gempir 2022-10-16 10:28:42 +02:00 committed by GitHub
commit 9c0966223b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,7 +28,14 @@ type Bot struct {
type worker struct {
client *twitch.Client
joinedChannels []string
joinedChannels map[string]bool
}
func newWorker(client *twitch.Client) *worker {
return &worker{
client: client,
joinedChannels: map[string]bool{},
}
}
// NewBot create new bot instance
@ -57,7 +64,7 @@ func (b *Bot) Say(channel, text string) {
func (b *Bot) Connect() {
b.startTime = time.Now()
client := b.newClient()
b.initialJoins()
go b.startJoinLoop()
if strings.HasPrefix(b.cfg.Username, "justinfan") {
log.Info("[bot] joining as anonymous user")
@ -68,6 +75,18 @@ func (b *Bot) Connect() {
log.Fatal(client.Connect())
}
// constantly join channels to rejoin some channels that got unbanned over time
func (b *Bot) startJoinLoop() {
for {
for _, channel := range b.channels {
b.Join(channel.Login)
}
time.Sleep(time.Hour * 1)
log.Info("[bot] running hourly join loop")
}
}
func (b *Bot) Part(channelNames ...string) {
for _, channelName := range channelNames {
log.Info("[bot] leaving " + channelName)
@ -80,13 +99,21 @@ func (b *Bot) Part(channelNames ...string) {
func (b *Bot) Join(channelNames ...string) {
for _, channel := range channelNames {
channel = strings.ToLower(channel)
joined := false
for _, worker := range b.worker {
if _, ok := worker.joinedChannels[channel]; ok {
// already joined but join again in case it was a temporary ban
b.Join(channel)
return
}
if len(worker.joinedChannels) < 50 {
log.Info("[bot] joining " + channel)
worker.client.Join(channel)
worker.joinedChannels = append(worker.joinedChannels, channel)
worker.joinedChannels[channel] = true
joined = true
break
}
@ -105,7 +132,7 @@ func (b *Bot) newClient() *twitch.Client {
client.SetJoinRateLimiter(twitch.CreateVerifiedRateLimiter())
}
b.worker = append(b.worker, &worker{client, []string{}})
b.worker = append(b.worker, newWorker(client))
log.Infof("[bot] creating new twitch connection, new total: %d", len(b.worker))
client.OnPrivateMessage(b.handlePrivateMessage)
@ -115,12 +142,6 @@ func (b *Bot) newClient() *twitch.Client {
return client
}
func (b *Bot) initialJoins() {
for _, channel := range b.channels {
b.Join(channel.Login)
}
}
func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) {
b.handlePrivateMessageCommands(message)