2022-08-11 01:18:51 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-11 20:35:22 +02:00
|
|
|
"strconv"
|
2022-08-11 01:18:51 +02:00
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v3"
|
|
|
|
"github.com/lyx0/nourybot/pkg/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (app *Application) AddCommand(name string, message twitch.PrivateMessage) {
|
2022-08-11 01:29:17 +02:00
|
|
|
// prefixLength is the length of `()addcommand` plus +2 (for the space and zero based)
|
|
|
|
prefixLength := 14
|
2022-08-11 01:18:51 +02:00
|
|
|
|
2022-08-11 01:29:17 +02:00
|
|
|
// Split the twitch message at the length of the prefix + the length of the name of the command.
|
|
|
|
// prefixLength |name| text
|
|
|
|
// 0123456789012|4567|
|
|
|
|
// e.g. ()addcommand dank FeelsDankMan
|
2022-08-11 01:49:49 +02:00
|
|
|
// | part1 snip ^ part2 |
|
2022-08-11 01:29:17 +02:00
|
|
|
text := message.Message[prefixLength+len(name) : len(message.Message)]
|
2022-08-11 01:18:51 +02:00
|
|
|
err := app.Models.Commands.Insert(name, text)
|
|
|
|
|
2022-08-11 01:29:17 +02:00
|
|
|
// app.Logger.Infow("Message splits",
|
|
|
|
// "Command Name:", name,
|
|
|
|
// "Command Text:", text)
|
|
|
|
|
2022-08-11 01:18:51 +02:00
|
|
|
if err != nil {
|
|
|
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reply := fmt.Sprintf("Successfully added command: %s", name)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 01:49:49 +02:00
|
|
|
|
2022-08-11 19:31:58 +02:00
|
|
|
func (app *Application) GetCommand(name, username string) (string, error) {
|
|
|
|
// Fetch the command from the database if it exists.
|
2022-08-11 01:49:49 +02:00
|
|
|
command, err := app.Models.Commands.Get(name)
|
|
|
|
if err != nil {
|
2022-08-11 19:31:58 +02:00
|
|
|
// It probably did not exist
|
2022-08-11 01:49:49 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:35:22 +02:00
|
|
|
// If the command has no level set just return the text.
|
2022-08-11 19:31:58 +02:00
|
|
|
// Otherwise check if the level is high enough.
|
2022-08-11 20:35:22 +02:00
|
|
|
if command.Level == 0 {
|
2022-08-11 19:31:58 +02:00
|
|
|
return command.Text, nil
|
|
|
|
} else {
|
|
|
|
// Get the user from the database to check if the userlevel is equal
|
2022-08-11 20:35:22 +02:00
|
|
|
// or higher than the command.Level.
|
2022-08-11 19:31:58 +02:00
|
|
|
user, err := app.Models.Users.Get(username)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-11 20:35:22 +02:00
|
|
|
if user.Level >= command.Level {
|
2022-08-11 19:31:58 +02:00
|
|
|
// Userlevel is sufficient so return the command.Text
|
|
|
|
return command.Text, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Userlevel was not enough so return an empty string and error.
|
|
|
|
return "", ErrUserInsufficientLevel
|
2022-08-11 01:49:49 +02:00
|
|
|
|
|
|
|
}
|
2022-08-11 20:35:22 +02:00
|
|
|
|
|
|
|
func (app *Application) EditCommand(name, 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", ErrCommandLevelNotInteger), app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Commands.SetLevel(name, 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 command %s to level %v", name, level)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|