get channels from database and join them on startup

This commit is contained in:
lyx0 2022-08-10 18:16:39 +02:00
parent 68faa1b7a4
commit fa9566d6ce
5 changed files with 75 additions and 9 deletions

View file

@ -38,6 +38,7 @@ func (app *Application) GetAllChannels() {
channel, err := app.Models.Channels.GetAll()
if err != nil {
app.Logger.Error(err)
return
}
app.Logger.Infow("All channels:",
"channel", channel)
@ -54,3 +55,18 @@ func (app *Application) DeleteChannel(login string, message twitch.PrivateMessag
reply := fmt.Sprintf("Deleted channel %s", login)
common.Send(message.Channel, reply, app.TwitchClient)
}
func (app *Application) InitialJoin() {
channel, err := app.Models.Channels.GetJoinable()
if err != nil {
app.Logger.Error(err)
return
}
for _, v := range channel {
app.TwitchClient.Join(v)
app.Logger.Infow("Joining channel:",
"channel", v)
}
}

View file

@ -99,11 +99,10 @@ func main() {
common.StartTime()
common.Send("nourylul", "xd", app.TwitchClient)
app.GetAllChannels()
// app.GetAllChannels()
app.InitialJoin()
})
app.TwitchClient.Join("nourylul")
// Actually connect to chat.
err = app.TwitchClient.Connect()
if err != nil {

View file

@ -30,12 +30,12 @@ func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) {
}
// Message was no command so we just print it.
app.Logger.Infow("Private Message received",
// "message", message,
"message.Channel", message.Channel,
"message.User", message.User.DisplayName,
"message.Message", message.Message,
)
// app.Logger.Infow("Private Message received",
// // "message", message,
// "message.Channel", message.Channel,
// "message.User", message.User.DisplayName,
// "message.Message", message.Message,
// )
}

View file

@ -128,6 +128,56 @@ func (c ChannelModel) GetAll() ([]*Channel, error) {
return channels, nil
}
// GetAll() returns a slice of all channels in the database.
func (c ChannelModel) GetJoinable() ([]string, error) {
query := `
SELECT login
FROM channels
ORDER BY id`
// Create a context with 3 seconds timeout.
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// Use QueryContext() the context and query. This returns a
// sql.Rows resultset containing our channels.
rows, err := c.DB.QueryContext(ctx, query)
if err != nil {
return nil, err
}
// Need to defer a call to rows.Close() to ensure the resultset
// is closed before GetAll() returns.
defer rows.Close()
// Initialize an empty slice to hold the data.
channels := []string{}
// Iterate over the resultset.
for rows.Next() {
// Initialize an empty Channel struct where we put on
// a single channel value.
var channel Channel
// Scan the values onto the channel struct
err := rows.Scan(
&channel.Login,
)
if err != nil {
return nil, err
}
// Add the single movie struct onto the slice.
channels = append(channels, channel.Login)
}
// When rows.Next() finishes call rows.Err() to retrieve any errors.
if err = rows.Err(); err != nil {
return nil, err
}
return channels, nil
}
func (c ChannelModel) Delete(login string) error {
// Prepare the statement.
query := `

View file

@ -18,6 +18,7 @@ type Models struct {
Insert(channel *Channel) error
Get(login string) (*Channel, error)
GetAll() ([]*Channel, error)
GetJoinable() ([]string, error)
Delete(login string) error
}
Users interface {