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"
)
// AddCommand takes in a name parameter and a twitch.PrivateMessage. It slices the
// twitch.PrivateMessage after the name parameter and adds everything after to a text
// value. Then it calls the app.Models.Commands.Insert method with both name, and text
// values adding them to the database.
// 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 string, message twitch.PrivateMessage) {
// prefixLength is the length of `()addtimer` plus +2 (for the space and zero based)
cmdParams := strings.SplitN(message.Message, " ", 500)
// prefixLength is the length of `()addcommand` plus +2 (for the space and zero based)
prefixLength := 12
repeat := cmdParams[2]
// Split the twitch message at the length of the prefix + the length of the name of the command.
// prefixLength |name| text
// 0123456789012|4567|
// e.g. ()addcommand dank FeelsDankMan
// | part1 snip ^ part2 |
// 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)]
// ()addtimer gfuel 5m Yo buy my cool gfuel cause its cool n shit
// | | name |
timer := &data.Timer{
Name: name,
Text: text,
@ -35,90 +30,100 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
Repeat: repeat,
}
app.Logger.Infow("timer", timer)
err := app.Models.Timers.Insert(timer)
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)
return
} else {
// app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
// app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), (func() {
// app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func(message.Channel, text) { app.newTimer(message.Channel, text) }))
// 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.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
reply := fmt.Sprintf("Successfully added timer %s repeating every %s (ID: xd)", name, repeat)
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
}
}
// InitialJoin is called on startup and queries the database for a list of
// channels which the TwitchClient then joins.
// 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() {
// GetJoinable returns a slice of channel names.
timer, err := app.Models.Timers.GetAll()
if err != nil {
app.Logger.Errorw("XD", err)
app.Logger.Errorw("Error trying to retrieve all timers from database", err)
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
// idk either, it works for some reason
// 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
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
// app.Logger.Infow("Initial timers:",
// "Name", v.Name,
// "Channel", v.Channel,
// "Text", v.Text,
// "Repeat", v.Repeat,
// "V", v,
// "cronName", cronName,
// )
// 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", v.Channel, v.Name)
// 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)
app.Scheduler.AddFunc(repeating, func() { app.newPrivateMessageTimer(v.Channel, v.Text) }, cronName)
}
// Iterate over each timer and add them to the scheduler.
// for _, v := range timer {
// c := cron.New()
// c.Start()
//
// // app.Scheduler.Tag(fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text)
// }
for _, v := range app.Scheduler.Entries() {
app.Logger.Info(v)
}
//ent := app.Scheduler.Enries()
app.Logger.Infow("Entries", app.Scheduler.Entries())
// Log the initial timers
app.Logger.Infow("Initial timers",
"timer",
ts,
)
return
}
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)
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) {
cronName := fmt.Sprintf("%s%s", message.Channel, name)
app.Scheduler.RemoveJob(cronName)
// app.Scheduler.Remove(timer.ID)
err := app.Models.Timers.Delete(name)
if err != nil {
app.Logger.Error(err)
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
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
}
reply := fmt.Sprintf("Deleted timer %s", name)
reply := fmt.Sprintf("Deleted timer with name %s", name)
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
('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-7m',' 7 minute timer xD','nourybot','7m'),
('nourybot-10m','10 minute timer xd','nourybot','10m'),
('nourybot-20m',' 20 minutes XD','nourybot','20m'),
('uude-5m',' 5 minutes timer :)','uudelleenkytkeytynyt','5m'),
('uude-10m',' 10 minutes timer :)','uudelleenkytkeytynyt','10m'),
('xnoury-3m',' 3m timer','xnoury','3m'),
('xnoury-15m',' 15 minutes timer :)','xnoury','15m');
('nourybot-20m','20 minutes XD','nourybot','20m'),
('uude-5m','5 minutes timer :)','uudelleenkytkeytynyt','5m'),
('uude-10m','10 minutes timer :)','uudelleenkytkeytynyt','10m'),
('uude-15m','10 minutes timer :)','uudelleenkytkeytynyt','15m'),
('uude-30m','10 minutes timer :)','uudelleenkytkeytynyt','30m'),
('xnoury-3m','3m timer','xnoury','3m'),
('xnoury-15m','15 minutes timer :)','xnoury','15m');