2023-09-08 01:36:41 +02:00
package main
import (
"fmt"
"strconv"
"github.com/gempir/go-twitch-irc/v4"
2023-10-11 18:52:46 +02:00
"github.com/lyx0/nourybot/pkg/lastfm"
"github.com/lyx0/nourybot/pkg/owm"
2023-09-08 01:36:41 +02:00
)
2024-02-28 11:53:39 +01:00
// InitUser is called on each command usage and checks if the user that sent the command
// is already in the database. If he isn't then add the user to the database.
2023-10-10 12:49:06 +02:00
func ( app * application ) InitUser ( login , twitchId string ) {
2023-09-08 01:36:41 +02:00
_ , err := app . Models . Users . Check ( twitchId )
2023-10-10 12:49:06 +02:00
//app.Log.Error(err)
2023-09-08 01:36:41 +02:00
if err != nil {
2023-10-10 18:03:13 +02:00
go app . Models . Users . Insert ( login , twitchId )
2023-09-08 01:36:41 +02:00
return
}
}
2024-02-28 11:53:39 +01:00
// DebugUser queries the database for a login name, if that name exists it creates a new paste
2023-09-08 01:36:41 +02:00
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 )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , reply , message )
2023-09-08 01:36:41 +02:00
return
} else {
2023-09-16 19:39:10 +02:00
// subject := fmt.Sprintf("DEBUG for user %v", login)
2023-09-16 17:18:14 +02:00
body := fmt . Sprintf ( "id=%v \nlogin=%v \nlevel=%v \nlocation=%v \nlastfm=%v" ,
user . TwitchID ,
user . Login ,
user . Level ,
user . Location ,
user . LastFMUsername ,
)
resp , err := app . uploadPaste ( body )
if err != nil {
app . Log . Errorln ( "Could not upload paste:" , err )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , fmt . Sprintf ( "Something went wrong FeelsBadMan %v" , ErrDuringPasteUpload ) , message )
2023-09-16 17:18:14 +02:00
return
}
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , resp , message )
2023-09-08 01:36:41 +02:00
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 {
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , "Something went wrong FeelsBadMan" , message )
2023-09-08 01:36:41 +02:00
app . Log . Error ( err )
return
}
reply := fmt . Sprintf ( "Deleted user %s" , login )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , reply , message )
2023-09-08 01:36:41 +02:00
}
// 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 )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , fmt . Sprintf ( "Something went wrong FeelsBadMan %s" , ErrUserLevelNotInteger ) , message )
2023-09-08 01:36:41 +02:00
return
}
err = app . Models . Users . SetLevel ( login , level )
if err != nil {
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , fmt . Sprintf ( "Something went wrong FeelsBadMan %s" , ErrRecordNotFound ) , message )
2023-09-08 01:36:41 +02:00
app . Log . Error ( err )
return
} else {
reply := fmt . Sprintf ( "Updated user %s to level %v" , login , level )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , reply , message )
2023-09-08 01:36:41 +02:00
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`.
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 {
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , fmt . Sprintf ( "Something went wrong FeelsBadMan %s" , ErrRecordNotFound ) , message )
2023-09-08 01:36:41 +02:00
app . Log . Error ( err )
return
} else {
reply := fmt . Sprintf ( "Successfully set your location to %v" , location )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , reply , message )
2023-09-08 01:36:41 +02:00
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 {
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , fmt . Sprintf ( "Something went wrong FeelsBadMan %s" , ErrRecordNotFound ) , message )
2023-09-08 01:36:41 +02:00
app . Log . Error ( err )
return
} else {
reply := fmt . Sprintf ( "Successfully set your lastfm username to %v" , lastfmUser )
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , reply , message )
2023-09-08 01:36:41 +02:00
return
}
}
2024-02-28 11:53:39 +01:00
// GetUserLevel returns the level a user has in the database.
2023-10-11 20:46:02 +02:00
func ( app * application ) GetUserLevel ( msg twitch . PrivateMessage ) int {
var dbUserLevel int
var twitchUserLevel int
lvl , err := app . Models . Users . GetLevel ( msg . User . ID )
2023-09-08 01:36:41 +02:00
if err != nil {
2023-10-11 20:46:02 +02:00
dbUserLevel = 0
} else {
dbUserLevel = lvl
}
if msg . User . Badges [ "moderator" ] == 1 ||
msg . User . Badges [ "broadcaster" ] == 1 {
twitchUserLevel = 250
2023-10-11 21:35:47 +02:00
} else if msg . User . Badges [ "vip" ] == 1 {
twitchUserLevel = 100
2023-10-11 20:46:02 +02:00
} else {
twitchUserLevel = 0
}
if dbUserLevel > twitchUserLevel {
return dbUserLevel
2023-09-08 01:36:41 +02:00
} else {
2023-10-11 20:46:02 +02:00
return twitchUserLevel
2023-09-08 01:36:41 +02:00
}
}
2024-02-28 11:53:39 +01:00
// UserCheckWeather checks if a user is in the database and if he has a location
// provided. If both is true it calls owm.Weather with the location and replies
// with the result.
// If no location was provided the response will instruct the user how to set a location.
2023-09-08 01:36:41 +02:00
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."
2023-10-10 17:51:12 +02:00
app . Send ( message . Channel , reply , message )
2023-09-08 01:36:41 +02:00
return
}
2023-10-11 18:52:46 +02:00
reply , _ := owm . Weather ( location )
2023-10-10 17:51:12 +02:00
app . Send ( target , reply , message )
2023-09-08 01:36:41 +02:00
}
2024-02-28 11:53:39 +01:00
// UserCheckWeather checks if a user is in the database and if he has a lastfm
// username provided. If both is true it calls lastfm.LastFmUserRecent with the username
// and replies with the result.
// If no lastfm username was provided the response will instruct the user how to set a lastfm username.
2023-09-08 01:36:41 +02:00
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
}
2023-10-11 18:52:46 +02:00
reply := lastfm . LastFmUserRecent ( target , lastfmUser )
2023-09-08 01:36:41 +02:00
return reply
}