clean up timers

This commit is contained in:
lyx0 2022-08-26 02:01:43 +02:00
parent e8a4888489
commit 5abb28ec5d
2 changed files with 79 additions and 65 deletions

View file

@ -9,25 +9,20 @@ import (
"github.com/lyx0/nourybot/pkg/common" "github.com/lyx0/nourybot/pkg/common"
) )
// AddCommand takes in a name parameter and a twitch.PrivateMessage. It slices the // AddTimer slices the message into relevant parts, adding the values onto a
// twitch.PrivateMessage after the name parameter and adds everything after to a text // new data.Timer struct so that the timer can be inserted into the database.
// value. Then it calls the app.Models.Commands.Insert method with both name, and text
// values adding them to the database.
func (app *Application) AddTimer(name string, message twitch.PrivateMessage) { func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
// prefixLength is the length of `()addtimer` plus +2 (for the space and zero based)
cmdParams := strings.SplitN(message.Message, " ", 500) cmdParams := strings.SplitN(message.Message, " ", 500)
// prefixLength is the length of `()addcommand` plus +2 (for the space and zero based)
prefixLength := 12 prefixLength := 12
repeat := cmdParams[2] repeat := cmdParams[2]
// Split the twitch message at the length of the prefix + the length of the name of the command. // Split the message into the parts we need.
// prefixLength |name| text //
// 0123456789012|4567| // message: ()addtimer sponsor 20m hecking love my madmonq pills BatChest
// e.g. ()addcommand dank FeelsDankMan // parts: | prefix | |name | |repeat | <----------- text -------------> |
// | part1 snip ^ part2 |
text := message.Message[prefixLength+len(name)+len(cmdParams[2]) : len(message.Message)] text := message.Message[prefixLength+len(name)+len(cmdParams[2]) : len(message.Message)]
// ()addtimer gfuel 5m Yo buy my cool gfuel cause its cool n shit
// | | name |
timer := &data.Timer{ timer := &data.Timer{
Name: name, Name: name,
Text: text, Text: text,
@ -35,90 +30,100 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
Repeat: repeat, Repeat: repeat,
} }
app.Logger.Infow("timer", timer)
err := app.Models.Timers.Insert(timer) err := app.Models.Timers.Insert(timer)
if err != nil { if err != nil {
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) 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) common.Send(message.Channel, reply, app.TwitchClient)
return return
} else { } else {
// app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { // cronName is the internal, unique tag/name for the timer.
// app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), (func() { // A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
// app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func(message.Channel, text) { app.newTimer(message.Channel, text) }))
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.newTimer(message.Channel, text) }, cronName)
//app.Scheduler.Tag(fmt.Sprintf("%s-%s", message.Channel, name)).Every(repeat).StartAt(time.Now()).Do(app.newTimer, message.Channel, text app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newPrivateMessageTimer(message.Channel, text) }, cronName)
reply := fmt.Sprintf("Successfully added timer %s repeating every %s (ID: xd)", name, repeat) 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) common.Send(message.Channel, reply, app.TwitchClient)
return return
} }
} }
// InitialJoin 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
// channels which the TwitchClient then joins. // timers and then adds each onto the scheduler.
func (app *Application) InitialTimers() { func (app *Application) InitialTimers() {
// GetJoinable returns a slice of channel names.
timer, err := app.Models.Timers.GetAll() timer, err := app.Models.Timers.GetAll()
if err != nil { if err != nil {
app.Logger.Errorw("XD", err) app.Logger.Errorw("Error trying to retrieve all timers from database", err)
return return
} }
app.Logger.Info(timer) // The slice of timers is only used to log them at
// the start so it looks a bit nicer.
var ts []*data.Timer
// https://github.com/robfig/cron/issues/420#issuecomment-940949195 // Iterate over all timers and then add them onto the scheduler.
// idk either, it works for some reason
for i, v := range timer { 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, v := i, v
cronName := fmt.Sprintf("%s%s", v.Channel, v.Name)
// app.Logger.Info(cronName)
//app.Logger.Info(fmt.Sprintf("@every %s", v.Repeat))
repeating := fmt.Sprintf("@every %s", v.Repeat)
app.Scheduler.AddFunc(repeating, func() { app.newTimer(v.Channel, v.Text) }, cronName)
_ = i _ = i
// app.Logger.Infow("Initial timers:",
// "Name", v.Name, // cronName is the internal, unique tag/name for the timer.
// "Channel", v.Channel, // A timer named "sponsor" in channel "forsen" will be named "forsensponsor"
// "Text", v.Text, cronName := fmt.Sprintf("%s%s", v.Channel, v.Name)
// "Repeat", v.Repeat,
// "V", v, // Repeating is at what times the timer should repeat.
// "cronName", cronName, // 2 minute timer is @every 2m
// ) repeating := fmt.Sprintf("@every %s", v.Repeat)
// Add new value to the slice
ts = append(ts, v)
app.Scheduler.AddFunc(repeating, func() { app.newPrivateMessageTimer(v.Channel, v.Text) }, cronName)
} }
// Iterate over each timer and add them to the scheduler. // Log the initial timers
// for _, v := range timer { app.Logger.Infow("Initial timers",
// c := cron.New() "timer",
// c.Start() ts,
// )
// // app.Scheduler.Tag(fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text) return
// }
for _, v := range app.Scheduler.Entries() {
app.Logger.Info(v)
}
//ent := app.Scheduler.Enries()
app.Logger.Infow("Entries", app.Scheduler.Entries())
} }
func (app *Application) newTimer(channel, text string) { // newPrivateMessageTimer is a helper function to set timers
// which trigger into sending a twitch PrivateMessage.
func (app *Application) newPrivateMessageTimer(channel, text string) {
common.Send(channel, text, app.TwitchClient) common.Send(channel, text, app.TwitchClient)
return
} }
// DeleteCommand takes in a name value and deletes the command from the database if it exists. // 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) { func (app *Application) DeleteTimer(name string, message twitch.PrivateMessage) {
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.Scheduler.Remove(timer.ID)
err := app.Models.Timers.Delete(name) err := app.Models.Timers.Delete(name)
if err != nil { if err != nil {
app.Logger.Error(err) app.Logger.Errorw("Error deleting timer from database",
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient) "name", name,
"cronName", cronName,
"error", err,
)
reply := fmt.Sprintln("Something went wrong FeelsBadMan")
common.Send(message.Channel, reply, app.TwitchClient)
return
} }
reply := fmt.Sprintf("Deleted timer %s", name) reply := fmt.Sprintf("Deleted timer with name %s", name)
common.Send(message.Channel, reply, app.TwitchClient) common.Send(message.Channel, reply, app.TwitchClient)
return
} }

