mirror-nourybot/pkg/db/partchannel.go

39 lines
1 KiB
Go
Raw Normal View History

2021-10-28 16:20:27 +02:00
package db
import (
"context"
"fmt"
2021-10-28 16:20:27 +02:00
"time"
"github.com/lyx0/nourybot/cmd/bot"
2021-10-28 16:20:27 +02:00
log "github.com/sirupsen/logrus"
)
2021-10-28 21:49:54 +02:00
// PartChannel takes in a target channel and querys the database for
// its name. If the channel is found it deletes the channel and returns a
// success message. If the channel couldn't be found it will return an error.
func PartChannel(target, channelName string, nb *bot.Bot) {
2021-10-28 16:20:27 +02:00
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := nb.MongoClient.Database("nourybot").Collection("channels")
2021-10-28 16:20:27 +02:00
// Channel
2021-10-28 17:54:04 +02:00
// {{"name": string}, {"connect": bool}}
2021-10-28 16:20:27 +02:00
chnl := Channel{channelName, true}
res, insertErr := collection.DeleteOne(ctx, chnl)
2021-10-28 17:54:04 +02:00
// 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))
2021-10-28 16:20:27 +02:00
log.Error(insertErr)
return
2021-10-28 16:20:27 +02:00
}
2021-10-29 15:32:13 +02:00
nb.TwitchClient.Depart(channelName)
nb.Send(target, fmt.Sprintf("Parted %s", channelName))
2021-10-28 17:48:18 +02:00
// log.Info(res)
2021-10-28 16:20:27 +02:00
}