-
Notifications
You must be signed in to change notification settings - Fork 6
/
d4jTestScript.kt
119 lines (106 loc) · 4.07 KB
/
d4jTestScript.kt
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import dev.arbjerg.lavalink.client.*
import dev.arbjerg.lavalink.client.event.TrackStartEvent
import dev.arbjerg.lavalink.client.player.TrackUpdateBuilder
import dev.arbjerg.lavalink.libraries.discord4j.installVoiceHandler
import discord4j.core.DiscordClientBuilder
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent
import discord4j.core.`object`.command.ApplicationCommandInteractionOption
import discord4j.core.`object`.command.ApplicationCommandInteractionOptionValue
import discord4j.core.`object`.command.ApplicationCommandOption
import discord4j.discordjson.json.ApplicationCommandOptionData
import discord4j.discordjson.json.ApplicationCommandRequest
import discord4j.gateway.intent.IntentSet
import reactor.core.publisher.Mono
fun main() {
val token = System.getenv("BOT_TOKEN")
val client = LavalinkClient(
getUserIdFromToken(token)
)
val discord = DiscordClientBuilder.create(token)
.build()
.gateway()
.setEnabledIntents(IntentSet.nonPrivileged())
.login().block()!!
registerNodeD4j(client)
val appId = discord.restClient.applicationId.block()!!
val join = ApplicationCommandRequest.builder()
.name("join")
.description("Join the voice channel you are in.")
.build()
val leave = ApplicationCommandRequest.builder()
.name("leave")
.description("Leaves the vc")
.build()
val play = ApplicationCommandRequest.builder()
.name("play")
.description("Play a song")
.addOption(
ApplicationCommandOptionData.builder()
.name("identifier")
.description("The identifier of the song you want to play")
.type(ApplicationCommandOption.Type.STRING.value)
.required(true)
.build()
)
.build()
discord.restClient.applicationService.bulkOverwriteGlobalApplicationCommand(
appId,
listOf(join, leave, play)
).subscribe()
discord.on(ChatInputInteractionEvent::class.java) {
handleSlash(client, it)
Mono.empty<Unit>()
}.subscribe()
discord.installVoiceHandler(client)
readln()
}
fun registerNodeD4j(client: LavalinkClient) {
val node = client.addNode(
NodeOptions.Builder()
.setName("Testnode")
.setServerUri("ws://localhost:2333")
.setPassword("youshallnotpass")
.build()
)
node.on<TrackStartEvent>()
.next()
.subscribe { event ->
// A new track is started!
println("A new track is started!")
println(event.track.info)
}
}
private fun handleSlash(lavalink: LavalinkClient, event: ChatInputInteractionEvent) {
when (event.commandName) {
"join" -> {
val member = event.interaction.member.get()
val voiceState = member.voiceState.block()!!
val memberVoice = voiceState.channel.block()
memberVoice?.sendConnectVoiceState(false, false)?.subscribe()
event.reply("Joining your channel!").subscribe()
}
"leave" -> {
// Disconnecting automatically destroys the player
val member = event.interaction.member.get()
val voiceState = member.voiceState.block()!!
val memberVoice = voiceState.channel.block()
memberVoice?.sendDisconnectVoiceState()?.subscribe()
event.reply("Leaving your channel!").subscribe()
}
"play" -> {
val input = event.getOption("identifier")
.flatMap(ApplicationCommandInteractionOption::getValue)
.map(ApplicationCommandInteractionOptionValue::asString)
.get()
val link = lavalink.getOrCreateLink(event.interaction.guildId.get().asLong())
link.createOrUpdatePlayer()
.updateTrack(
TrackUpdateBuilder()
.setIdentifier(input)
.build()
)
.subscribe()
event.reply("Playing!!").subscribe()
}
}
}