View file

@ -7,11 +7,20 @@ CREATE TABLE IF NOT EXISTS timers (
); );
INSERT INTO timers (name,"text",channel,repeat) VALUES INSERT INTO timers (name,"text",channel,repeat) VALUES
('nourylul-60m','timer every 60 minutes :)','nourylul','60m'),
('nourybot-60m','timer every 60 minutes :)','nourybot','60m'),
('nourybot-1h','timer every 1 hour :)','nourybot','1h'),
('nouryxd-60m','timer every 60 minutes :)','nouryxd','60m'),
('nouryxd-1h','timer every 1 hour :)','nouryxd','1h'),
('nourybot-2m',' 2 minute timer xD','nourybot','2m'),
('nourybot-4m',' 4 minute timer xD','nourybot','4m'), ('nourybot-4m',' 4 minute timer xD','nourybot','4m'),
('nourybot-7m',' 7 minute timer xD','nourybot','7m'),
('nourybot-10m','10 minute timer xd','nourybot','10m'), ('nourybot-10m','10 minute timer xd','nourybot','10m'),
('nourybot-20m',' 20 minutes XD','nourybot','20m'), ('nourybot-20m','20 minutes XD','nourybot','20m'),
('uude-5m',' 5 minutes timer :)','uudelleenkytkeytynyt','5m'), ('uude-5m','5 minutes timer :)','uudelleenkytkeytynyt','5m'),
('uude-10m',' 10 minutes timer :)','uudelleenkytkeytynyt','10m'), ('uude-10m','10 minutes timer :)','uudelleenkytkeytynyt','10m'),
('xnoury-3m',' 3m timer','xnoury','3m'), ('uude-15m','10 minutes timer :)','uudelleenkytkeytynyt','15m'),
('xnoury-15m',' 15 minutes timer :)','xnoury','15m'); ('uude-30m','10 minutes timer :)','uudelleenkytkeytynyt','30m'),
('xnoury-3m','3m timer','xnoury','3m'),
('xnoury-15m','15 minutes timer :)','xnoury','15m');