mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add lastfm functionality
This commit is contained in:
parent
3e351431b2
commit
9825967182
|
@ -81,6 +81,15 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
|
|||
reply, _ = commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4])
|
||||
}
|
||||
|
||||
case "lastfm":
|
||||
if msgLen == 1 {
|
||||
reply = app.UserCheckLastFM(message)
|
||||
} else {
|
||||
// Default to first argument supplied being the name
|
||||
// of the user to look up recently played.
|
||||
reply = commands.LastFmUserRecent(target, cmdParams[1])
|
||||
}
|
||||
|
||||
case "nourybot":
|
||||
reply = "Lidl Twitch bot made by @nourylul. Prefix: ()"
|
||||
|
||||
|
@ -97,7 +106,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
|
|||
// ()weather <location>
|
||||
case "weather":
|
||||
if msgLen == 1 {
|
||||
reply = "Not enough arguments provided."
|
||||
app.UserCheckWeather(message)
|
||||
} else if msgLen < 2 {
|
||||
reply = "Not enough arguments provided."
|
||||
} else {
|
||||
|
@ -139,6 +148,23 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
|
|||
}
|
||||
}
|
||||
|
||||
case "set":
|
||||
switch cmdParams[1] {
|
||||
case "lastfm":
|
||||
app.SetUserLastFM(cmdParams[2], message)
|
||||
case "location":
|
||||
app.SetUserLocation(message)
|
||||
}
|
||||
|
||||
case "user":
|
||||
switch cmdParams[1] {
|
||||
case "edit":
|
||||
switch cmdParams[2] {
|
||||
case "level":
|
||||
app.EditUserLevel(cmdParams[3], cmdParams[4], message)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the commandName exists as the "name" of a command in the database.
|
||||
// if it doesnt then ignore it.
|
||||
// ##################
|
||||
|
|
169
cmd/nourybot/user.go
Normal file
169
cmd/nourybot/user.go
Normal file
|
@ -0,0 +1,169 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v4"
|
||||
"github.com/lyx0/nourybot/internal/commands"
|
||||
)
|
||||
|
||||
// AddUser calls GetIdByLogin to get the twitch id of the login name and then adds
|
||||
// the login name, twitch id and supplied level to the database.
|
||||
func (app *application) InitUser(login, twitchId string, message twitch.PrivateMessage) {
|
||||
_, err := app.Models.Users.Check(twitchId)
|
||||
app.Log.Error(err)
|
||||
if err != nil {
|
||||
app.Models.Users.Insert(login, twitchId)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// DebugUser queries the database for a login name, if that name exists it returns the fields
|
||||
// and outputs them to twitch chat and a twitch whisper.
|
||||
func (app *application) DebugUser(login string, message twitch.PrivateMessage) {
|
||||
user, err := app.Models.Users.Get(login)
|
||||
|
||||
if err != nil {
|
||||
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
|
||||
app.Send(message.Channel, reply)
|
||||
return
|
||||
} else {
|
||||
reply := fmt.Sprintf("User %v: ID %v, Login: %s, TwitchID: %v, Level: %v", login, user.ID, user.Login, user.TwitchID, user.Level)
|
||||
app.Send(message.Channel, reply)
|
||||
//app.TwitchClient.Whisper(message.User.Name, reply)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteUser takes in a login string, queries the database for an entry with
|
||||
// that login name and tries to delete that entry in the database.
|
||||
func (app *application) DeleteUser(login string, message twitch.PrivateMessage) {
|
||||
err := app.Models.Users.Delete(login)
|
||||
if err != nil {
|
||||
app.Send(message.Channel, "Something went wrong FeelsBadMan")
|
||||
app.Log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
reply := fmt.Sprintf("Deleted user %s", login)
|
||||
app.Send(message.Channel, reply)
|
||||
}
|
||||
|
||||
// EditUserLevel tries to update the database record for the supplied
|
||||
// login name with the new level.
|
||||
func (app *application) EditUserLevel(login, lvl string, message twitch.PrivateMessage) {
|
||||
// Convert the level string to an integer. This is an easy check to see if
|
||||
// the level supplied was a number only.
|
||||
level, err := strconv.Atoi(lvl)
|
||||
if err != nil {
|
||||
app.Log.Error(err)
|
||||
app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger))
|
||||
return
|
||||
}
|
||||
|
||||
err = app.Models.Users.SetLevel(login, level)
|
||||
if err != nil {
|
||||
app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound))
|
||||
app.Log.Error(err)
|
||||
return
|
||||
} else {
|
||||
reply := fmt.Sprintf("Updated user %s to level %v", login, level)
|
||||
app.Send(message.Channel, reply)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// SetUserLocation sets new location for the user
|
||||
func (app *application) SetUserLocation(message twitch.PrivateMessage) {
|
||||
// snipLength is the length we need to "snip" off of the start of `message`.
|
||||
// `()set location` = +13
|
||||
// trailing space = +1
|
||||
// zero-based = +1
|
||||
// = 16
|
||||
snipLength := 15
|
||||
|
||||
// Split the twitch message at `snipLength` plus length of the name of the
|
||||
// The part of the message we are left over with is then passed on to the database
|
||||
// handlers as the `location` part of the command.
|
||||
location := message.Message[snipLength:len(message.Message)]
|
||||
twitchId := message.User.ID
|
||||
|
||||
err := app.Models.Users.SetLocation(twitchId, location)
|
||||
if err != nil {
|
||||
app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound))
|
||||
app.Log.Error(err)
|
||||
return
|
||||
} else {
|
||||
reply := fmt.Sprintf("Successfully set your location to %v", location)
|
||||
app.Send(message.Channel, reply)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// SetUserLastFM tries to update the database record for the supplied
|
||||
// login name with the new level.
|
||||
func (app *application) SetUserLastFM(lastfmUser string, message twitch.PrivateMessage) {
|
||||
login := message.User.Name
|
||||
|
||||
err := app.Models.Users.SetLastFM(login, lastfmUser)
|
||||
if err != nil {
|
||||
app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound))
|
||||
app.Log.Error(err)
|
||||
return
|
||||
} else {
|
||||
reply := fmt.Sprintf("Successfully set your lastfm username to %v", lastfmUser)
|
||||
app.Send(message.Channel, reply)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserLevel takes in a login name and queries the database for an entry
|
||||
// with such a name value. If there is one it returns the level value as an integer.
|
||||
// Returns 0 on an error which is the level for unregistered users.
|
||||
func (app *application) GetUserLevel(twitchId string) int {
|
||||
userLevel, err := app.Models.Users.GetLevel(twitchId)
|
||||
if err != nil {
|
||||
return 0
|
||||
} else {
|
||||
return userLevel
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) UserCheckWeather(message twitch.PrivateMessage) {
|
||||
target := message.Channel
|
||||
twitchLogin := message.User.Name
|
||||
twitchId := message.User.ID
|
||||
|
||||
location, err := app.Models.Users.GetLocation(twitchId)
|
||||
if err != nil {
|
||||
app.Log.Errorw("No location data registered for: ",
|
||||
"twitchLogin:", twitchLogin,
|
||||
"twitchId:", twitchId,
|
||||
)
|
||||
reply := "No location for your account set in my database. Use ()set location <location> to register. Otherwise use ()weather <location> without registering."
|
||||
app.Send(message.Channel, reply)
|
||||
return
|
||||
}
|
||||
|
||||
reply, _ := commands.Weather(location)
|
||||
app.Send(target, reply)
|
||||
}
|
||||
|
||||
func (app *application) UserCheckLastFM(message twitch.PrivateMessage) string {
|
||||
twitchLogin := message.User.Name
|
||||
target := message.Channel
|
||||
|
||||
lastfmUser, err := app.Models.Users.GetLastFM(twitchLogin)
|
||||
if err != nil {
|
||||
app.Log.Errorw("No LastFM account registered for: ",
|
||||
"twitchLogin:", twitchLogin,
|
||||
)
|
||||
reply := "No lastfm account registered in my database. Use ()set lastfm <username> to register. Otherwise use ()lastfm <username> without registering."
|
||||
return reply
|
||||
}
|
||||
|
||||
reply := commands.LastFmUserRecent(target, lastfmUser)
|
||||
return reply
|
||||
}
|
1
go.mod
1
go.mod
|
@ -8,6 +8,7 @@ require (
|
|||
github.com/lib/pq v1.10.9
|
||||
github.com/nicklaw5/helix v1.25.0
|
||||
github.com/rs/zerolog v1.29.1
|
||||
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0
|
||||
go.uber.org/zap v1.24.0
|
||||
)
|
||||
|
||||
|
|
2
go.sum
2
go.sum
|
@ -31,6 +31,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
|
||||
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
|
||||
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0 h1:cgqwZtnR+IQfUYDLJ3Kiy4aE+O/wExTzEIg8xwC4Qfs=
|
||||
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0/go.mod h1:n3nudMl178cEvD44PaopxH9jhJaQzthSxUzLO5iKMy4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
|
|
43
internal/commands/lastfm.go
Normal file
43
internal/commands/lastfm.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/shkh/lastfm-go/lastfm"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func LastFmUserRecent(target, user string) string {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
sugar.Error("Error loading LASTFM API keys from .env file")
|
||||
}
|
||||
|
||||
apiKey := os.Getenv("LAST_FM_API_KEY")
|
||||
apiSecret := os.Getenv("LAST_FM_SECRET")
|
||||
|
||||
api := lastfm.New(apiKey, apiSecret)
|
||||
result, _ := api.User.GetRecentTracks(lastfm.P{"user": user}) //discarding error
|
||||
|
||||
var reply string
|
||||
for i, track := range result.Tracks {
|
||||
// The 0th result is the most recent one since it goes from most recent
|
||||
// to least recent.
|
||||
if i == 0 {
|
||||
sugar.Infow("Most recent: ",
|
||||
"user:", user,
|
||||
"track", track.Name,
|
||||
"artist", track.Artist.Name,
|
||||
)
|
||||
|
||||
reply = fmt.Sprintf("Most recently played track for user %v: %v - %v", user, track.Artist.Name, track.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return reply
|
||||
}
|
Loading…
Reference in a new issue