fix only the last timer running

This commit is contained in:
lyx0 2022-08-25 05:43:15 +02:00
parent 3af85de04f
commit e8a4888489
3 changed files with 35 additions and 22 deletions

View file

@ -389,6 +389,10 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) {
return return
} }
case "asd":
app.Logger.Info(app.Scheduler.Entries())
return
case "bttvemotes": case "bttvemotes":
if userLevel < 1000 { if userLevel < 1000 {
commands.Bttvemotes(target, app.TwitchClient) commands.Bttvemotes(target, app.TwitchClient)

View file

@ -71,7 +71,6 @@ func main() {
sugar.Fatal(err) sugar.Fatal(err)
} }
c := cron.New()
//s := gocron.NewScheduler(time.UTC) //s := gocron.NewScheduler(time.UTC)
// Initialize Application // Initialize Application
@ -80,7 +79,7 @@ func main() {
Logger: sugar, Logger: sugar,
Db: db, Db: db,
Models: data.NewModels(db), Models: data.NewModels(db),
Scheduler: c, Scheduler: cron.New(),
} }
// Received a PrivateMessage (normal chat message). // Received a PrivateMessage (normal chat message).
@ -142,6 +141,7 @@ func main() {
app.InitialJoin() app.InitialJoin()
app.InitialTimers() app.InitialTimers()
app.Scheduler.Start()
common.Send("nourylul", "dankCircle", app.TwitchClient) common.Send("nourylul", "dankCircle", app.TwitchClient)
common.Send("nourybot", "gopherDance", app.TwitchClient) common.Send("nourybot", "gopherDance", app.TwitchClient)
@ -149,8 +149,6 @@ func main() {
common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient) common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient)
}) })
c.Start()
// Actually connect to chat. // Actually connect to chat.
err = app.TwitchClient.Connect() err = app.TwitchClient.Connect()
if err != nil { if err != nil {

View file

@ -45,7 +45,7 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
// app.TwitchClient.OnPrivateMessage(func(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() {
// app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func(message.Channel, text) { app.newTimer(message.Channel, text) })) // 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.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.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. // 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.Error(err) app.Logger.Errorw("XD", err)
return return
} }
app.Logger.Info(timer) app.Logger.Info(timer)
// Iterate over each timer and add them to the scheduler. // https://github.com/robfig/cron/issues/420#issuecomment-940949195
for _, v := range timer { // idk either, it works for some reason
cronName := fmt.Sprintf("%s-%s", v.Channel, v.Name) for i, v := range timer {
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", v.Repeat), func() { app.newTimer(v.Channel, v.Text) }, cronName) i, v := i, v
app.Logger.Infow("Initial timers:", cronName := fmt.Sprintf("%s%s", v.Channel, v.Name)
"Name", v.Name, // app.Logger.Info(cronName)
"Channel", v.Channel, //app.Logger.Info(fmt.Sprintf("@every %s", v.Repeat))
"Text", v.Text, repeating := fmt.Sprintf("@every %s", v.Repeat)
"Repeat", v.Repeat, app.Scheduler.AddFunc(repeating, func() { app.newTimer(v.Channel, v.Text) }, cronName)
"V", v, _ = i
"cronName", cronName, // app.Logger.Infow("Initial timers:",
) // "Name", v.Name,
// "Channel", v.Channel,
// app.Scheduler.Tag(fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text) // "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() { for _, v := range app.Scheduler.Entries() {
app.Logger.Info(v) 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. // 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) { 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) // app.Scheduler.Remove(timer.ID)