implement adding/deleting channel and joining/parting them

This commit is contained in:
lyx0 2023-10-10 19:07:49 +02:00
parent dda26a8e49
commit 025f236144
5 changed files with 113 additions and 3 deletions

73
cmd/nourybot/channel.go Normal file
View file

@ -0,0 +1,73 @@
package main
import (
"fmt"
"github.com/gempir/go-twitch-irc/v4"
"github.com/lyx0/nourybot/internal/ivr"
)
// AddChannel takes in a channel name, then calls GetIdByLogin for the
// channels ID and inserts both the name and id value into the database.
// If there is no error thrown the TwitchClient joins the channel afterwards.
func (app *application) AddChannel(login string, message twitch.PrivateMessage) {
userID := ivr.IDByUsername(login)
err := app.Models.Channels.Insert(login, userID)
if err != nil {
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
app.Send(message.Channel, reply, message)
return
} else {
app.TwitchClient.Join(login)
reply := fmt.Sprintf("Added channel %s", login)
app.Send(message.Channel, reply, message)
return
}
}
// GetAllChannels() queries the database and lists all channels.
// Only used for debug/information purposes.
func (app *application) GetAllChannels() {
channel, err := app.Models.Channels.GetAll()
if err != nil {
app.Log.Error(err)
return
}
app.Log.Infow("All channels:",
"channel", channel)
}
// DeleteChannel queries the database for a channel name and if it exists
// deletes the channel and makes the bot depart said channel.
func (app *application) DeleteChannel(login string, message twitch.PrivateMessage) {
err := app.Models.Channels.Delete(login)
if err != nil {
app.Send(message.Channel, "Something went wrong FeelsBadMan", message)
app.Log.Error(err)
return
}
app.TwitchClient.Depart(login)
reply := fmt.Sprintf("Deleted channel %s", login)
app.Send(message.Channel, reply, message)
}
// InitialJoin is called on startup and queries the database for a list of
// channels which the TwitchClient then joins.
func (app *application) InitialJoin() {
// GetJoinable returns a slice of channel names.
channel, err := app.Models.Channels.GetJoinable()
if err != nil {
app.Log.Error(err)
return
}
// Iterate over the slice of channels and join each.
for _, v := range channel {
app.TwitchClient.Join(v)
app.Log.Infow("Joining channel",
"channel", v)
}
}

View file

@ -6,6 +6,7 @@ import (
"github.com/gempir/go-twitch-irc/v4" "github.com/gempir/go-twitch-irc/v4"
"github.com/lyx0/nourybot/internal/commands" "github.com/lyx0/nourybot/internal/commands"
"github.com/lyx0/nourybot/internal/common" "github.com/lyx0/nourybot/internal/common"
"github.com/lyx0/nourybot/internal/ivr"
) )
// handleCommand takes in a twitch.PrivateMessage and then routes the message to // handleCommand takes in a twitch.PrivateMessage and then routes the message to
@ -237,6 +238,15 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
} }
} }
case "join":
go app.AddChannel(cmdParams[1], message)
case "part":
go app.DeleteChannel(cmdParams[1], message)
case "uid":
reply = ivr.IDByUsername(cmdParams[1])
default: default:
r, err := app.GetCommand(target, commandName, userLevel) r, err := app.GetCommand(target, commandName, userLevel)
if err != nil { if err != nil {

View file

@ -49,7 +49,7 @@ func (c ChannelModel) Get(login string) (*Channel, error) {
} }
// Insert takes in a channel struct and inserts it into the database. // Insert takes in a channel struct and inserts it into the database.
func (c ChannelModel) Insert(channel *Channel) error { func (c ChannelModel) Insert(login, id string) error {
query := ` query := `
INSERT INTO channels(login, twitchid) INSERT INTO channels(login, twitchid)
VALUES ($1, $2) VALUES ($1, $2)
@ -58,7 +58,7 @@ func (c ChannelModel) Insert(channel *Channel) error {
RETURNING id, added_at; RETURNING id, added_at;
` `
args := []interface{}{channel.Login, channel.TwitchID} args := []interface{}{login, id}
// Execute the query returning the number of affected rows. // Execute the query returning the number of affected rows.
result, err := c.DB.Exec(query, args...) result, err := c.DB.Exec(query, args...)

View file

@ -18,7 +18,7 @@ var (
// as app.models.Channels.Get(login) // as app.models.Channels.Get(login)
type Models struct { type Models struct {
Channels interface { Channels interface {
Insert(channel *Channel) error Insert(login, id string) error
Get(login string) (*Channel, error) Get(login string) (*Channel, error)
GetAll() ([]*Channel, error) GetAll() ([]*Channel, error)
GetJoinable() ([]string, error) GetJoinable() ([]string, error)

27
internal/ivr/user.go Normal file
View file

@ -0,0 +1,27 @@
package ivr
import (
"encoding/json"
"fmt"
"net/http"
)
type ivrIDByUsernameResponse struct {
ID string `json:"id"`
}
func IDByUsername(username string) string {
baseUrl := "https://api.ivr.fi/v2/twitch/user?login="
resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, username))
if err != nil {
return "xd"
}
defer resp.Body.Close()
responseList := make([]ivrIDByUsernameResponse, 0)
_ = json.NewDecoder(resp.Body).Decode(&responseList)
return responseList[0].ID
}