Repository navigation
Expand file tree
/
Copy pathShardingBot.swift
More file actions
54 lines (48 loc) · 1.62 KB
/
Copy pathShardingBot.swift
File metadata and controls
54 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import SwiftDisc
import Foundation
@main
struct ShardingBotMain {
static func main() async {
let token = ProcessInfo.processInfo.environment["DISCORD_BOT_TOKEN"] ?? "YOUR_BOT_TOKEN"
let config = ShardingGatewayManager.Configuration(
shardCount: .automatic,
identifyConcurrency: .respectDiscordLimits,
fallbackPresence: .init(
activities: [
.init(name: "with shards", type: 0)
],
status: "online",
afk: false
),
connectionDelay: .staggered(interval: 5.0)
)
let manager = ShardingGatewayManager(
token: token,
configuration: config,
intents: [.guilds, .guildMessages, .messageContent]
)
// Monitor shard health periodically
Task {
while true {
let health = await manager.healthCheck()
print("Health: \(health.readyShards)/\(health.totalShards) shards ready, \(health.totalGuilds) guilds")
try? await Task.sleep(nanoseconds: 30_000_000_000)
}
}
// Monitor events with shard metadata
Task {
for await event in await manager.events {
print("[Shard \(event.shardId)] \(event.event)")
}
}
do {
try await manager.connect()
// Keep the process alive
while true {
try await Task.sleep(nanoseconds: UInt64.max)
}
} catch {
print("Sharding manager failed: \(error)")
}
}
}