add channel to database

This commit is contained in:
lyx0 2022-08-09 01:09:53 +02:00
parent 07a39a7fcb
commit 1c268cd09a
5 changed files with 83 additions and 3 deletions

35
cmd/bot/add_channel.go Normal file
View file

@ -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)
}

View file

@ -53,6 +53,17 @@ func handleCommand(message twitch.PrivateMessage, app *Application) {
common.Send(target, "xd", app.TwitchClient) common.Send(target, "xd", app.TwitchClient)
return 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": case "bttv":
if msgLen < 2 { if msgLen < 2 {
common.Send(target, "Not enough arguments provided. Usage: ()bttv <emote name>", app.TwitchClient) common.Send(target, "Not enough arguments provided. Usage: ()bttv <emote name>", app.TwitchClient)

View file

@ -31,7 +31,7 @@ type Application struct {
TwitchClient *twitch.Client TwitchClient *twitch.Client
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
Db *sql.DB Db *sql.DB
models data.Models Models data.Models
} }
func main() { func main() {
@ -71,7 +71,7 @@ func main() {
TwitchClient: tc, TwitchClient: tc,
Logger: sugar, Logger: sugar,
Db: db, Db: db,
models: data.NewModels(db), Models: data.NewModels(db),
} }
// Received a PrivateMessage (normal chat message). // Received a PrivateMessage (normal chat message).

View file

@ -22,7 +22,7 @@ func (c ChannelModel) Insert(channel *Channel) error {
query := ` query := `
INSERT INTO channels (login, twitchid, announce) INSERT INTO channels (login, twitchid, announce)
VALUES ($1, $2, $3) VALUES ($1, $2, $3)
RETURNING id, created_at` RETURNING id, joined_at`
args := []interface{}{channel.Login, channel.TwitchID, channel.Announce} args := []interface{}{channel.Login, channel.TwitchID, channel.Announce}

View file

@ -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
}