-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShardingBot.swift
More file actions
45 lines (41 loc) · 1.41 KB
/
Copy pathShardingBot.swift
File metadata and controls
45 lines (41 loc) · 1.41 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
import SwiftDisc
import Foundation
@main
struct ShardingBotMain {
static func main() async {
let token = ProcessInfo.processInfo.environment["DISCORD_BOT_TOKEN"] ?? "YOUR_BOT_TOKEN"
let client = DiscordClient(token: token)
// Observe connection state across all shards
await client.setOnReady { info in
print("Shard ready as: \(info.user.username)")
}
// Use the connection state stream to monitor gateway lifecycle
Task {
for await state in await client.connectionState {
switch state {
case .ready:
print("Gateway ready — bot is receiving events")
case .reconnecting:
print("Gateway reconnecting...")
case .disconnected:
print("Gateway disconnected")
default:
break
}
}
}
// Connect as shard 0 of 2 total shards
// In production, determine shard count via gateway/bot endpoint
do {
try await client.loginAndConnectSharded(
index: 0,
total: 2,
intents: [.guilds, .guildMessages, .messageContent]
)
let events = await client.events
for await _ in events { }
} catch {
print("Connection error: \(error)")
}
}
}