addchannel works

This commit is contained in:
lyx0 2021-10-28 16:16:11 +02:00
parent 20f9470ce8
commit cb0181c01f
5 changed files with 27 additions and 24 deletions

View file

@ -11,9 +11,9 @@ type Bot struct {
Uptime time.Time Uptime time.Time
} }
type Channel struct { // type Channel struct {
Name string // Name string
} // }
// Send checks the message against a banphrase api // Send checks the message against a banphrase api
// and also splits the message into two if the message // and also splits the message into two if the message

View file

@ -5,7 +5,6 @@ import (
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
) )
func AddChannel(channelName string) { func AddChannel(channelName string) {
@ -21,17 +20,8 @@ func AddChannel(channelName string) {
*/ */
collection := client.Database("nourybot").Collection("channels") collection := client.Database("nourybot").Collection("channels")
/* // Channel
Insert channel chnl := Channel{channelName, true}
*/
// 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) res, insertErr := collection.InsertOne(ctx, chnl)
if insertErr != nil { if insertErr != nil {

View file

@ -2,7 +2,6 @@ package db
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/lyx0/nourybot/pkg/config" "github.com/lyx0/nourybot/pkg/config"
@ -12,6 +11,11 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
type Channel struct {
Name string `bson:"name,omitempty"`
Connect bool `bson:"connect,omitempty"`
}
func Connect() *mongo.Client { func Connect() *mongo.Client {
conf := config.LoadConfig() conf := config.LoadConfig()
@ -29,20 +33,23 @@ func Connect() *mongo.Client {
} }
// defer client.Disconnect(ctx) // defer client.Disconnect(ctx)
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
log.Info("Connected to MongoDB!")
databases, err := client.ListDatabaseNames(ctx, bson.M{}) databases, err := client.ListDatabaseNames(ctx, bson.M{})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(databases) _ = databases
// log.Info(databases)
return client return client
} }
type channel struct {
Name string `bson:"name,omitempty"`
Connect bool `bson:"connect,omitempty"`
}
/* /*
List databases List databases
*/ */

View file

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/cmd/bot"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
) )
@ -24,15 +25,15 @@ func InitialJoin(nb *bot.Bot) {
} }
defer cur.Close(ctx) defer cur.Close(ctx)
var channels []channel var channels []Channel
if err := cur.All(ctx, &channels); err != nil { if err := cur.All(ctx, &channels); err != nil {
panic(err) panic(err)
} }
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")
// fmt.Printf("%v\n", ch.Name)
} }
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/commands" "github.com/lyx0/nourybot/pkg/commands"
"github.com/lyx0/nourybot/pkg/db"
"github.com/lyx0/nourybot/pkg/utils" "github.com/lyx0/nourybot/pkg/utils"
) )
@ -140,6 +141,10 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
return return
} }
case "join":
db.AddChannel(cmdParams[1])
nb.Send(message.Channel, "Joined")
case "firstline": case "firstline":
if msgLen == 1 { if msgLen == 1 {
nb.Send(target, "Usage: ()firstline [channel] [user]") nb.Send(target, "Usage: ()firstline [channel] [user]")