refactor mongo client

This commit is contained in:
lyx0 2021-10-28 17:20:24 +02:00
parent 4171d11dae
commit 10397e3adc
6 changed files with 13 additions and 11 deletions

View file

@ -4,10 +4,12 @@ import (
"time"
twitch "github.com/gempir/go-twitch-irc/v2"
"go.mongodb.org/mongo-driver/mongo"
)
type Bot struct {
TwitchClient *twitch.Client
MongoClient *mongo.Client
Uptime time.Time
}

View file

@ -21,6 +21,7 @@ func main() {
nb = &bot.Bot{
TwitchClient: twitch.NewClient(conf.Username, conf.Oauth),
MongoClient: db.Connect(),
Uptime: time.Now(),
}

View file

@ -5,22 +5,22 @@ import (
"time"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)
func AddChannel(channelName string) {
func AddChannel(channelName string, mongoClient *mongo.Client) {
// Interact with data
client := Connect()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
/*
Get my collection instance
*/
collection := client.Database("nourybot").Collection("channels")
collection := mongoClient.Database("nourybot").Collection("channels")
// Channel
// {{"name": string}, {"connect": bool}}
chnl := Channel{channelName, true}
res, insertErr := collection.InsertOne(ctx, chnl)

View file

@ -32,8 +32,8 @@ func InitialJoin(nb *bot.Bot) {
for _, ch := range channels {
nb.TwitchClient.Join(ch.Name)
log.Infof("Joined: %s\n", ch.Name)
nb.TwitchClient.Say(ch.Name, "xd")
log.Infof("Joined: %s\n", ch.Name)
}
}

View file

@ -5,20 +5,19 @@ import (
"time"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)
func PartChannel(channelName string) {
func PartChannel(channelName string, mongoClient *mongo.Client) {
// Interact with data
client := Connect()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
/*
Get my collection instance
*/
collection := client.Database("nourybot").Collection("channels")
collection := mongoClient.Database("nourybot").Collection("channels")
// Channel
chnl := Channel{channelName, true}

View file

@ -142,12 +142,12 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
}
case "join":
db.AddChannel(cmdParams[1])
db.AddChannel(cmdParams[1], nb.MongoClient)
nb.Send(message.Channel, "Joined")
return
case "part":
db.PartChannel(cmdParams[1])
db.PartChannel(cmdParams[1], nb.MongoClient)
nb.Send(message.Channel, "Parted")
case "firstline":