From 1c268cd09a52c8cc4387e543da638fa04b11aed4 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 9 Aug 2022 01:09:53 +0200 Subject: [PATCH] add channel to database --- cmd/bot/add_channel.go | 35 +++++++++++++++++++++++++++++++++++ cmd/bot/command.go | 11 +++++++++++ cmd/bot/main.go | 4 ++-- internal/data/channel.go | 2 +- pkg/commands/decapi/userid.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 cmd/bot/add_channel.go create mode 100644 pkg/commands/decapi/userid.go diff --git a/cmd/bot/add_channel.go b/cmd/bot/add_channel.go new file mode 100644 index 0000000..3ad6fb5 --- /dev/null +++ b/cmd/bot/add_channel.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/internal/data" + "github.com/lyx0/nourybot/pkg/commands/decapi" + "github.com/lyx0/nourybot/pkg/common" +) + +func AddChannel(login string, message twitch.PrivateMessage, app *Application) { + fmt.Println(login) + userId, err := decapi.GetIdByLogin(login) + if err != nil { + app.Logger.Error(err) + return + } + + fmt.Println(userId) + channel := &data.Channel{ + Login: login, + TwitchID: userId, + Announce: false, + } + + err = app.Models.Channels.Insert(channel) + if err != nil { + app.Logger.DPanic(err) + } + + reply := fmt.Sprintf("Joined channel %s", login) + common.Send(message.Channel, reply, app.TwitchClient) + +} diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 54619c7..1377dac 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -53,6 +53,17 @@ func handleCommand(message twitch.PrivateMessage, app *Application) { common.Send(target, "xd", app.TwitchClient) return } + case "addchannel": + if message.User.ID != "31437432" { // Limit to myself for now. + return + } else if msgLen < 2 { + common.Send(target, "Not enough arguments provided.", app.TwitchClient) + return + } else { + // ()addchannel noemience + AddChannel(cmdParams[1], message, app) + return + } case "bttv": if msgLen < 2 { common.Send(target, "Not enough arguments provided. Usage: ()bttv ", app.TwitchClient) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 605c49a..aaf65ba 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -31,7 +31,7 @@ type Application struct { TwitchClient *twitch.Client Logger *zap.SugaredLogger Db *sql.DB - models data.Models + Models data.Models } func main() { @@ -71,7 +71,7 @@ func main() { TwitchClient: tc, Logger: sugar, Db: db, - models: data.NewModels(db), + Models: data.NewModels(db), } // Received a PrivateMessage (normal chat message). diff --git a/internal/data/channel.go b/internal/data/channel.go index 6c66450..1ab903f 100644 --- a/internal/data/channel.go +++ b/internal/data/channel.go @@ -22,7 +22,7 @@ func (c ChannelModel) Insert(channel *Channel) error { query := ` INSERT INTO channels (login, twitchid, announce) VALUES ($1, $2, $3) - RETURNING id, created_at` + RETURNING id, joined_at` args := []interface{}{channel.Login, channel.TwitchID, channel.Announce} diff --git a/pkg/commands/decapi/userid.go b/pkg/commands/decapi/userid.go new file mode 100644 index 0000000..c5efba2 --- /dev/null +++ b/pkg/commands/decapi/userid.go @@ -0,0 +1,34 @@ +package decapi + +import ( + "fmt" + "io" + "net/http" + + "go.uber.org/zap" +) + +func GetIdByLogin(login string) (string, error) { + var basePath = "https://decapi.me/twitch/id/" + + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + resp, err := http.Get(fmt.Sprint(basePath + login)) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + reply := string(body) + return reply, nil + +}