-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinToCreateVCListener.java
More file actions
73 lines (61 loc) · 2.84 KB
/
JoinToCreateVCListener.java
File metadata and controls
73 lines (61 loc) · 2.84 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
package listeners;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import java.util.HashMap;
import java.util.Map;
public class JoinToCreateVCListener extends ListenerAdapter {
private final Map<Long, VoiceChannel> userToVCMap = new HashMap<>();
private int channelCount = 0;
@Override
public void onGuildVoiceUpdate(GuildVoiceUpdateEvent event) {
User user = event.getMember().getUser();
AudioChannel oldChannel = event.getOldValue();
// channel ID for Join to Create channel
long joinToCreateChannelId = 1096110594077237399L;
try {
if (event.getChannelJoined().getIdLong() == joinToCreateChannelId) {
Guild guild = event.getGuild();
VoiceChannel vc = userToVCMap.get(user.getIdLong());
if (vc != null) {
// User already has a temporary VC, move them to it
} else {
// Create new temporary VC
vc = createNewVC(guild);
// Move user to new VC
guild.moveVoiceMember(event.getMember(), vc).queue();
// Add user to VC map
userToVCMap.put(user.getIdLong(), vc);
}
}
} catch (NullPointerException ignoredError) {
}
if (oldChannel != null) {
if (oldChannel.equals(userToVCMap.get(user.getIdLong()))) {
userToVCMap.remove(user.getIdLong());
}
if (oldChannel.getParentCategory().getId().equals("1086418807293219006")
&& !oldChannel.getId().equals("1096110594077237399") && !oldChannel.getId().equals("1086419539153137747")
&& !oldChannel.getId().equals("1086419583050723418") && oldChannel.getMembers().isEmpty()) {
event.getChannelLeft().delete().queue();
channelCount--;
}
}
}
private VoiceChannel createNewVC(Guild guild) {
// Increment channel count and create new VC with incremented name
channelCount++;
// category ID for Join to Create VCs
long CATEGORY_ID = 1086418807293219006L;
VoiceChannel vc = guild.createVoiceChannel("VC " + channelCount)
.setParent(guild.getCategoryById(CATEGORY_ID))
.complete();
// Set maximum number of members for the VC
int maxMembersPerVC = 10;
vc.getManager().setUserLimit(maxMembersPerVC).queue();
return vc;
}
}