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" "time"
twitch "github.com/gempir/go-twitch-irc/v2" twitch "github.com/gempir/go-twitch-irc/v2"
"go.mongodb.org/mongo-driver/mongo"
) )
type Bot struct { type Bot struct {
TwitchClient *twitch.Client TwitchClient *twitch.Client
MongoClient *mongo.Client
Uptime time.Time Uptime time.Time
} }

View file

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

View file

@ -5,22 +5,22 @@ import (
"time" "time"
log "github.com/sirupsen/logrus" 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 // Interact with data
client := Connect()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
/* /*
Get my collection instance Get my collection instance
*/ */
collection := client.Database("nourybot").Collection("channels") collection := mongoClient.Database("nourybot").Collection("channels")
// Channel // Channel
// {{"name": string}, {"connect": bool}}
chnl := Channel{channelName, true} chnl := Channel{channelName, true}
res, insertErr := collection.InsertOne(ctx, chnl) res, insertErr := collection.InsertOne(ctx, chnl)

View file

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

View file

@ -5,20 +5,19 @@ import (
"time" "time"
log "github.com/sirupsen/logrus" 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 // Interact with data
client := Connect()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
/* /*
Get my collection instance Get my collection instance
*/ */
collection := client.Database("nourybot").Collection("channels") collection := mongoClient.Database("nourybot").Collection("channels")
// Channel // Channel
chnl := Channel{channelName, true} chnl := Channel{channelName, true}

View file

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