clean up editing timer

This commit is contained in:
lyx0 2022-08-27 22:00:17 +02:00
parent 7f7839d9cb
commit 95d8268a77

View file

@ -91,6 +91,7 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
// EditTimer just contains the logic for deleting a timer, and then adding a new one // 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. // with the same name. It is technically not editing the timer.
func (app *Application) EditTimer(name string, message twitch.PrivateMessage) { func (app *Application) EditTimer(name string, message twitch.PrivateMessage) {
// Check if a timer with that name is in the database.
old, err := app.Models.Timers.Get(name) old, err := app.Models.Timers.Get(name)
if err != nil { if err != nil {
app.Logger.Errorw("Could not get timer", app.Logger.Errorw("Could not get timer",
@ -102,16 +103,12 @@ func (app *Application) EditTimer(name string, message twitch.PrivateMessage) {
return return
} }
// app.DeleteTimer(name, message) // -----------------------
// Delete the old timer
// -----------------------
cronName := fmt.Sprintf("%s%s", message.Channel, name) cronName := fmt.Sprintf("%s%s", message.Channel, name)
app.Scheduler.RemoveJob(cronName) app.Scheduler.RemoveJob(cronName)
app.Logger.Infow("Deleting timer",
"name", name,
"message.Channel", message.Channel,
"cronName", cronName,
)
err = app.Models.Timers.Delete(name) err = app.Models.Timers.Delete(name)
if err != nil { if err != nil {
app.Logger.Errorw("Error deleting timer from database", app.Logger.Errorw("Error deleting timer from database",
@ -125,7 +122,9 @@ func (app *Application) EditTimer(name string, message twitch.PrivateMessage) {
return return
} }
// app.AddTimer(name, message) // -----------------------
// Add the new timer
// -----------------------
cmdParams := strings.SplitN(message.Message, " ", 500) cmdParams := strings.SplitN(message.Message, " ", 500)
// prefixLength is the length of `()editcommand` plus +2 (for the space and zero based) // prefixLength is the length of `()editcommand` plus +2 (for the space and zero based)
prefixLength := 13 prefixLength := 13
@ -176,14 +175,18 @@ func (app *Application) EditTimer(name string, message twitch.PrivateMessage) {
reply := fmt.Sprintln("Something went wrong FeelsBadMan") reply := fmt.Sprintln("Something went wrong FeelsBadMan")
common.Send(message.Channel, reply, app.TwitchClient) common.Send(message.Channel, reply, app.TwitchClient)
return return
} else { } else { // this is a bit scuffed. The else here is the end of a successful call.
// cronName is the internal, unique tag/name for the timer. // cronName is the internal, unique tag/name for the timer.
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor" // A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
cronName := fmt.Sprintf("%s%s", message.Channel, name) cronName := fmt.Sprintf("%s%s", message.Channel, name)
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, cronName) app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, cronName)
app.Logger.Infow("Added new timer",
"timer", timer, app.Logger.Infow("Updated a timer",
"Name", name,
"Channel", message.Channel,
"Old timer", old,
"New timer", timer,
) )
reply := fmt.Sprintf("Successfully updated timer %s", name) reply := fmt.Sprintf("Successfully updated timer %s", name)