2022-08-09 22:34:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v3"
|
|
|
|
"github.com/lyx0/nourybot/internal/data"
|
|
|
|
"github.com/lyx0/nourybot/pkg/commands/decapi"
|
|
|
|
"github.com/lyx0/nourybot/pkg/common"
|
|
|
|
)
|
|
|
|
|
2022-08-09 22:49:48 +02:00
|
|
|
func (app *Application) AddUser(login, lvl string, message twitch.PrivateMessage) {
|
2022-08-09 22:34:12 +02:00
|
|
|
userId, err := decapi.GetIdByLogin(login)
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Error(err)
|
|
|
|
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
level, err := strconv.Atoi(lvl)
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Error(err)
|
|
|
|
common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger), app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := &data.User{
|
|
|
|
Login: login,
|
|
|
|
TwitchID: userId,
|
|
|
|
Level: level,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Users.Insert(user)
|
|
|
|
if err != nil {
|
|
|
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
reply := fmt.Sprintf("Added user %s with level %v", login, level)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 02:18:39 +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)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
reply := fmt.Sprintf("User %v: ID %v, Login: %s, TwitchID: %v, Level %v", login, user.ID, user.Login, user.TwitchID, user.Level)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
app.TwitchClient.Whisper(message.User.Name, reply)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 22:49:48 +02:00
|
|
|
func (app *Application) DeleteUser(login string, message twitch.PrivateMessage) {
|
2022-08-09 22:34:12 +02:00
|
|
|
err := app.Models.Users.Delete(login)
|
|
|
|
if err != nil {
|
|
|
|
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
|
|
|
|
app.Logger.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := fmt.Sprintf("Deleted user %s", login)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
}
|
2022-08-10 00:14:54 +02:00
|
|
|
|
|
|
|
func (app *Application) EditUserLevel(user, lvl string, message twitch.PrivateMessage) {
|
|
|
|
|
|
|
|
level, err := strconv.Atoi(lvl)
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Error(err)
|
|
|
|
common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger), app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Users.SetLevel(user, level)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
common.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), app.TwitchClient)
|
|
|
|
app.Logger.Error(err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
reply := fmt.Sprintf("Updated user %s to level %v", user, level)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 22:06:36 +02:00
|
|
|
|
|
|
|
func (app *Application) GetUserLevel(login string) int {
|
|
|
|
user, err := app.Models.Users.Get(login)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return user.Level
|
|
|
|
}
|
|
|
|
}
|