mirror-nourybot/pkg/db/initialjoin.go

47 lines
1.1 KiB
Go
Raw Normal View History

2021-10-28 15:13:39 +02:00
package db
import (
"context"
"fmt"
2021-10-28 15:13:39 +02:00
"time"
"github.com/lyx0/nourybot/cmd/bot"
2021-10-28 16:16:11 +02:00
log "github.com/sirupsen/logrus"
2021-10-28 15:13:39 +02:00
"go.mongodb.org/mongo-driver/bson"
)
2021-10-28 21:49:54 +02:00
// InitialJoin is called everytime the Bot starts and joins the
// initial list of twitch channels it should be in.
2021-10-28 15:13:39 +02:00
func InitialJoin(nb *bot.Bot) {
2021-10-28 17:48:18 +02:00
collection := nb.MongoClient.Database("nourybot").Collection("channels")
2021-10-28 17:54:04 +02:00
2021-10-28 15:22:19 +02:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// defer nb.MongoClient.Disconnect(ctx)
2021-10-28 15:13:39 +02:00
cur, currErr := collection.Find(ctx, bson.D{})
if currErr != nil {
panic(currErr)
}
defer cur.Close(ctx)
2021-10-28 16:16:11 +02:00
var channels []Channel
2021-10-28 15:13:39 +02:00
if err := cur.All(ctx, &channels); err != nil {
panic(err)
}
channelCount := 0
2021-10-28 15:13:39 +02:00
for _, ch := range channels {
nb.TwitchClient.Join(ch.Name)
2021-10-28 21:49:54 +02:00
// nb.TwitchClient.Say(ch.Name, "xd")
channelCount++
2021-10-28 17:20:24 +02:00
log.Infof("Joined: %s\n", ch.Name)
2021-10-28 15:13:39 +02:00
}
// It worked
nb.Send("nourybot", fmt.Sprintf("Badabing Badaboom Pepepains Joined %v channel", channelCount))
2021-10-29 00:13:08 +02:00
nb.Send("nouryqt", fmt.Sprintf("Badabing Badaboom Pepepains Joined %v channel", channelCount))
2021-10-28 15:13:39 +02:00
}