2022-08-09 01:09:53 +02:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-08-12 16:59:58 +02:00
|
|
|
// AddChannel takes in a channel name, then calls GetIdByLogin for the
|
2022-08-11 23:34:05 +02:00
|
|
|
// channels ID and inserts both the name and id value into the database.
|
2022-08-12 16:59:58 +02:00
|
|
|
// If there is no error thrown the TwitchClient joins the channel afterwards.
|
2022-08-09 22:49:48 +02:00
|
|
|
func (app *Application) AddChannel(login string, message twitch.PrivateMessage) {
|
2022-08-09 01:09:53 +02:00
|
|
|
userId, err := decapi.GetIdByLogin(login)
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:34:05 +02:00
|
|
|
// Initialize a new channel struct holding the values that will be
|
2022-08-12 16:59:58 +02:00
|
|
|
// passed into the app.Models.Channels.Insert() method.
|
2022-08-09 01:09:53 +02:00
|
|
|
channel := &data.Channel{
|
|
|
|
Login: login,
|
|
|
|
TwitchID: userId,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Channels.Insert(channel)
|
|
|
|
if err != nil {
|
2022-08-09 21:24:46 +02:00
|
|
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
} else {
|
2022-08-10 22:06:36 +02:00
|
|
|
app.TwitchClient.Join(login)
|
2022-08-09 21:24:46 +02:00
|
|
|
reply := fmt.Sprintf("Added channel %s", login)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
2022-08-09 01:09:53 +02:00
|
|
|
}
|
2022-08-09 19:50:46 +02:00
|
|
|
}
|
2022-08-09 01:09:53 +02:00
|
|
|
|
2022-08-11 23:34:05 +02:00
|
|
|
// GetAllChannels() queries the database and lists all channels.
|
|
|
|
// Only used for debug/information purposes.
|
2022-08-09 23:19:08 +02:00
|
|
|
func (app *Application) GetAllChannels() {
|
|
|
|
channel, err := app.Models.Channels.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Error(err)
|
2022-08-10 18:16:39 +02:00
|
|
|
return
|
2022-08-09 23:19:08 +02:00
|
|
|
}
|
|
|
|
app.Logger.Infow("All channels:",
|
|
|
|
"channel", channel)
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:34:05 +02:00
|
|
|
// DeleteChannel queries the database for a channel name and if it exists
|
|
|
|
// deletes the channel and makes the bot depart said channel.
|
2022-08-09 22:49:48 +02:00
|
|
|
func (app *Application) DeleteChannel(login string, message twitch.PrivateMessage) {
|
2022-08-09 19:50:46 +02:00
|
|
|
err := app.Models.Channels.Delete(login)
|
|
|
|
if err != nil {
|
|
|
|
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
|
|
|
|
app.Logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-10 22:06:36 +02:00
|
|
|
app.TwitchClient.Depart(login)
|
|
|
|
|
2022-08-09 19:50:46 +02:00
|
|
|
reply := fmt.Sprintf("Deleted channel %s", login)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
2022-08-09 01:09:53 +02:00
|
|
|
}
|
2022-08-10 18:16:39 +02:00
|
|
|
|
2022-08-12 16:59:58 +02:00
|
|
|
// InitialJoin is called on startup and queries the database for a list of
|
|
|
|
// channels which the TwitchClient then joins.
|
2022-08-10 18:16:39 +02:00
|
|
|
func (app *Application) InitialJoin() {
|
2022-08-12 16:59:58 +02:00
|
|
|
// GetJoinable returns a slice of channel names.
|
2022-08-10 18:16:39 +02:00
|
|
|
channel, err := app.Models.Channels.GetJoinable()
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-12 16:59:58 +02:00
|
|
|
// Iterate over the slice of channels and join each.
|
2022-08-10 18:16:39 +02:00
|
|
|
for _, v := range channel {
|
|
|
|
app.TwitchClient.Join(v)
|
2022-08-14 20:33:49 +02:00
|
|
|
app.Logger.Infow("Joining channel",
|
2022-08-10 18:16:39 +02:00
|
|
|
"channel", v)
|
|
|
|
}
|
|
|
|
}
|