mirror-nourybot/pkg/db/initialjoin.go

41 lines
855 B
Go
Raw Normal View History

2021-10-28 15:13:39 +02:00
package db
import (
"context"
"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)
}
for _, ch := range channels {
nb.TwitchClient.Join(ch.Name)
2021-10-28 21:49:54 +02:00
// nb.TwitchClient.Say(ch.Name, "xd")
2021-10-28 17:20:24 +02:00
log.Infof("Joined: %s\n", ch.Name)
2021-10-28 15:13:39 +02:00
}
}