mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add logic for editing a timer
This commit is contained in:
parent
e6c54b90b8
commit
7f7839d9cb
|
@ -325,6 +325,17 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// ()edittimer testname 10m test text xd
|
||||||
|
case "edittimer":
|
||||||
|
if userLevel < 1000 { // Limit to myself for now.
|
||||||
|
return
|
||||||
|
} else if msgLen < 4 {
|
||||||
|
common.Send(target, "Not enough arguments provided.", app.TwitchClient)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// ()edituser level nourylul 1000
|
||||||
|
app.EditTimer(cmdParams[1], message)
|
||||||
|
}
|
||||||
case "editcommand": // ()editcommand level dankwave 1000
|
case "editcommand": // ()editcommand level dankwave 1000
|
||||||
if userLevel < 1000 {
|
if userLevel < 1000 {
|
||||||
return
|
return
|
||||||
|
|
115
cmd/bot/timer.go
115
cmd/bot/timer.go
|
@ -26,7 +26,7 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
|
||||||
|
|
||||||
// validateTimeFormat will be true if the repeat parameter is in
|
// validateTimeFormat will be true if the repeat parameter is in
|
||||||
// the format of either 30m, 10h, or 10h30m.
|
// the format of either 30m, 10h, or 10h30m.
|
||||||
validateTimeFormat, err := regexp.MatchString(`^(\d{1,2}[h])$|^(\d+[m])$|^((\d{1,2}[h])((([0]?|[1-5]{1})[0-9])[m]))$`, repeat)
|
validateTimeFormat, err := regexp.MatchString(`^(\d{1,2}[h])$|^(\d+[m])$|^(\d+[s])$|((\d{1,2}[h])((([0]?|[1-5]{1})[0-9])[m]))$`, repeat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Errorw("Received malformed time format in timer",
|
app.Logger.Errorw("Received malformed time format in timer",
|
||||||
"repeat", repeat,
|
"repeat", repeat,
|
||||||
|
@ -88,6 +88,119 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EditTimer just contains the logic for deleting a timer, and then adding a new one
|
||||||
|
// with the same name. It is technically not editing the timer.
|
||||||
|
func (app *Application) EditTimer(name string, message twitch.PrivateMessage) {
|
||||||
|
old, err := app.Models.Timers.Get(name)
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Errorw("Could not get timer",
|
||||||
|
"timer", old,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan")
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// app.DeleteTimer(name, message)
|
||||||
|
cronName := fmt.Sprintf("%s%s", message.Channel, name)
|
||||||
|
app.Scheduler.RemoveJob(cronName)
|
||||||
|
|
||||||
|
app.Logger.Infow("Deleting timer",
|
||||||
|
"name", name,
|
||||||
|
"message.Channel", message.Channel,
|
||||||
|
"cronName", cronName,
|
||||||
|
)
|
||||||
|
|
||||||
|
err = app.Models.Timers.Delete(name)
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Errorw("Error deleting timer from database",
|
||||||
|
"name", name,
|
||||||
|
"cronName", cronName,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// app.AddTimer(name, message)
|
||||||
|
cmdParams := strings.SplitN(message.Message, " ", 500)
|
||||||
|
// prefixLength is the length of `()editcommand` plus +2 (for the space and zero based)
|
||||||
|
prefixLength := 13
|
||||||
|
repeat := cmdParams[2]
|
||||||
|
|
||||||
|
// Split the message into the parts we need.
|
||||||
|
//
|
||||||
|
// message: ()addtimer sponsor 20m hecking love my madmonq pills BatChest
|
||||||
|
// parts: | prefix | |name | |repeat | <----------- text -------------> |
|
||||||
|
text := message.Message[prefixLength+len(name)+len(cmdParams[2]) : len(message.Message)]
|
||||||
|
|
||||||
|
// validateTimeFormat will be true if the repeat parameter is in
|
||||||
|
// the format of either 30m, 10h, or 10h30m.
|
||||||
|
validateTimeFormat, err := regexp.MatchString(`^(\d{1,2}[h])$|^(\d+[m])$|^(\d+[s])$|((\d{1,2}[h])((([0]?|[1-5]{1})[0-9])[m]))$`, repeat)
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Errorw("Received malformed time format in timer",
|
||||||
|
"repeat", repeat,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
timer := &data.Timer{
|
||||||
|
Name: name,
|
||||||
|
Text: text,
|
||||||
|
Channel: message.Channel,
|
||||||
|
Repeat: repeat,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the time string we got is valid, this is important
|
||||||
|
// because the Scheduler panics instead of erroring out if an invalid
|
||||||
|
// time format string is supplied.
|
||||||
|
if validateTimeFormat {
|
||||||
|
timer := &data.Timer{
|
||||||
|
Name: name,
|
||||||
|
Text: text,
|
||||||
|
Channel: message.Channel,
|
||||||
|
Repeat: repeat,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = app.Models.Timers.Insert(timer)
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Errorw("Error inserting new timer into database",
|
||||||
|
"timer", timer,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
|
||||||
|
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// cronName is the internal, unique tag/name for the timer.
|
||||||
|
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
|
||||||
|
cronName := fmt.Sprintf("%s%s", message.Channel, name)
|
||||||
|
|
||||||
|
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, cronName)
|
||||||
|
app.Logger.Infow("Added new timer",
|
||||||
|
"timer", timer,
|
||||||
|
)
|
||||||
|
|
||||||
|
reply := fmt.Sprintf("Successfully updated timer %s", name)
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
app.Logger.Errorw("Received malformed time format in timer",
|
||||||
|
"timer", timer,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30s, 30m, 10h, 10h30m")
|
||||||
|
common.Send(message.Channel, reply, app.TwitchClient)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// InitialTimers is called on startup and queries the database for a list of
|
// InitialTimers is called on startup and queries the database for a list of
|
||||||
// timers and then adds each onto the scheduler.
|
// timers and then adds each onto the scheduler.
|
||||||
func (app *Application) InitialTimers() {
|
func (app *Application) InitialTimers() {
|
||||||
|
|
|
@ -40,6 +40,7 @@ type Models struct {
|
||||||
Timers interface {
|
Timers interface {
|
||||||
Get(name string) (*Timer, error)
|
Get(name string) (*Timer, error)
|
||||||
Insert(timer *Timer) error
|
Insert(timer *Timer) error
|
||||||
|
Update(timer *Timer) error
|
||||||
GetAll() ([]*Timer, error)
|
GetAll() ([]*Timer, error)
|
||||||
Delete(name string) error
|
Delete(name string) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,6 +128,33 @@ func (t TimerModel) GetAll() ([]*Timer, error) {
|
||||||
return timers, nil
|
return timers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t TimerModel) Update(timer *Timer) error {
|
||||||
|
query := `
|
||||||
|
UPDATE timers
|
||||||
|
SET text = $2, channel = $3, repeat = $4
|
||||||
|
WHERE name = $1
|
||||||
|
RETURNING id`
|
||||||
|
|
||||||
|
args := []interface{}{
|
||||||
|
timer.Name,
|
||||||
|
timer.Text,
|
||||||
|
timer.Channel,
|
||||||
|
timer.Repeat,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := t.DB.QueryRow(query, args...).Scan(&timer.ID)
|
||||||
|
if err != nil {
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, sql.ErrNoRows):
|
||||||
|
return ErrEditConflict
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Delete takes in a command name and queries the database for an entry with
|
// Delete takes in a command name and queries the database for an entry with
|
||||||
// the same name and tries to delete that entry.
|
// the same name and tries to delete that entry.
|
||||||
func (t TimerModel) Delete(name string) error {
|
func (t TimerModel) Delete(name string) error {
|
||||||
|
|
Loading…
Reference in a new issue