justlog/bot/commands.go

112 lines
3.2 KiB
Go
Raw Normal View History

package bot
import (
"fmt"
"strings"
twitch "github.com/gempir/go-twitch-irc/v2"
"github.com/gempir/justlog/humanize"
log "github.com/sirupsen/logrus"
)
func (b *Bot) handlePrivateMessageCommands(message twitch.PrivateMessage) {
2020-08-27 03:35:20 +02:00
if contains(b.cfg.Admins, message.User.Name) {
if strings.HasPrefix(message.Message, "!justlog status") {
uptime := humanize.TimeSince(b.startTime)
b.Say(message.Channel, message.User.DisplayName+", uptime: "+uptime)
}
2021-06-05 16:23:52 +02:00
if strings.HasPrefix(strings.ToLower(message.Message), "!justlog join ") {
b.handleJoin(message)
}
2021-06-05 16:23:52 +02:00
if strings.HasPrefix(strings.ToLower(message.Message), "!justlog part ") {
b.handlePart(message)
}
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)
}
}
}
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)
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.Join(user.Login)
}
b.cfg.AddChannels(ids...)
b.Say(message.Channel, fmt.Sprintf("%s, added channels: %v", message.User.DisplayName, ids))
}
func (b *Bot) handlePart(message twitch.PrivateMessage) {
input := strings.TrimPrefix(message.Message, "!justlog part ")
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.Part(user.Login)
}
b.cfg.RemoveChannels(ids...)
b.Say(message.Channel, fmt.Sprintf("%s, removed channels: %v", message.User.DisplayName, ids))
}
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
}