mirror of
https://github.com/Xevion/banner.git
synced 2025-12-11 00:06:35 -06:00
Handle commands during bot shutdown, move redis setup into initRedis func
This commit is contained in:
19
main.go
19
main.go
@@ -29,6 +29,7 @@ var (
|
|||||||
baseURL string // Base URL for all requests to the banner system
|
baseURL string // Base URL for all requests to the banner system
|
||||||
environment string
|
environment string
|
||||||
CentralTimeLocation *time.Location
|
CentralTimeLocation *time.Location
|
||||||
|
isClosing bool = false
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -77,7 +78,7 @@ func init() {
|
|||||||
baseURL = os.Getenv("BANNER_BASE_URL")
|
baseURL = os.Getenv("BANNER_BASE_URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func initRedis() {
|
||||||
// Setup redis
|
// Setup redis
|
||||||
redisUrl := GetFirstEnv("REDIS_URL", "REDIS_PRIVATE_URL")
|
redisUrl := GetFirstEnv("REDIS_URL", "REDIS_PRIVATE_URL")
|
||||||
if redisUrl == "" {
|
if redisUrl == "" {
|
||||||
@@ -122,8 +123,13 @@ func main() {
|
|||||||
log.Debug().Str("ping", pong).Msg("Redis connection successful")
|
log.Debug().Str("ping", pong).Msg("Redis connection successful")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
initRedis()
|
||||||
|
|
||||||
// Create cookie jar
|
// Create cookie jar
|
||||||
|
var err error
|
||||||
cookies, err = cookiejar.New(nil)
|
cookies, err = cookiejar.New(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("Cannot create cookie jar")
|
log.Err(err).Msg("Cannot create cookie jar")
|
||||||
@@ -150,6 +156,15 @@ func main() {
|
|||||||
|
|
||||||
// Setup command handlers
|
// Setup command handlers
|
||||||
session.AddHandler(func(internalSession *discordgo.Session, interaction *discordgo.InteractionCreate) {
|
session.AddHandler(func(internalSession *discordgo.Session, interaction *discordgo.InteractionCreate) {
|
||||||
|
// Handle commands during restart (highly unlikely, but just in case)
|
||||||
|
if isClosing {
|
||||||
|
err := RespondError(internalSession, interaction.Interaction, "Bot is currently restarting, try again later.", nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Failed to respond with restart error feedback")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
name := interaction.ApplicationCommandData().Name
|
name := interaction.ApplicationCommandData().Name
|
||||||
if handler, ok := commandHandlers[name]; ok {
|
if handler, ok := commandHandlers[name]; ok {
|
||||||
// Build dict of options for the log
|
// Build dict of options for the log
|
||||||
@@ -277,6 +292,8 @@ func main() {
|
|||||||
// Wait for signal (indefinite)
|
// Wait for signal (indefinite)
|
||||||
<-stop
|
<-stop
|
||||||
|
|
||||||
|
isClosing = true
|
||||||
|
|
||||||
// Defers are called after this
|
// Defers are called after this
|
||||||
log.Warn().Msg("Gracefully shutting down")
|
log.Warn().Msg("Gracefully shutting down")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user