Skip to content

Commit

Permalink
handle startup gateway event only once & start more message update ro…
Browse files Browse the repository at this point in the history
…utines
  • Loading branch information
jxsl13 committed Jan 18, 2024
1 parent 6a66d70 commit 9686dba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
*.env
twstatus-bot
.DS_Store
*.db
Expand Down
10 changes: 9 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"name": "Launch Dev Environment",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["-c", "${workspaceFolder}/.env"]
},
{
"name": "Launch Prod Environment",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["-c", "${workspaceFolder}/prod.env"]
}
]
}
52 changes: 29 additions & 23 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"runtime"
"sort"
"sync"
"time"

"github.com/diamondburned/arikawa/v3/api"
Expand Down Expand Up @@ -325,33 +326,38 @@ func New(
gateway.IntentGuilds | gateway.IntentGuildMessages | gateway.IntentGuildMessageReactions,
)

var startupOnce sync.Once
s.AddHandler(func(*gateway.ReadyEvent) {
me, err := s.Me()
if err != nil {
log.Fatalf("failed to get bot user: %v", err)
}
bot.userID = me.ID
// it's possible that the bot occasionally looses the gateway connection
// calling this heavy weight function on every reconnect is not ideal
startupOnce.Do(func() {
me, err := s.Me()
if err != nil {
log.Fatalf("failed to get bot user: %v", err)
}
bot.userID = me.ID

log.Println("connected to the gateway as", me.Tag())
src, dst, err := bot.updateServers()
if err != nil {
log.Printf("failed to initialize server list: %v", err)
} else {
log.Printf("initialized server list with %d source and %d target servers", src, dst)
}
log.Println("connected to the gateway as", me.Tag())
src, dst, err := bot.updateServers()
if err != nil {
log.Printf("failed to initialize server list: %v", err)
} else {
log.Printf("initialized server list with %d source and %d target servers", src, dst)
}

// sync trackings and player notification requests
err = bot.syncDatabaseState(ctx)
if err != nil {
log.Fatalf("failed to synchronize database with discord state: %v", err)
}
// sync trackings and player notification requests
err = bot.syncDatabaseState(ctx)
if err != nil {
log.Fatalf("failed to synchronize database with discord state: %v", err)
}

// start polling
go bot.cacheCleanup()
go bot.serverUpdater(pollingInterval)
for i := 0; i < max(runtime.NumCPU(), 2); i++ {
go bot.messageUpdater(i + 1)
}
// start polling
go bot.cacheCleanup()
go bot.serverUpdater(pollingInterval)
for i := 0; i < max(2*runtime.NumCPU(), 5); i++ {
go bot.messageUpdater(i + 1)
}
})
})

// requires guild message intents
Expand Down

0 comments on commit 9686dba

Please sign in to comment.