diff --git a/bot.go b/bot.go index 7985ccb..468b787 100644 --- a/bot.go +++ b/bot.go @@ -112,6 +112,30 @@ func LaunchBotTui() { } } +func checkBotChannels() bool { + select { + case <-BotStruct.hotReloadCallback: + if err := BotStruct.HotReload(); err != nil { + logrus.Errorf("Bot hot reload failed: %v", err) + BotStruct.hotReloadCallback <- false + } else { + logrus.Info("Bot hot reload successfully.") + BotStruct.hotReloadCallback <- true + } + break + case <-BotStruct.reloadCallback: + logrus.Info("Bot reload successfully.") + break + case <-BotStruct.exitCh: + logrus.Info("Bot process successfully stopped.") + return true + default: + break + } + + return false +} + // Initialize client and bot configs, download dependencies, // launch Selenium instance and finally run dm bot routine func launchDmBot(ctx context.Context) { @@ -159,35 +183,24 @@ func launchDmBot(ctx context.Context) { go func() { rand.Seed(time.Now().Unix()) if err = BotStruct.Scheduler.CheckTime(); err == nil { + if exit := checkBotChannels(); exit { + return + } BotStruct.ConnectToInstagram() for { var users []string + if exit := checkBotChannels(); exit { + return + } users, err = BotStruct.FetchUsersFromUserFollowers() if err != nil { BotStruct.crashCh <- fmt.Errorf("Failed users fetching: %v. Check logs tab for more details", err) return } for _, username := range users { - select { - case <-BotStruct.hotReloadCallback: - if err = BotStruct.HotReload(); err != nil { - logrus.Errorf("Bot hot reload failed: %v", err) - BotStruct.hotReloadCallback <- false - } else { - logrus.Info("Bot hot reload successfully.") - BotStruct.hotReloadCallback <- true - } - break - case <-BotStruct.reloadCallback: - logrus.Info("Bot reload successfully.") - break - case <-BotStruct.exitCh: - logrus.Info("Bot process successfully stopped.") + if exit := checkBotChannels(); exit { return - default: - break } - var res bool res, err = BotStruct.SendMessage(username, BotStruct.DmModule.DmTemplates[rand.Intn(len(BotStruct.DmModule.DmTemplates))]) if !res || err != nil { diff --git a/resources/static/app/assets/js/controllers.js b/resources/static/app/assets/js/controllers.js index 3dfb5ee..fc7b092 100644 --- a/resources/static/app/assets/js/controllers.js +++ b/resources/static/app/assets/js/controllers.js @@ -69,6 +69,7 @@ $(document).ready(function(){ }); } else { $('#dmBotLaunchIcon').addClass('fa-spinner').addClass('fa-spin').removeClass('fa-skull-crossbones'); + toastr.info("Stop procedure launched, the bot will stop once the current action is finished.") astilectron.sendMessage({"msg":"stopDmBot"}, function(message) { if(message.status === SUCCESS) { toastr.success(message.msg); @@ -87,6 +88,7 @@ $(document).ready(function(){ $("#dmBotHotReloadBtn").on("click", function(){ $('#dmBotHotReloadIcn').addClass('fa-spinner').addClass('fa-spin').removeClass('fa-fire'); + toastr.info("Hot reload launched, the bot will update once the current action is finished.") astilectron.sendMessage({"msg":"hotReloadBot"}, function(message) { if(message.status === SUCCESS) { toastr.success(message.msg);