2020-03-08 17:54:06 +01:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
twitch "github.com/gempir/go-twitch-irc/v2"
|
|
|
|
"github.com/gempir/justlog/humanize"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-01-08 22:19:20 +01:00
|
|
|
func (b *Bot) handlePrivateMessageCommands(message twitch.PrivateMessage) {
|
2020-08-27 03:35:20 +02:00
|
|
|
if contains(b.cfg.Admins, message.User.Name) {
|
2021-10-01 06:32:17 -04:00
|
|
|
if strings.HasPrefix(message.Message, "!justlog status") {
|
2020-03-08 17:54:06 +01:00
|
|
|
uptime := humanize.TimeSince(b.startTime)
|
2020-12-05 18:32:21 +01:00
|
|
|
b.Say(message.Channel, message.User.DisplayName+", uptime: "+uptime)
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
2021-06-05 16:23:52 +02:00
|
|
|
if strings.HasPrefix(strings.ToLower(message.Message), "!justlog join ") {
|
2020-03-08 17:54:06 +01:00
|
|
|
b.handleJoin(message)
|
|
|
|
}
|
2021-06-05 16:23:52 +02:00
|
|
|
if strings.HasPrefix(strings.ToLower(message.Message), "!justlog part ") {
|
2021-04-16 18:03:41 +02:00
|
|
|
b.handlePart(message)
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
2021-06-05 16:23:52 +02:00
|
|
|
if strings.HasPrefix(strings.ToLower(message.Message), "!justlog optout ") {
|
|
|
|
b.handleOptOut(message)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(strings.ToLower(message.Message), "!justlog optin ") {
|
|
|
|
b.handleOptIn(message)
|
|
|
|
}
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) handleJoin(message twitch.PrivateMessage) {
|
|
|
|
input := strings.TrimPrefix(message.Message, "!justlog join ")
|
|
|
|
|
|
|
|
users, err := b.helixClient.GetUsersByUsernames(strings.Split(input, ","))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2020-12-05 18:32:21 +01:00
|
|
|
b.Say(message.Channel, message.User.DisplayName+", something went wrong requesting the userids")
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ids := []string{}
|
|
|
|
for _, user := range users {
|
|
|
|
ids = append(ids, user.ID)
|
2020-12-05 18:32:21 +01:00
|
|
|
b.Join(user.Login)
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
|
|
|
b.cfg.AddChannels(ids...)
|
2020-12-05 18:32:21 +01:00
|
|
|
b.Say(message.Channel, fmt.Sprintf("%s, added channels: %v", message.User.DisplayName, ids))
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 18:03:41 +02:00
|
|
|
func (b *Bot) handlePart(message twitch.PrivateMessage) {
|
|
|
|
input := strings.TrimPrefix(message.Message, "!justlog part ")
|
2020-03-08 17:54:06 +01:00
|
|
|
|
2021-04-16 18:03:41 +02:00
|
|
|
users, err := b.helixClient.GetUsersByUsernames(strings.Split(input, ","))
|
2020-03-08 17:54:06 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2021-04-16 18:03:41 +02:00
|
|
|
b.Say(message.Channel, message.User.DisplayName+", something went wrong requesting the userids")
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 18:03:41 +02:00
|
|
|
ids := []string{}
|
|
|
|
for _, user := range users {
|
|
|
|
ids = append(ids, user.ID)
|
|
|
|
b.Part(user.Login)
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
2021-04-16 18:03:41 +02:00
|
|
|
b.cfg.RemoveChannels(ids...)
|
|
|
|
b.Say(message.Channel, fmt.Sprintf("%s, removed channels: %v", message.User.DisplayName, ids))
|
2020-03-08 17:54:06 +01:00
|
|
|
}
|
2020-08-27 03:35:20 +02:00
|
|
|
|
2021-06-05 16:23:52 +02:00
|
|
|
func (b *Bot) handleOptOut(message twitch.PrivateMessage) {
|
|
|
|
input := strings.TrimPrefix(strings.ToLower(message.Message), "!justlog optout ")
|
|
|
|
|
|
|
|
users, err := b.helixClient.GetUsersByUsernames(strings.Split(input, ","))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
b.Say(message.Channel, message.User.DisplayName+", something went wrong requesting the userids")
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := []string{}
|
|
|
|
for _, user := range users {
|
|
|
|
ids = append(ids, user.ID)
|
|
|
|
}
|
|
|
|
b.cfg.OptOutUsers(ids...)
|
|
|
|
b.Say(message.Channel, fmt.Sprintf("%s, opted out channels: %v", message.User.DisplayName, ids))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) handleOptIn(message twitch.PrivateMessage) {
|
|
|
|
input := strings.TrimPrefix(strings.ToLower(message.Message), "!justlog optin ")
|
|
|
|
|
|
|
|
users, err := b.helixClient.GetUsersByUsernames(strings.Split(input, ","))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
b.Say(message.Channel, message.User.DisplayName+", something went wrong requesting the userids")
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := []string{}
|
|
|
|
for _, user := range users {
|
|
|
|
ids = append(ids, user.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cfg.RemoveOptOut(ids...)
|
|
|
|
b.Say(message.Channel, fmt.Sprintf("%s, opted in channels: %v", message.User.DisplayName, ids))
|
|
|
|
}
|
|
|
|
|
2020-08-27 03:35:20 +02:00
|
|
|
func contains(arr []string, str string) bool {
|
|
|
|
for _, x := range arr {
|
|
|
|
if x == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|