2023-09-07 22:34:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v4"
|
2023-10-04 00:05:38 +02:00
|
|
|
"github.com/google/uuid"
|
2023-09-07 22:34:53 +02:00
|
|
|
"github.com/lyx0/nourybot/internal/data"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (app *application) AddTimer(name, repeat string, message twitch.PrivateMessage) {
|
|
|
|
cmdParams := strings.SplitN(message.Message, " ", 500)
|
|
|
|
// prefixLength is the length of `()add timer` plus +2 (for the space and zero based)
|
2023-10-03 22:58:03 +02:00
|
|
|
prefix := "()add timer"
|
|
|
|
prefixLength := len("()add timer")
|
|
|
|
nameLength := len(name)
|
|
|
|
repeatLength := len(repeat)
|
|
|
|
app.Log.Infow("Lengths",
|
|
|
|
"prefix", prefixLength,
|
|
|
|
"name", nameLength,
|
|
|
|
"repeat", repeatLength,
|
|
|
|
"repeat2", len(cmdParams[2]),
|
|
|
|
"sum", prefixLength+nameLength+repeatLength,
|
|
|
|
)
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
// Split the message into the parts we need.
|
|
|
|
//
|
2023-10-03 22:58:03 +02:00
|
|
|
// message: ()timer add sponsor 20m hecking love my madmonq pills BatChest
|
2023-09-07 22:34:53 +02:00
|
|
|
// parts: | prefix | |name | |repeat | <----------- text -------------> |
|
2023-10-03 22:58:03 +02:00
|
|
|
text := message.Message[3+len(prefix)+len(name)+len(repeat) : len(message.Message)]
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
// 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.Log.Errorw("Received malformed time format in timer",
|
|
|
|
"repeat", repeat,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2023-10-04 00:05:38 +02:00
|
|
|
id := uuid.NewString()
|
2023-09-07 22:34:53 +02:00
|
|
|
timer := &data.Timer{
|
2023-10-04 00:05:38 +02:00
|
|
|
Name: name,
|
|
|
|
Text: text,
|
|
|
|
Identifier: id,
|
|
|
|
Channel: message.Channel,
|
|
|
|
Repeat: repeat,
|
2023-09-07 22:34:53 +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{
|
2023-10-04 00:05:38 +02:00
|
|
|
Name: name,
|
|
|
|
Text: text,
|
|
|
|
Identifier: id,
|
|
|
|
Channel: message.Channel,
|
|
|
|
Repeat: repeat,
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Timers.Insert(timer)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error inserting new timer into database",
|
|
|
|
"timer", timer,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// cronName is the internal, unique tag/name for the timer.
|
|
|
|
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
|
|
|
|
|
2023-10-04 00:05:38 +02:00
|
|
|
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, id)
|
2023-09-07 22:34:53 +02:00
|
|
|
app.Log.Infow("Added new timer",
|
|
|
|
"timer", timer,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.Log.Errorw("Received malformed time format in timer",
|
|
|
|
"timer", timer,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
reply := "Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30m, 10h, 10h30m"
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, repeat string, message twitch.PrivateMessage) {
|
|
|
|
// Check if a timer with that name is in the database.
|
|
|
|
app.Log.Info(name)
|
|
|
|
|
|
|
|
old, err := app.Models.Timers.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Could not get timer",
|
|
|
|
"timer", old,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
reply := "Something went wrong FeelsBadMan"
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------
|
|
|
|
// Delete the old timer
|
|
|
|
// -----------------------
|
2023-10-04 00:05:38 +02:00
|
|
|
identifier := old.Identifier
|
|
|
|
app.Scheduler.RemoveJob(identifier)
|
2023-09-07 22:34:53 +02:00
|
|
|
|
2023-12-17 23:15:21 +01:00
|
|
|
err = app.Models.Timers.Delete(identifier)
|
2023-09-07 22:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error deleting timer from database",
|
|
|
|
"name", name,
|
2023-10-04 00:05:38 +02:00
|
|
|
"identifier", identifier,
|
2023-09-07 22:34:53 +02:00
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------
|
|
|
|
// Add the new timer
|
|
|
|
// -----------------------
|
2023-10-03 22:58:03 +02:00
|
|
|
//cmdParams := strings.SplitN(message.Message, " ", 500)
|
2023-09-07 22:34:53 +02:00
|
|
|
// prefixLength is the length of `()editcommand` plus +2 (for the space and zero based)
|
2023-10-03 22:58:03 +02:00
|
|
|
prefix := "()edit timer"
|
|
|
|
prefixLength := len("()add timer")
|
|
|
|
nameLength := len(name)
|
|
|
|
repeatLength := len(repeat)
|
|
|
|
app.Log.Infow("Lengths",
|
|
|
|
"prefix", prefixLength,
|
|
|
|
"name", nameLength,
|
|
|
|
"repeat", repeatLength,
|
|
|
|
"sum", prefixLength+nameLength+repeatLength,
|
|
|
|
)
|
2023-09-07 22:34:53 +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 -------------> |
|
2023-10-03 22:58:03 +02:00
|
|
|
text := message.Message[3+len(prefix)+len(name)+len(repeat) : len(message.Message)]
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
// 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.Log.Errorw("Received malformed time format in timer",
|
|
|
|
"repeat", repeat,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-04 00:05:38 +02:00
|
|
|
id := uuid.NewString()
|
2023-09-07 22:34:53 +02:00
|
|
|
timer := &data.Timer{
|
2023-10-04 00:05:38 +02:00
|
|
|
Name: name,
|
|
|
|
Text: text,
|
|
|
|
Identifier: id,
|
|
|
|
Channel: message.Channel,
|
|
|
|
Repeat: repeat,
|
2023-09-07 22:34:53 +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{
|
2023-10-04 00:05:38 +02:00
|
|
|
Name: name,
|
|
|
|
Text: text,
|
|
|
|
Identifier: id,
|
|
|
|
Channel: message.Channel,
|
|
|
|
Repeat: repeat,
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Timers.Insert(timer)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error inserting new timer into database",
|
|
|
|
"timer", timer,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
} 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.
|
|
|
|
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
|
2023-10-04 00:05:38 +02:00
|
|
|
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, id)
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
app.Log.Infow("Updated a timer",
|
|
|
|
"Name", name,
|
|
|
|
"Channel", message.Channel,
|
|
|
|
"Old timer", old,
|
|
|
|
"New timer", timer,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := fmt.Sprintf("Successfully updated timer %s", name)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.Log.Errorw("Received malformed time format in timer",
|
|
|
|
"timer", timer,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
reply := "Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30s, 30m, 10h, 10h30m"
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 22:58:03 +02:00
|
|
|
// InitialTimers is called on startup and queries the database for a list of
|
|
|
|
// timers and then adds each onto the scheduler.
|
|
|
|
func (app *application) ListTimers() string {
|
|
|
|
timer, err := app.Models.Timers.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
var ts []string
|
|
|
|
|
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range timer {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
|
|
|
|
// cronName is the internal, unique tag/name for the timer.
|
|
|
|
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
|
|
|
|
t := fmt.Sprintf(
|
|
|
|
"ID: \t\t%v\n"+
|
|
|
|
"Name: \t\t%v\n"+
|
2023-10-04 00:05:38 +02:00
|
|
|
"Identifier: \t%v\n"+
|
2023-10-03 22:58:03 +02:00
|
|
|
"Text: \t\t%v\n"+
|
|
|
|
"Channel: \t%v\n"+
|
|
|
|
"Repeat: \t%v\n"+
|
|
|
|
"\n\n",
|
2023-10-04 00:05:38 +02:00
|
|
|
v.ID, v.Name, v.Identifier, v.Text, v.Channel, v.Repeat,
|
2023-10-03 22:58:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
ts = append(ts, t)
|
|
|
|
|
|
|
|
//app.Scheduler.AddFunc(repeating, func() { app.newPrivateMessageTimer(v.Channel, v.Text) }, cronName)
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, err := app.uploadPaste(strings.Join(ts, ""))
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply
|
|
|
|
}
|
|
|
|
|
2023-09-07 22:34:53 +02:00
|
|
|
// InitialTimers is called on startup and queries the database for a list of
|
|
|
|
// timers and then adds each onto the scheduler.
|
|
|
|
func (app *application) InitialTimers() {
|
|
|
|
timer, err := app.Models.Timers.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
var ts []*data.Timer
|
|
|
|
|
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range timer {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
|
|
|
|
// cronName is the internal, unique tag/name for the timer.
|
|
|
|
// A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
|
|
|
|
|
|
|
|
// Repeating is at what times the timer should repeat.
|
|
|
|
// 2 minute timer is @every 2m
|
|
|
|
repeating := fmt.Sprintf("@every %s", v.Repeat)
|
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
ts = append(ts, v)
|
|
|
|
|
2023-10-04 00:05:38 +02:00
|
|
|
app.Scheduler.AddFunc(repeating, func() { app.newPrivateMessageTimer(v.Channel, v.Text) }, v.Identifier)
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Log.Infow("Initial timers",
|
|
|
|
"timer", ts,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newPrivateMessageTimer is a helper function to set timers
|
|
|
|
// which trigger into sending a twitch PrivateMessage.
|
|
|
|
func (app *application) newPrivateMessageTimer(channel, text string) {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.SendNoContext(channel, text)
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTimer takes in the name of a timer and tries to delete the timer from the database.
|
|
|
|
func (app *application) DeleteTimer(name string, message twitch.PrivateMessage) {
|
2023-10-04 00:05:38 +02:00
|
|
|
|
|
|
|
identifier, err := app.Models.Timers.GetIdentifier(name)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error retrieving identifier rom database",
|
|
|
|
"name", name,
|
|
|
|
"identifier", identifier,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Scheduler.RemoveJob(identifier)
|
2023-09-07 22:34:53 +02:00
|
|
|
|
|
|
|
app.Log.Infow("Deleting timer",
|
|
|
|
"name", name,
|
2023-10-04 00:05:38 +02:00
|
|
|
"identifier", identifier,
|
2023-09-07 22:34:53 +02:00
|
|
|
"message.Channel", message.Channel,
|
|
|
|
)
|
|
|
|
|
2023-10-04 00:05:38 +02:00
|
|
|
err = app.Models.Timers.Delete(identifier)
|
2023-09-07 22:34:53 +02:00
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error deleting timer from database",
|
|
|
|
"name", name,
|
2023-10-04 00:05:38 +02:00
|
|
|
"identifier", identifier,
|
2023-09-07 22:34:53 +02:00
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
|
|
|
|
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := fmt.Sprintf("Deleted timer with name %s", name)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(message.Channel, reply, message)
|
2023-09-07 22:34:53 +02:00
|
|
|
}
|
2023-12-17 20:11:02 +01:00
|
|
|
|
2023-12-17 23:15:21 +01:00
|
|
|
func (app *application) DebugChannelTimers(channel string) string {
|
2023-12-17 20:11:02 +01:00
|
|
|
timer, err := app.Models.Timers.GetChannelTimer(channel)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
var ts []string
|
|
|
|
|
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range timer {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
var t string
|
|
|
|
|
|
|
|
t = fmt.Sprintf(
|
|
|
|
"Name: \t%v\n"+
|
2023-12-17 23:15:21 +01:00
|
|
|
"ID: \t%v\n"+
|
|
|
|
"Identifier: \t%v\n"+
|
2023-12-17 20:11:02 +01:00
|
|
|
"Text: \t%v\n"+
|
|
|
|
"Repeat: \t%v\n"+
|
|
|
|
"\n",
|
2023-12-17 23:15:21 +01:00
|
|
|
v.Name, v.ID, v.Identifier, v.Text, v.Repeat,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
ts = append(ts, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, err := app.uploadPaste(strings.Join(ts, ""))
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply
|
|
|
|
}
|
|
|
|
func (app *application) ListChannelTimer(channel string) string {
|
|
|
|
timer, err := app.Models.Timers.GetChannelTimer(channel)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
var ts []string
|
|
|
|
|
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range timer {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
var t string
|
|
|
|
|
|
|
|
t = fmt.Sprintf(
|
|
|
|
"Name: %v\n"+
|
|
|
|
"Text: %v\n"+
|
|
|
|
"Repeat: %v\n"+
|
|
|
|
"\n",
|
2023-12-17 20:11:02 +01:00
|
|
|
v.Name, v.Text, v.Repeat,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
ts = append(ts, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
reply, err := app.uploadPaste(strings.Join(ts, ""))
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timers from database", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply
|
|
|
|
}
|