2021-10-28 15:13:39 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-28 17:44:19 +02:00
|
|
|
"fmt"
|
2021-10-28 15:13:39 +02:00
|
|
|
"time"
|
|
|
|
|
2021-10-28 17:44:19 +02:00
|
|
|
"github.com/lyx0/nourybot/cmd/bot"
|
2021-10-28 15:13:39 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-10-28 21:49:54 +02:00
|
|
|
// AddChannel adds a given channel name to the database.
|
2021-10-28 17:44:19 +02:00
|
|
|
func AddChannel(target, channelName string, nb *bot.Bot) {
|
2021-10-28 15:22:19 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-10-28 17:44:19 +02:00
|
|
|
collection := nb.MongoClient.Database("nourybot").Collection("channels")
|
2021-10-28 15:13:39 +02:00
|
|
|
|
2021-10-28 16:16:11 +02:00
|
|
|
// Channel
|
2021-10-28 17:20:24 +02:00
|
|
|
// {{"name": string}, {"connect": bool}}
|
2021-10-28 16:16:11 +02:00
|
|
|
chnl := Channel{channelName, true}
|
2021-10-28 15:13:39 +02:00
|
|
|
|
2021-10-28 17:53:58 +02:00
|
|
|
_, insertErr := collection.InsertOne(ctx, chnl)
|
2021-10-28 15:13:39 +02:00
|
|
|
if insertErr != nil {
|
2021-10-28 17:44:19 +02:00
|
|
|
nb.Send(target, fmt.Sprintf("Error trying to join %s", channelName))
|
2021-10-28 15:13:39 +02:00
|
|
|
log.Error(insertErr)
|
2021-10-28 17:44:19 +02:00
|
|
|
return
|
2021-10-28 15:13:39 +02:00
|
|
|
}
|
2021-10-29 15:32:13 +02:00
|
|
|
nb.TwitchClient.Join(channelName)
|
2021-10-28 17:53:58 +02:00
|
|
|
nb.Send(target, fmt.Sprintf("Joined %s", channelName))
|
2021-10-28 15:13:39 +02:00
|
|
|
}
|