Skip to content

Commit

Permalink
Voting and snagging support
Browse files Browse the repository at this point in the history
  • Loading branch information
nugget committed Apr 11, 2021
1 parent a29a5c0 commit 9c7b57d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 15 deletions.
6 changes: 5 additions & 1 deletion lib/tt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"github.com/alaingilbert/ttapi"
)

var Bot *ttapi.Bot
var (
Bot *ttapi.Bot
Me string
)

func New(auth, userID, roomID string) error {
Bot = ttapi.NewBot(auth, userID, roomID)
Me = userID

return nil
}
38 changes: 36 additions & 2 deletions lib/tt/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,44 @@ import (
"github.com/sirupsen/logrus"
)

func UnpackVotelog(votelog [][]string) (string, string) {
if len(votelog) < 1 {
logrus.WithField("votelog", votelog).Warn("Cannot parse Votelog")
return "", ""
}

if len(votelog[0]) < 2 {
logrus.WithField("votelog", votelog).Warn("Cannot parse Votelog")
return "", ""
}

userID := votelog[0][0]
vote := votelog[0][1]

return userID, vote
}

func Bop() {
err := Bot.Bop()
if Bot.CurrentDjID == Me {
logrus.Debug("Not voting for myself")
return
}

err := Bot.VoteUp()
if err != nil {
logrus.WithError(err).Error("Unable to VoteUp")
}
}

func Lame() {
if Bot.CurrentDjID == Me {
logrus.Debug("Not voting for myself")
return
}

err := Bot.VoteDown()
if err != nil {
logrus.WithError(err).Error("Failed to bop")
logrus.WithError(err).Error("Unable to VoteDown")
}
}

Expand Down
64 changes: 52 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"math/rand"
"os"
"regexp"
Expand Down Expand Up @@ -36,6 +35,7 @@ func onNewSong(evt ttapi.NewSongEvt) {
logrus.WithFields(logrus.Fields{
"command": evt.Command,
"dj": evt.Room.Metadata.CurrentSong.Djname,
"djID": evt.Room.Metadata.CurrentSong.Djid,
"song": evt.Room.Metadata.CurrentSong.Metadata.Song,
"artist": evt.Room.Metadata.CurrentSong.Metadata.Artist,
"success": evt.Success,
Expand All @@ -53,14 +53,28 @@ func onSpeak(evt ttapi.SpeakEvt) {
}

func onUpdateVotes(evt ttapi.UpdateVotesEvt) {
userID, vote := tt.UnpackVotelog(evt.Room.Metadata.Votelog)
user, err := tt.Bot.GetProfile(userID)
if err != nil {
logrus.WithFields(logrus.Fields{
"userID": userID,
"error": err,
}).Error("Can't load user profile")
}

logrus.WithFields(logrus.Fields{
"command": evt.Command,
"success": evt.Success,
"up": evt.Room.Metadata.Upvotes,
"down": evt.Room.Metadata.Downvotes,
"listeners": evt.Room.Metadata.Listeners,
"votelog": evt.Room.Metadata.Votelog,
"userID": userID,
"vote": vote,
"name": user.Name,
"points": user.Points,
"ACL": user.ACL,
}).Info("Vote")

}

func onPmmed(evt ttapi.PmmedEvt) {
Expand All @@ -71,6 +85,39 @@ func onPmmed(evt ttapi.PmmedEvt) {
}).Info("Received PM")
}

func onSnagged(evt ttapi.SnaggedEvt) {
user, err := tt.Bot.GetProfile(evt.UserID)
if err != nil {
logrus.WithFields(logrus.Fields{
"userID": evt.UserID,
"error": err,
}).Error("Can't load user profile")
}

logrus.WithFields(logrus.Fields{
"userID": evt.UserID,
"user": user.Name,
"roomID": evt.RoomID,
"command": evt.Command,
}).Info("User snagged current song")

if evt.UserID == tt.Me {
logrus.Debug("Ignoring self-snag")
} else {
err = tt.Bot.PlaylistAdd("", "", 0)
if err != nil {
logrus.WithError(err).Error("Cannot add to Playlist")
} else {
logrus.Info("Added current song to my playlist")

err = tt.Bot.Snag()
if err != nil {
logrus.WithError(err).Warn("Could not emote the Snag heart")
}
}
}
}

func pmSay(evt ttapi.PmmedEvt) {
re := regexp.MustCompile(`(?i)^/(say) (.*)`)
res := re.FindStringSubmatch(evt.Text)
Expand Down Expand Up @@ -109,24 +156,16 @@ func pmSimpleCommands(evt ttapi.PmmedEvt) {
re := regexp.MustCompile(`(?i)^/([^ ]+)$`)
res := re.FindStringSubmatch(evt.Text)

fmt.Println("pmSimple", len(res), res)

if len(res) != 0 {
command := strings.ToLower(res[1])

switch command {
case "skip":
tt.Bot.Skip()
case "bop":
err := tt.Bot.VoteUp()
if err != nil {
logrus.WithError(err).Error("Unable to bop")
}
tt.Bop()
case "lame":
err := tt.Bot.VoteDown()
if err != nil {
logrus.WithError(err).Error("Unable to lame")
}
tt.Lame()
}
}
}
Expand Down Expand Up @@ -175,6 +214,7 @@ func main() {
tt.Bot.OnPmmed(pmDJ)

// General Purpose event handlers
tt.Bot.OnSnagged(onSnagged)
tt.Bot.OnReady(onReady)
tt.Bot.OnNewSong(onNewSong)
tt.Bot.OnSpeak(onSpeak)
Expand Down

0 comments on commit 9c7b57d

Please sign in to comment.