mirror-nourybot/pkg/db/connect.go

184 lines
3.6 KiB
Go
Raw Normal View History

2021-10-25 23:33:00 +02:00
package db
2021-10-27 15:39:21 +02:00
import (
"context"
"fmt"
"time"
2021-10-27 19:41:42 +02:00
"github.com/lyx0/nourybot/cmd/bot"
2021-10-27 15:39:21 +02:00
"github.com/lyx0/nourybot/pkg/config"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func Connect() *mongo.Client {
conf := config.LoadConfig()
client, err := mongo.NewClient(options.Client().ApplyURI(conf.MongoURI))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
2021-10-27 19:27:35 +02:00
// defer client.Disconnect(ctx)
2021-10-27 15:39:21 +02:00
databases, err := client.ListDatabaseNames(ctx, bson.M{})
if err != nil {
log.Fatal(err)
}
fmt.Println(databases)
return client
}
type channel struct {
Name string `bson:"name,omitempty"`
Connect bool `bson:"connect,omitempty"`
}
func InsertInitialData() {
// Interact with data
client := Connect()
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
/*
Get my collection instance
*/
collection := client.Database("nourybot").Collection("channels")
/*
Insert channel
*/
chnl := []interface{}{
2021-10-27 19:27:35 +02:00
bson.D{{Key: "name", Value: "nouryqt"}, {Key: "connect", Value: true}},
bson.D{{Key: "name", Value: "nourybot"}, {Key: "connect", Value: true}},
2021-10-27 15:39:21 +02:00
}
res, insertErr := collection.InsertMany(ctx, chnl)
if insertErr != nil {
log.Error(insertErr)
}
log.Info(res)
}
2021-10-27 19:41:42 +02:00
func ListChannels(nb *bot.Bot) {
2021-10-27 15:39:21 +02:00
client := Connect()
collection := client.Database("nourybot").Collection("channels")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
2021-10-27 19:27:35 +02:00
defer client.Disconnect(ctx)
2021-10-27 19:41:42 +02:00
cur, currErr := collection.Find(ctx, bson.D{})
2021-10-27 15:39:21 +02:00
if currErr != nil {
panic(currErr)
}
defer cur.Close(ctx)
var channels []channel
if err := cur.All(ctx, &channels); err != nil {
panic(err)
}
2021-10-27 19:27:35 +02:00
for _, ch := range channels {
2021-10-27 19:41:42 +02:00
nb.TwitchClient.Join(ch.Name)
nb.TwitchClient.Say(ch.Name, "xd")
// fmt.Printf("%v\n", ch.Name)
2021-10-27 19:27:35 +02:00
}
2021-10-27 15:39:21 +02:00
}
/*
List databases
*/
/*
Iterate a cursor
*/
// log.Info(channels)
2021-10-26 17:03:16 +02:00
// import (
// "context"
// "fmt"
// "time"
// "github.com/lyx0/nourybot/pkg/config"
// log "github.com/sirupsen/logrus"
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
// )
// func Connect() {
// conf := config.LoadConfig()
// client, err := mongo.NewClient(options.Client().ApplyURI(conf.MongoURI))
// if err != nil {
// log.Fatal(err)
// }
// ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
// err = client.Connect(ctx)
// if err != nil {
// log.Fatal(err)
// }
// defer client.Disconnect(ctx)
// /*
// List databases
// */
// databases, err := client.ListDatabaseNames(ctx, bson.M{})
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(databases)
// // Interact with data
// type Channel struct {
// Name string `bson:"name,omitempty"`
// }
// /*
// Get my collection instance
// */
// collection := client.Database("nourybot").Collection("channels")
// /*
// Insert channel
// */
// // chnl := []interface{}{
// // bson.D{{"name", "nouryqt"}},
// // bson.D{{"name", "nourybot"}},
// // }
// // res, insertErr := collection.InsertMany(ctx, chnl)
// // if insertErr != nil {
// // log.Fatal(insertErr)
// // }
// // log.Info(res)
// /*
// Iterate a cursor
// */
// cur, currErr := collection.Find(ctx, bson.D{})
// if currErr != nil {
// panic(currErr)
// }
// defer cur.Close(ctx)
// var channels []Channel
// if err = cur.All(ctx, &channels); err != nil {
// panic(err)
// }
// log.Info(channels)
// }