From 07b1b39afd2034df146e5f01bc727243ba120f38 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Thu, 28 Oct 2021 15:13:39 +0200 Subject: [PATCH] restructure --- pkg/db/addchannel.go | 40 +++++++++++++++++++++++++++++++ pkg/db/connect.go | 55 ------------------------------------------- pkg/db/initialjoin.go | 36 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 pkg/db/addchannel.go create mode 100644 pkg/db/initialjoin.go diff --git a/pkg/db/addchannel.go b/pkg/db/addchannel.go new file mode 100644 index 0000000..2d8a1f5 --- /dev/null +++ b/pkg/db/addchannel.go @@ -0,0 +1,40 @@ +package db + +import ( + "context" + "time" + + log "github.com/sirupsen/logrus" + "go.mongodb.org/mongo-driver/bson" +) + +func AddChannel(channelName string) { + // 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{}{ + // bson.D{{Key: "name", Value: "nouryqt"}, {Key: "connect", Value: true}}, + // bson.D{{Key: "name", Value: "nourybot"}, {Key: "connect", Value: true}}, + // } + + chnl := []interface{}{ + bson.D{{Key: "name", Value: channelName}, {Key: "connect", Value: true}}, + } + + res, insertErr := collection.InsertOne(ctx, chnl) + if insertErr != nil { + log.Error(insertErr) + } + log.Info(res) + +} diff --git a/pkg/db/connect.go b/pkg/db/connect.go index 7eca189..3fe8b32 100644 --- a/pkg/db/connect.go +++ b/pkg/db/connect.go @@ -5,7 +5,6 @@ import ( "fmt" "time" - "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/config" log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" @@ -41,60 +40,6 @@ type channel struct { 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{}{ - bson.D{{Key: "name", Value: "nouryqt"}, {Key: "connect", Value: true}}, - bson.D{{Key: "name", Value: "nourybot"}, {Key: "connect", Value: true}}, - } - - res, insertErr := collection.InsertMany(ctx, chnl) - if insertErr != nil { - log.Error(insertErr) - } - log.Info(res) - -} - -func JoinChannels(nb *bot.Bot) { - client := Connect() - - collection := client.Database("nourybot").Collection("channels") - ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) - defer client.Disconnect(ctx) - - 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) - } - - for _, ch := range channels { - nb.TwitchClient.Join(ch.Name) - nb.TwitchClient.Say(ch.Name, "xd") - // fmt.Printf("%v\n", ch.Name) - } - -} - /* List databases */ diff --git a/pkg/db/initialjoin.go b/pkg/db/initialjoin.go new file mode 100644 index 0000000..21c2bea --- /dev/null +++ b/pkg/db/initialjoin.go @@ -0,0 +1,36 @@ +package db + +import ( + "context" + "time" + + "github.com/lyx0/nourybot/cmd/bot" + "go.mongodb.org/mongo-driver/bson" +) + +func InitialJoin(nb *bot.Bot) { + client := Connect() + + collection := client.Database("nourybot").Collection("channels") + ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) + defer client.Disconnect(ctx) + + 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) + } + + for _, ch := range channels { + nb.TwitchClient.Join(ch.Name) + nb.TwitchClient.Say(ch.Name, "xd") + // fmt.Printf("%v\n", ch.Name) + } + +}