From 7a7c3680fdf0f9b6c5bf9f5166baa91b03eec9a0 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Thu, 28 Oct 2021 17:54:04 +0200 Subject: [PATCH] little bit of cleanings --- pkg/db/connect.go | 88 ------------------------------------------- pkg/db/initialjoin.go | 2 +- pkg/db/partchannel.go | 9 ++--- 3 files changed, 4 insertions(+), 95 deletions(-) diff --git a/pkg/db/connect.go b/pkg/db/connect.go index 1ca806f..5a72164 100644 --- a/pkg/db/connect.go +++ b/pkg/db/connect.go @@ -17,7 +17,6 @@ type Channel struct { } func Connect(cfg *config.Config) *mongo.Client { - client, err := mongo.NewClient(options.Client().ApplyURI(cfg.MongoURI)) if err != nil { log.Fatal(err) @@ -48,90 +47,3 @@ func Connect(cfg *config.Config) *mongo.Client { return client } - -/* - List databases -*/ - -/* - Iterate a cursor -*/ - -// log.Info(channels) - -// 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) - -// } diff --git a/pkg/db/initialjoin.go b/pkg/db/initialjoin.go index 7b176fe..34984fb 100644 --- a/pkg/db/initialjoin.go +++ b/pkg/db/initialjoin.go @@ -10,8 +10,8 @@ import ( ) func InitialJoin(nb *bot.Bot) { - collection := nb.MongoClient.Database("nourybot").Collection("channels") + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() diff --git a/pkg/db/partchannel.go b/pkg/db/partchannel.go index 0ab9e95..911c4c5 100644 --- a/pkg/db/partchannel.go +++ b/pkg/db/partchannel.go @@ -10,20 +10,18 @@ import ( ) func PartChannel(target, channelName string, nb *bot.Bot) { - // Interact with data - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - /* - Get my collection instance - */ collection := nb.MongoClient.Database("nourybot").Collection("channels") // Channel + // {{"name": string}, {"connect": bool}} chnl := Channel{channelName, true} res, insertErr := collection.DeleteOne(ctx, chnl) + + // res.DeletedCount is 0 if trying to delete something that wasn't there. if insertErr != nil || res.DeletedCount != 1 { nb.Send(target, fmt.Sprintf("Error trying to part %s", channelName)) log.Error(insertErr) @@ -32,5 +30,4 @@ func PartChannel(target, channelName string, nb *bot.Bot) { nb.Send(target, fmt.Sprintf("Parted %s", channelName)) // log.Info(res) - }