diff --git a/pkg/db/addchannel.go b/pkg/db/addchannel.go index fd79b5d..7311adf 100644 --- a/pkg/db/addchannel.go +++ b/pkg/db/addchannel.go @@ -2,13 +2,14 @@ package db import ( "context" + "fmt" "time" + "github.com/lyx0/nourybot/cmd/bot" log "github.com/sirupsen/logrus" - "go.mongodb.org/mongo-driver/mongo" ) -func AddChannel(channelName string, mongoClient *mongo.Client) { +func AddChannel(target, channelName string, nb *bot.Bot) { // Interact with data ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) @@ -17,7 +18,7 @@ func AddChannel(channelName string, mongoClient *mongo.Client) { /* Get my collection instance */ - collection := mongoClient.Database("nourybot").Collection("channels") + collection := nb.MongoClient.Database("nourybot").Collection("channels") // Channel // {{"name": string}, {"connect": bool}} @@ -25,8 +26,11 @@ func AddChannel(channelName string, mongoClient *mongo.Client) { res, insertErr := collection.InsertOne(ctx, chnl) if insertErr != nil { + nb.Send(target, fmt.Sprintf("Error trying to join %s", channelName)) log.Error(insertErr) + return } + nb.Send(target, fmt.Sprintf("Joined %s", channelName)) log.Info(res) } diff --git a/pkg/db/partchannel.go b/pkg/db/partchannel.go index 4dce9dc..a399e6d 100644 --- a/pkg/db/partchannel.go +++ b/pkg/db/partchannel.go @@ -2,13 +2,14 @@ package db import ( "context" + "fmt" "time" + "github.com/lyx0/nourybot/cmd/bot" log "github.com/sirupsen/logrus" - "go.mongodb.org/mongo-driver/mongo" ) -func PartChannel(channelName string, mongoClient *mongo.Client) { +func PartChannel(target, channelName string, nb *bot.Bot) { // Interact with data ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) @@ -17,15 +18,19 @@ func PartChannel(channelName string, mongoClient *mongo.Client) { /* Get my collection instance */ - collection := mongoClient.Database("nourybot").Collection("channels") + collection := nb.MongoClient.Database("nourybot").Collection("channels") // Channel chnl := Channel{channelName, true} res, insertErr := collection.DeleteOne(ctx, chnl) - if insertErr != nil { + if insertErr != nil || res.DeletedCount != 1 { + nb.Send(target, fmt.Sprintf("Error trying to part %s", channelName)) log.Error(insertErr) + return } - log.Info(res) + nb.Send(target, fmt.Sprintf("Parted %s", channelName)) + + log.Info(res.DeletedCount) } diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index e01d5af..54d29f4 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -146,8 +146,7 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { nb.Send(target, "You are not allowed to do this") return } - db.AddChannel(cmdParams[1], nb.MongoClient) - nb.Send(message.Channel, "Joined") + db.AddChannel(target, cmdParams[1], nb) return case "part": @@ -155,8 +154,7 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { nb.Send(target, "You are not allowed to do this") return } - db.PartChannel(cmdParams[1], nb.MongoClient) - nb.Send(message.Channel, "Parted") + db.PartChannel(target, cmdParams[1], nb) return case "firstline":