mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
refactor mongo client
This commit is contained in:
parent
4171d11dae
commit
10397e3adc
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ func main() {
|
|||
|
||||
nb = &bot.Bot{
|
||||
TwitchClient: twitch.NewClient(conf.Username, conf.Oauth),
|
||||
MongoClient: db.Connect(),
|
||||
Uptime: time.Now(),
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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":
|
||||
|
|
Loading…
Reference in a new issue