-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebhookBot.swift
More file actions
53 lines (45 loc) · 2.04 KB
/
Copy pathWebhookBot.swift
File metadata and controls
53 lines (45 loc) · 2.04 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
import SwiftDisc
import Foundation
@main
struct WebhookBotMain {
static func main() async {
let token = ProcessInfo.processInfo.environment["DISCORD_BOT_TOKEN"] ?? "YOUR_BOT_TOKEN"
let channelId = ChannelID(ProcessInfo.processInfo.environment["DISCORD_CHANNEL_ID"] ?? "0")
let client = DiscordClient(token: token)
await client.setOnReady { info in
print("Connected as: \(info.user.username)")
// Create a webhook for the channel
do {
let webhook = try await client.createWebhook(channelId: channelId, name: "MyWebhook")
print("Webhook created: \(webhook.id)")
// Execute the webhook (asynchronous, no message returned)
_ = try await client.executeWebhook(webhookId: webhook.id, token: webhook.token ?? "", content: "Hello from webhook!")
// Execute with wait=true to get the message back
if let msg = try await client.executeWebhook(
webhookId: webhook.id,
token: webhook.token ?? "",
content: "Webhook with embed",
embeds: [Embed(title: "Webhook Embed", description: "Sent via webhook")],
wait: true
) {
print("Webhook message sent: \(msg.id)")
}
// Edit a webhook's name
let updated = try await client.modifyWebhook(webhookId: webhook.id, name: "RenamedWebhook")
print("Webhook renamed to: \(updated.name ?? "''")")
// Delete the webhook
try await client.deleteWebhook(webhookId: webhook.id)
print("Webhook deleted")
} catch {
print("Webhook operation failed: \(error)")
}
}
do {
try await client.loginAndConnect(intents: [.guilds])
let events = await client.events
for await _ in events { }
} catch {
print("Connection error: \(error)")
}
}
}