restructure

This commit is contained in:
lyx0 2021-10-28 15:13:39 +02:00
parent 6e6440e6b0
commit 07b1b39afd
3 changed files with 76 additions and 55 deletions

40
pkg/db/addchannel.go Normal file
View file

@ -0,0 +1,40 @@
package db
import (
"context"
"time"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
)
func AddChannel(channelName string) {
// Interact with data
client := Connect()
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
/*
Get my collection instance
*/
collection := client.Database("nourybot").Collection("channels")
/*
Insert channel
*/
// chnl := []interface{}{
// bson.D{{Key: "name", Value: "nouryqt"}, {Key: "connect", Value: true}},
// bson.D{{Key: "name", Value: "nourybot"}, {Key: "connect", Value: true}},
// }
chnl := []interface{}{
bson.D{{Key: "name", Value: channelName}, {Key: "connect", Value: true}},
}
res, insertErr := collection.InsertOne(ctx, chnl)
if insertErr != nil {
log.Error(insertErr)
}
log.Info(res)
}

View file

@ -5,7 +5,6 @@ import (
"fmt"
"time"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/config"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
@ -41,60 +40,6 @@ type channel struct {
Connect bool `bson:"connect,omitempty"`
}
func InsertInitialData() {
// Interact with data
client := Connect()
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
/*
Get my collection instance
*/
collection := client.Database("nourybot").Collection("channels")
/*
Insert channel
*/
chnl := []interface{}{
bson.D{{Key: "name", Value: "nouryqt"}, {Key: "connect", Value: true}},
bson.D{{Key: "name", Value: "nourybot"}, {Key: "connect", Value: true}},
}
res, insertErr := collection.InsertMany(ctx, chnl)
if insertErr != nil {
log.Error(insertErr)
}
log.Info(res)
}
func JoinChannels(nb *bot.Bot) {
client := Connect()
collection := client.Database("nourybot").Collection("channels")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
defer client.Disconnect(ctx)
cur, currErr := collection.Find(ctx, bson.D{})
if currErr != nil {
panic(currErr)
}
defer cur.Close(ctx)
var channels []channel
if err := cur.All(ctx, &channels); err != nil {
panic(err)
}
for _, ch := range channels {
nb.TwitchClient.Join(ch.Name)
nb.TwitchClient.Say(ch.Name, "xd")
// fmt.Printf("%v\n", ch.Name)
}
}
/*
List databases
*/

36
pkg/db/initialjoin.go Normal file
View file

@ -0,0 +1,36 @@
package db
import (
"context"
"time"
"github.com/lyx0/nourybot/cmd/bot"
"go.mongodb.org/mongo-driver/bson"
)
func InitialJoin(nb *bot.Bot) {
client := Connect()
collection := client.Database("nourybot").Collection("channels")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
defer client.Disconnect(ctx)
cur, currErr := collection.Find(ctx, bson.D{})
if currErr != nil {
panic(currErr)
}
defer cur.Close(ctx)
var channels []channel
if err := cur.All(ctx, &channels); err != nil {
panic(err)
}
for _, ch := range channels {
nb.TwitchClient.Join(ch.Name)
nb.TwitchClient.Say(ch.Name, "xd")
// fmt.Printf("%v\n", ch.Name)
}
}