From e8a48884892341b77fe67164c903f833d7777729 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 25 Aug 2022 05:43:15 +0200 Subject: [PATCH] fix only the last timer running --- cmd/bot/commands.go | 4 ++++ cmd/bot/main.go | 6 ++---- cmd/bot/timer.go | 47 ++++++++++++++++++++++++++++----------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index c81c1bb..82c75c1 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -389,6 +389,10 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { return } + case "asd": + app.Logger.Info(app.Scheduler.Entries()) + return + case "bttvemotes": if userLevel < 1000 { commands.Bttvemotes(target, app.TwitchClient) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index c2c1eae..8db3178 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -71,7 +71,6 @@ func main() { sugar.Fatal(err) } - c := cron.New() //s := gocron.NewScheduler(time.UTC) // Initialize Application @@ -80,7 +79,7 @@ func main() { Logger: sugar, Db: db, Models: data.NewModels(db), - Scheduler: c, + Scheduler: cron.New(), } // Received a PrivateMessage (normal chat message). @@ -142,6 +141,7 @@ func main() { app.InitialJoin() app.InitialTimers() + app.Scheduler.Start() common.Send("nourylul", "dankCircle", app.TwitchClient) common.Send("nourybot", "gopherDance", app.TwitchClient) @@ -149,8 +149,6 @@ func main() { common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient) }) - c.Start() - // Actually connect to chat. err = app.TwitchClient.Connect() if err != nil { diff --git a/cmd/bot/timer.go b/cmd/bot/timer.go index cc12ab6..08fad3c 100644 --- a/cmd/bot/timer.go +++ b/cmd/bot/timer.go @@ -45,7 +45,7 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) { // 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 := 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 @@ -61,28 +61,39 @@ func (app *Application) InitialTimers() { // GetJoinable returns a slice of channel names. timer, err := app.Models.Timers.GetAll() if err != nil { - app.Logger.Error(err) + app.Logger.Errorw("XD", err) return } app.Logger.Info(timer) - // Iterate over each timer and add them to the scheduler. - for _, v := range timer { - cronName := fmt.Sprintf("%s-%s", v.Channel, v.Name) - app.Scheduler.AddFunc(fmt.Sprintf("@every %s", v.Repeat), func() { app.newTimer(v.Channel, v.Text) }, cronName) - app.Logger.Infow("Initial timers:", - "Name", v.Name, - "Channel", v.Channel, - "Text", v.Text, - "Repeat", v.Repeat, - "V", v, - "cronName", cronName, - ) - - // app.Scheduler.Tag(fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text) - + // https://github.com/robfig/cron/issues/420#issuecomment-940949195 + // idk either, it works for some reason + for i, v := range timer { + 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, + // ) } + + // 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) } @@ -98,7 +109,7 @@ func (app *Application) newTimer(channel, text string) { // DeleteCommand takes in a name value and deletes the command from the database if it exists. 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.Remove(timer.ID)