-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComponentsV2Bot.swift
More file actions
79 lines (72 loc) · 3.04 KB
/
Copy pathComponentsV2Bot.swift
File metadata and controls
79 lines (72 loc) · 3.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import SwiftDisc
import Foundation
@main
struct ComponentsV2BotMain {
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 { _ in
print("Bot connected. Sending Components v2 message...")
let select = MessageComponent.ChannelSelectMenu(
custom_id: "channel_picker",
placeholder: "Pick a channel"
)
let row = MessageComponent.ActionRow(components: [.channelSelect(select)])
Task {
_ = try? await client.sendMessage(
channelId: channelId,
content: "Welcome! Use the menu below:",
components: [.actionRow(row)],
flags: .isComponentsV2
)
print("Components v2 message sent (flags: isComponentsV2)")
}
}
await client.setOnInteractionCreate { interaction in
guard let data = interaction.data else { return }
if interaction.type == .messageComponent, let customId = data.custom_id {
if customId == "channel_picker" {
let input = MessageComponent.TextInput(
custom_id: "feedback_text",
style: 2,
label: "Your thoughts?",
required: true,
placeholder: "Tell us what you think..."
)
let label = MessageComponent.Label(
label: "Feedback",
description: "Share your experience",
components: [.textInput(input)]
)
Task {
try? await client.createInteractionModal(
interactionId: interaction.id,
token: interaction.token,
title: "Component Feedback",
customId: "feedback_modal",
components: [.label(label)]
)
}
}
} else if interaction.type == .modalSubmit, data.custom_id == "feedback_modal" {
print("Modal submitted!")
Task {
try? await client.createInteractionResponse(
interactionId: interaction.id,
token: interaction.token,
type: .channelMessageWithSource,
content: "Thanks for your feedback!"
)
}
}
}
do {
try await client.loginAndConnect(intents: [.guilds])
let events = await client.events
for await _ in events { }
} catch {
print("Connection error: \(error)")
}
}
}