2022-08-21 21:04:27 +02:00
|
|
|
package main
|
2022-08-22 02:13:07 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-26 02:27:42 +02:00
|
|
|
"regexp"
|
2022-08-22 02:13:07 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v3"
|
|
|
|
"github.com/lyx0/nourybot/internal/data"
|
|
|
|
"github.com/lyx0/nourybot/pkg/common"
|
|
|
|
)
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// AddTimer slices the message into relevant parts, adding the values onto a
|
|
|
|
// new data.Timer struct so that the timer can be inserted into the database.
|
2022-08-22 02:13:07 +02:00
|
|
|
func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
|
|
|
|
cmdParams := strings.SplitN(message.Message, " ", 500)
|
2022-08-26 02:01:43 +02:00
|
|
|
// prefixLength is the length of `()addcommand` plus +2 (for the space and zero based)
|
2022-08-22 02:13:07 +02:00
|
|
|
prefixLength := 12
|
|
|
|
repeat := cmdParams[2]
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// Split the message into the parts we need.
|
|
|
|
//
|
|
|
|
// message: ()addtimer sponsor 20m hecking love my madmonq pills BatChest
|
|
|
|
// parts: | prefix | |name | |repeat | <----------- text -------------> |
|
2022-08-22 02:13:07 +02:00
|
|
|
text := message.Message[prefixLength+len(name)+len(cmdParams[2]) : len(message.Message)]
|
|
|
|
|
2022-08-26 02:27:42 +02:00
|
|
|
// validateTimeFormat will be true if the repeat parameter is in
|
|
|
|
// the format of either 30m, 10h, or 10h30m.
|
2022-08-27 17:38:03 +02:00
|
|
|
validateTimeFormat, err := regexp.MatchString(`^(\d{1,2}[h])$|^(\d+[m])$|^(\d+[s])$|((\d{1,2}[h])((([0]?|[1-5]{1})[0-9])[m]))$`, repeat)
|
2022-08-26 02:27:42 +02:00
|
|
|
if err != nil {
|
|
|
|
app.Logger.Errorw("Received malformed time format in timer",
|
|
|
|
"repeat", repeat,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 02:13:07 +02:00
|
|
|
timer := &data.Timer{
|
|
|
|
Name: name,
|
|
|
|
Text: text,
|
|
|
|
Channel: message.Channel,
|
|
|
|
Repeat: repeat,
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:27:42 +02:00
|
|
|
// 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 added timer %s repeating every %s", name, repeat)
|
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
2022-08-22 02:13:07 +02:00
|
|
|
} else {
|
2022-08-26 02:27:42 +02:00
|
|
|
app.Logger.Errorw("Received malformed time format in timer",
|
2022-08-26 02:01:43 +02:00
|
|
|
"timer", timer,
|
2022-08-26 02:27:42 +02:00
|
|
|
"error", err,
|
2022-08-26 02:01:43 +02:00
|
|
|
)
|
2022-08-26 02:27:42 +02:00
|
|
|
reply := fmt.Sprintf("Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30m, 10h, 10h30m")
|
2022-08-22 02:13:07 +02:00
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 03:06:23 +02:00
|
|
|
|
2022-08-27 17:38:03 +02:00
|
|
|
// 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) {
|
2022-08-27 22:00:17 +02:00
|
|
|
// Check if a timer with that name is in the database.
|
2022-08-27 17:38:03 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-27 22:00:17 +02:00
|
|
|
// -----------------------
|
|
|
|
// Delete the old timer
|
|
|
|
// -----------------------
|
2022-08-27 17:38:03 +02:00
|
|
|
cronName := fmt.Sprintf("%s%s", message.Channel, name)
|
|
|
|
app.Scheduler.RemoveJob(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
|
|
|
|
}
|
|
|
|
|
2022-08-27 22:00:17 +02:00
|
|
|
// -----------------------
|
|
|
|
// Add the new timer
|
|
|
|
// -----------------------
|
2022-08-27 17:38:03 +02:00
|
|
|
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
|
2022-08-27 22:00:17 +02:00
|
|
|
} else { // this is a bit scuffed. The else here is the end of a successful call.
|
2022-08-27 17:38:03 +02:00
|
|
|
// 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)
|
2022-08-27 22:00:17 +02:00
|
|
|
|
|
|
|
app.Logger.Infow("Updated a timer",
|
|
|
|
"Name", name,
|
|
|
|
"Channel", message.Channel,
|
|
|
|
"Old timer", old,
|
|
|
|
"New timer", timer,
|
2022-08-27 17:38:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// InitialTimers is called on startup and queries the database for a list of
|
|
|
|
// timers and then adds each onto the scheduler.
|
2022-08-22 03:06:23 +02:00
|
|
|
func (app *Application) InitialTimers() {
|
|
|
|
timer, err := app.Models.Timers.GetAll()
|
|
|
|
if err != nil {
|
2022-08-26 02:01:43 +02:00
|
|
|
app.Logger.Errorw("Error trying to retrieve all timers from database", err)
|
2022-08-22 03:06:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
var ts []*data.Timer
|
2022-08-22 03:06:23 +02:00
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
2022-08-25 05:43:15 +02:00
|
|
|
for i, v := range timer {
|
2022-08-26 02:01:43 +02:00
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
2022-08-25 05:43:15 +02:00
|
|
|
i, v := i, v
|
2022-08-26 02:01:43 +02:00
|
|
|
_ = i
|
|
|
|
|
|
|
|
// cronName is the internal, unique tag/name for the timer.
|
|
|
|
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
|
2022-08-25 05:43:15 +02:00
|
|
|
cronName := fmt.Sprintf("%s%s", v.Channel, v.Name)
|
2022-08-26 02:01:43 +02:00
|
|
|
|
|
|
|
// Repeating is at what times the timer should repeat.
|
|
|
|
// 2 minute timer is @every 2m
|
2022-08-25 05:43:15 +02:00
|
|
|
repeating := fmt.Sprintf("@every %s", v.Repeat)
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// Add new value to the slice
|
|
|
|
ts = append(ts, v)
|
|
|
|
|
|
|
|
app.Scheduler.AddFunc(repeating, func() { app.newPrivateMessageTimer(v.Channel, v.Text) }, cronName)
|
2022-08-25 04:42:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
app.Logger.Infow("Initial timers",
|
2022-08-26 03:42:19 +02:00
|
|
|
"timer", ts,
|
2022-08-26 02:01:43 +02:00
|
|
|
)
|
|
|
|
return
|
2022-08-22 03:06:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// newPrivateMessageTimer is a helper function to set timers
|
|
|
|
// which trigger into sending a twitch PrivateMessage.
|
|
|
|
func (app *Application) newPrivateMessageTimer(channel, text string) {
|
2022-08-23 16:37:45 +02:00
|
|
|
common.Send(channel, text, app.TwitchClient)
|
2022-08-26 02:01:43 +02:00
|
|
|
return
|
2022-08-22 03:06:23 +02:00
|
|
|
}
|
2022-08-24 21:00:09 +02:00
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
// DeleteTimer takes in the name of a timer and tries to delete the timer from the database.
|
2022-08-24 21:00:09 +02:00
|
|
|
func (app *Application) DeleteTimer(name string, message twitch.PrivateMessage) {
|
2022-08-25 05:43:15 +02:00
|
|
|
cronName := fmt.Sprintf("%s%s", message.Channel, name)
|
2022-08-25 04:42:37 +02:00
|
|
|
app.Scheduler.RemoveJob(cronName)
|
2022-08-26 03:42:19 +02:00
|
|
|
|
2022-08-26 02:27:42 +02:00
|
|
|
app.Logger.Infow("Deleting timer",
|
|
|
|
"name", name,
|
|
|
|
"message.Channel", message.Channel,
|
|
|
|
"cronName", cronName,
|
|
|
|
)
|
2022-08-25 04:42:37 +02:00
|
|
|
|
2022-08-24 21:00:09 +02:00
|
|
|
err := app.Models.Timers.Delete(name)
|
|
|
|
if err != nil {
|
2022-08-26 02:01:43 +02:00
|
|
|
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
|
2022-08-24 21:00:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 02:01:43 +02:00
|
|
|
reply := fmt.Sprintf("Deleted timer with name %s", name)
|
2022-08-24 21:00:09 +02:00
|
|
|
common.Send(message.Channel, reply, app.TwitchClient)
|
2022-08-26 02:01:43 +02:00
|
|
|
return
|
2022-08-24 21:00:09 +02:00
|
|
|
}
|