-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCogExample.swift
More file actions
37 lines (31 loc) · 1.05 KB
/
CogExample.swift
File metadata and controls
37 lines (31 loc) · 1.05 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
import Foundation
import SwiftDisc
/// Example demonstrating a simple Cog that registers a message handler.
struct HelloCog: Cog {
let name = "HelloCog"
func onLoad(client: DiscordClient) async throws {
client.onMessageCreate { message in
if message.content.lowercased() == "!hello" {
try? await client.sendMessage(channelId: message.channel_id, content: "Hello, \(message.author.username)!")
}
}
}
func onUnload(client: DiscordClient) async throws {
// Unregistering handlers is left to implementations; this example is illustrative.
}
}
@main
struct CogExampleBot {
static func main() async {
let token = ProcessInfo.processInfo.environment["DISCORD_TOKEN"] ?? ""
let client = DiscordClient(token: token)
let manager = ExtensionManager()
let cog = HelloCog()
do {
try await manager.load(cog, client: client)
try await client.start()
} catch {
print("Failed to start: \(error)")
}
}
}