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"
|
"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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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}
|
||||||
|
|
|
@ -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":
|
||||||
|
|
Loading…
Reference in a new issue