From 7531a67b18ba42950c61b7affbbfbc0bcd3b0b93 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Mon, 1 Nov 2021 19:34:32 +0100 Subject: [PATCH] add channel list command --- pkg/commands/channellist.go | 11 ++++++++++ pkg/db/listchannel.go | 43 +++++++++++++++++++++++++++++++++++++ pkg/handlers/command.go | 8 +++++++ 3 files changed, 62 insertions(+) create mode 100644 pkg/commands/channellist.go create mode 100644 pkg/db/listchannel.go diff --git a/pkg/commands/channellist.go b/pkg/commands/channellist.go new file mode 100644 index 0000000..3275c99 --- /dev/null +++ b/pkg/commands/channellist.go @@ -0,0 +1,11 @@ +package commands + +import ( + "github.com/lyx0/nourybot/cmd/bot" + "github.com/lyx0/nourybot/pkg/db" +) + +func ChannelList(target string, nb *bot.Bot) { + db.ListChannel(nb) + nb.Send(target, "Whispered you the list of channel.") +} diff --git a/pkg/db/listchannel.go b/pkg/db/listchannel.go new file mode 100644 index 0000000..efcac1a --- /dev/null +++ b/pkg/db/listchannel.go @@ -0,0 +1,43 @@ +package db + +import ( + "context" + "time" + + "github.com/lyx0/nourybot/cmd/bot" + "go.mongodb.org/mongo-driver/bson" +) + +// InitialJoin is called everytime the Bot starts and joins the +// initial list of twitch channels it should be in. +func ListChannel(nb *bot.Bot) { + collection := nb.MongoClient.Database("nourybot").Collection("channels") + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + // defer nb.MongoClient.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) + } + + channelList := "" + for _, ch := range channels { + nb.TwitchClient.Join(ch.Name) + // nb.TwitchClient.Say(ch.Name, "xd") + channelList += ch.Name + " " + } + nb.TwitchClient.Whisper("nouryqt", channelList) + // It worked + // nb.Send("nourybot", fmt.Sprintf("Badabing Badaboom Pepepains Joined %v channel", channelCount)) + // nb.Send("nouryqt", fmt.Sprintf("Badabing Badaboom Pepepains Joined %v channel", channelCount)) +} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 996cbec..2416390 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -75,6 +75,14 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) { commands.Bttv(target, cmdParams[1], nb) return + case "channellist": + if message.User.ID != "31437432" { + nb.Send(target, "You are not allowed to do this") + return + } + commands.ChannelList(target, nb) + return + // case "bttvemotes": // commands.BttvEmotes(target, nb) // return