mirror of
https://github.com/Xevion/r2park.git
synced 2025-12-13 20:12:38 -06:00
Improve logging, use time.Time for lastReload var
This commit is contained in:
11
api.go
11
api.go
@@ -16,7 +16,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
client *http.Client
|
client *http.Client
|
||||||
requestCounter uint
|
requestCounter uint
|
||||||
lastReload int64
|
lastReload time.Time
|
||||||
parsePattern = regexp.MustCompile(`\s*(.+)\n\s+(.+)\n\s+(\d+)\s*`)
|
parsePattern = regexp.MustCompile(`\s*(.+)\n\s+(.+)\n\s+(\d+)\s*`)
|
||||||
cachedLocations []Location
|
cachedLocations []Location
|
||||||
cachedLocationsMap map[uint]Location
|
cachedLocationsMap map[uint]Location
|
||||||
@@ -24,7 +24,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cacheExpiry = time.Now().Add(-time.Second) // Set the cache as expired initially
|
lastReload = time.Now().Add(-time.Hour * 24 * 365) // Set the last reload time to a year ago
|
||||||
|
cacheExpiry = time.Now().Add(-time.Second) // Set the cache as expired initially
|
||||||
cookies, err := cookiejar.New(nil)
|
cookies, err := cookiejar.New(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -75,13 +76,13 @@ func reload() {
|
|||||||
// Attempts to reload the current session based on a given location's name.
|
// Attempts to reload the current session based on a given location's name.
|
||||||
// This uses the current request counter and last reload time to determine if a reload is necessary.
|
// This uses the current request counter and last reload time to determine if a reload is necessary.
|
||||||
func tryReload() {
|
func tryReload() {
|
||||||
currentTime := time.Now().Unix()
|
currentTime := time.Now()
|
||||||
lastReloadDiff := currentTime - lastReload
|
lastReloadDiff := currentTime.Sub(lastReload)
|
||||||
|
|
||||||
if requestCounter >= 10 {
|
if requestCounter >= 10 {
|
||||||
log.Info("Reloading session due to request count...")
|
log.Info("Reloading session due to request count...")
|
||||||
} else if lastReloadDiff >= 15*60 {
|
} else if lastReloadDiff >= 15*60 {
|
||||||
log.Info("Reloading session due to time...")
|
log.Infof("Reloading session due to time (%s)...", lastReloadDiff)
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
16
main.go
16
main.go
@@ -41,11 +41,11 @@ func Bot() {
|
|||||||
|
|
||||||
// Login handler
|
// Login handler
|
||||||
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
||||||
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
log.Infof("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
|
||||||
|
|
||||||
// Count serers
|
// Count servers
|
||||||
guilds := s.State.Guilds
|
guilds := s.State.Guilds
|
||||||
log.Printf("Connected to %d server%s", len(guilds), Plural(len(guilds)))
|
log.Debugf("Connected to %d server%s", len(guilds), Plural(len(guilds)))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Open the session
|
// Open the session
|
||||||
@@ -70,7 +70,7 @@ func Bot() {
|
|||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
|
|
||||||
// Register commands
|
// Register commands
|
||||||
log.Printf("Adding %d command%s...", len(commandDefinitions), Plural(len(commandDefinitions)))
|
log.Debugf("Adding %d command%s...", len(commandDefinitions), Plural(len(commandDefinitions)))
|
||||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commandDefinitions))
|
||||||
for definitionIndex, commandDefinition := range commandDefinitions {
|
for definitionIndex, commandDefinition := range commandDefinitions {
|
||||||
command, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), commandDefinition)
|
command, err := session.ApplicationCommandCreate(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), commandDefinition)
|
||||||
@@ -90,7 +90,7 @@ func Bot() {
|
|||||||
<-stop
|
<-stop
|
||||||
|
|
||||||
// Remove commands
|
// Remove commands
|
||||||
log.Infof("Removing %d command%s...\n", len(registeredCommands), Plural(len(registeredCommands)))
|
log.Debugf("Removing %d command%s...\n", len(registeredCommands), Plural(len(registeredCommands)))
|
||||||
for _, v := range registeredCommands {
|
for _, v := range registeredCommands {
|
||||||
log.Debugf("Removing '%v' command (%v)", v.Name, v.ID)
|
log.Debugf("Removing '%v' command (%v)", v.Name, v.ID)
|
||||||
err := session.ApplicationCommandDelete(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v.ID)
|
err := session.ApplicationCommandDelete(session.State.User.ID, os.Getenv("BOT_TARGET_GUILD"), v.ID)
|
||||||
@@ -132,13 +132,11 @@ func main() {
|
|||||||
|
|
||||||
switch command {
|
switch command {
|
||||||
case "scan":
|
case "scan":
|
||||||
log.Info("Running scan")
|
log.Info("Scanning...")
|
||||||
Scan()
|
Scan()
|
||||||
case "bot":
|
case "bot":
|
||||||
log.Info("Running bot")
|
|
||||||
Bot()
|
|
||||||
default:
|
default:
|
||||||
log.Info("Running bot (default)")
|
log.Info("Starting bot...")
|
||||||
Bot()
|
Bot()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user