-
Notifications
You must be signed in to change notification settings - Fork 6
/
JavaJDAExample.java
300 lines (261 loc) · 12.7 KB
/
JavaJDAExample.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import dev.arbjerg.lavalink.client.*;
import dev.arbjerg.lavalink.client.event.EmittedEvent;
import dev.arbjerg.lavalink.client.event.StatsEvent;
import dev.arbjerg.lavalink.client.event.TrackStartEvent;
import dev.arbjerg.lavalink.client.loadbalancing.RegionGroup;
import dev.arbjerg.lavalink.client.loadbalancing.builtin.VoiceRegionPenaltyProvider;
import dev.arbjerg.lavalink.client.player.*;
import dev.arbjerg.lavalink.libraries.jda.JDAVoiceUpdateListener;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Random;
public class JavaJDAExample extends ListenerAdapter {
private final LavalinkClient client;
public static void main(String[] args) throws InterruptedException {
new JavaJDAExample();
}
public JavaJDAExample() throws InterruptedException {
final var token = System.getenv("BOT_TOKEN");
this.client = new LavalinkClient(Helpers.getUserIdFromToken(token));
this.client.getLoadBalancer().addPenaltyProvider(new VoiceRegionPenaltyProvider());
this.registerLavalinkListeners();
this.registerLavalinkNodes();
JDABuilder.createDefault(token)
.setVoiceDispatchInterceptor(new JDAVoiceUpdateListener(this.client))
.enableIntents(GatewayIntent.GUILD_VOICE_STATES)
.enableCache(CacheFlag.VOICE_STATE)
.addEventListeners(this)
.build()
.awaitReady();
}
private void registerLavalinkNodes() {
List.of(
/*client.addNode(
"Testnode",
URI.create("ws://localhost:2333"),
"youshallnotpass",
RegionGroup.EUROPE
),*/
client.addNode(new NodeOptions.Builder().setName("Mac-mini")
.setServerUri(URI.create("ws://mac-mini.local.duncte123.lgbt:2333/bepis"))
.setPassword("youshallnotpass")
.setRegionFilter(RegionGroup.US)
.setHttpTimeout(5000L)
.build()
)
).forEach((node) -> {
node.on(TrackStartEvent.class).subscribe((event) -> {
final LavalinkNode node1 = event.getNode();
System.out.printf(
"%s: track started: %s%n",
node1.getName(),
event.getTrack().getInfo()
);
});
});
}
private void registerLavalinkListeners() {
this.client.on(dev.arbjerg.lavalink.client.event.ReadyEvent.class).subscribe((event) -> {
final LavalinkNode node = event.getNode();
System.out.printf(
"Node '%s' is ready, session id is '%s'!%n",
node.getName(),
event.getSessionId()
);
});
this.client.on(StatsEvent.class).subscribe((event) -> {
final LavalinkNode node = event.getNode();
System.out.printf(
"Node '%s' has stats, current players: %d/%d%n",
node.getName(),
event.getPlayingPlayers(),
event.getPlayers()
);
});
this.client.on(EmittedEvent.class).subscribe((event) -> {
if (event instanceof TrackStartEvent) {
System.out.println("Is a track start event!");
}
final var node = event.getNode();
System.out.printf(
"Node '%s' emitted event: %s%n",
node.getName(),
event
);
});
}
@Override
public void onReady(@NotNull ReadyEvent event) {
System.out.println(event.getJDA().getSelfUser().getAsTag() + " is ready!");
event.getJDA().updateCommands()
.addCommands(
Commands.slash("custom-request", "Testing custom requests"),
Commands.slash("custom-json-request", "Testing custom json requests"),
Commands.slash("join", "Join the voice channel you are in."),
Commands.slash("leave", "Leaves the vc"),
Commands.slash("stop", "Stops the current track"),
Commands.slash("pause", "Pause or unpause the plauer"),
Commands.slash("play", "Play a song")
.addOption(
OptionType.STRING,
"identifier",
"The identifier of the song you want to play",
true
)
)
.queue();
}
@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
switch (event.getFullCommandName()) {
case "join":
joinHelper(event);
break;
case "stop":
this.client.getOrCreateLink(event.getGuild().getIdLong())
.updatePlayer(
(update) -> update.setTrack(null).setPaused(false)
)
.subscribe((__) -> {
event.reply("Stopped the current track").queue();
});
break;
case "leave":
event.getJDA().getDirectAudioController().disconnect(event.getGuild());
event.reply("Leaving your channel!").queue();
break;
case "pause":
this.client.getOrCreateLink(event.getGuild().getIdLong())
.getPlayer()
.flatMap((player) -> player.setPaused(!player.getPaused()))
.subscribe((player) -> {
event.reply("Player has been " + (player.getPaused() ? "paused" : "resumed") + "!").queue();
});
break;
case "play": {
final Guild guild = event.getGuild();
// We are already connected, go ahead and play
if (guild.getSelfMember().getVoiceState().inAudioChannel()) {
event.deferReply(false).queue();
} else {
// Connect to VC first
joinHelper(event);
}
final String identifier = event.getOption("identifier").getAsString();
final long guildId = guild.getIdLong();
final Link link = this.client.getOrCreateLink(guildId);
link.loadItem(identifier).subscribe(new FunctionalLoadResultHandler(
// Track loaded
(trackLoad) -> {
final Track track = trackLoad.getTrack();
// Inner class at the end of this file
final var userData = new MyUserData(event.getUser().getIdLong());
track.setUserData(userData);
// there are a few ways of updating the player! Just pick whatever you prefer
if (new Random().nextBoolean()) {
link.getPlayer()
.flatMap(
(p) -> p.setTrack(track).setVolume(35)
)
.subscribe((player) -> {
final Track playingTrack = player.getTrack();
final var trackTitle = playingTrack.getInfo().getTitle();
final MyUserData customData = playingTrack.getUserData(MyUserData.class);
event.getHook().sendMessage("Now playing: " + trackTitle + "\nRequested by: <@" + customData.requester() + '>').queue();
});
} else {
link.createOrUpdatePlayer()
.setTrack(track)
.setVolume(35)
.subscribe((player) -> {
final Track playingTrack = player.getTrack();
final var trackTitle = playingTrack.getInfo().getTitle();
final MyUserData customData = playingTrack.getUserData(MyUserData.class);
event.getHook().sendMessage("Now playing: " + trackTitle + "\nRequested by: <@" + customData.requester() + '>').queue();
});
}
},
null, // playlist loaded
// search result loaded
(search) -> {
final List<Track> tracks = search.getTracks();
if (tracks.isEmpty()) {
event.getHook().sendMessage("No tracks found!").queue();
return;
}
final Track firstTrack = tracks.get(0);
// This is a different way of updating the player! Choose your preference!
// This method will also create a player if there is not one in the server yet
link.updatePlayer((update) -> update.setTrack(firstTrack).setVolume(35))
.subscribe((ignored) -> {
event.getHook().sendMessage("Now playing: " + firstTrack.getInfo().getTitle()).queue();
});
},
null, // no matches
null // load failed
));
break;
}
case "custom-request": {
final Link link = this.client.getOrCreateLink(event.getGuild().getIdLong());
link.getNode().customRequest(
(builder) -> builder.get().path("/version").header("Accept", "text/plain")
).subscribe((response) -> {
try (ResponseBody body = response.body()) {
final String bodyText = body.string();
event.reply("Response from version endpoint (with custom request): " + bodyText).queue();
} catch (IOException e) {
event.reply("Something went wrong! " + e.getMessage()).queue();
}
});
break;
}
case "custom-json-request": {
final Link link = this.client.getOrCreateLink(event.getGuild().getIdLong());
link.getNode().customJsonRequest(com.github.topi314.lavasearch.protocol.SearchResult.Companion.serializer(),
(builder) -> builder.path("/v4/loadsearch?query=ytsearch%3Anever%20gonna%20give%20you%20up").get()
).doOnSuccess((loadResult -> {
if (loadResult == null) {
event.reply("No load result!").queue();
return;
}
event.reply("Response from loadsearch endpoint."
+ "\ntracks: " + loadResult.getTracks().size()
+ "\nalbums: " + loadResult.getAlbums().size()
+ "\nartists: " + loadResult.getArtists().size()
+ "\nplaylists: " + loadResult.getPlaylists().size()
+ "\ntexts: " + loadResult.getTexts().size()
).queue();
})).subscribe();
break;
}
default:
event.reply("Unknown command???").queue();
break;
}
}
// Makes sure that the bot is in a voice channel!
private void joinHelper(SlashCommandInteractionEvent event) {
final Member member = event.getMember();
final GuildVoiceState memberVoiceState = member.getVoiceState();
if (memberVoiceState.inAudioChannel()) {
event.getJDA().getDirectAudioController().connect(memberVoiceState.getChannel());
}
event.reply("Joining your channel!").queue();
}
record MyUserData(long requester) {}
}