Skip to content

Commit a62f773

Browse files
tj-wazeisurajkumar
andauthored
fix: DynamicVoiceChat throwing an exception when event occurs in non-voice channel (#1391)
Co-authored-by: Suraj Kumar <sk96.uk@gmail.com>
1 parent 9f46d3c commit a62f773

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

application/src/main/java/org/togetherjava/tjbot/features/voicechat/DynamicVoiceChat.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.entities.Guild;
55
import net.dv8tion.jda.api.entities.Member;
6-
import net.dv8tion.jda.api.entities.MessageEmbed;
7-
import net.dv8tion.jda.api.entities.MessageHistory;
86
import net.dv8tion.jda.api.entities.User;
7+
import net.dv8tion.jda.api.entities.channel.ChannelType;
98
import net.dv8tion.jda.api.entities.channel.concrete.Category;
109
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
1110
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
1211
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
1312
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
1413
import net.dv8tion.jda.api.managers.channel.middleman.AudioChannelManager;
1514
import net.dv8tion.jda.api.requests.RestAction;
16-
import org.jetbrains.annotations.NotNull;
1715
import org.slf4j.Logger;
1816
import org.slf4j.LoggerFactory;
1917

@@ -51,7 +49,7 @@ public DynamicVoiceChat(Config config) {
5149
}
5250

5351
@Override
54-
public void onVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
52+
public void onVoiceUpdate(GuildVoiceUpdateEvent event) {
5553
Member member = event.getMember();
5654
User user = member.getUser();
5755

@@ -62,18 +60,28 @@ public void onVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
6260
AudioChannelUnion channelJoined = event.getChannelJoined();
6361
AudioChannelUnion channelLeft = event.getChannelLeft();
6462

65-
if (channelJoined != null && eventHappenOnDynamicRootChannel(channelJoined)) {
63+
if (channelJoined != null && isVoiceChannel(channelJoined)) {
64+
handleVoiceChannelJoin(event, channelJoined);
65+
}
66+
67+
if (channelLeft != null && isVoiceChannel(channelLeft)) {
68+
handleVoiceChannelLeave(channelLeft);
69+
}
70+
}
71+
72+
private void handleVoiceChannelJoin(GuildVoiceUpdateEvent event,
73+
AudioChannelUnion channelJoined) {
74+
if (eventHappenOnDynamicRootChannel(channelJoined)) {
6675
logger.debug("Event happened on joined channel {}", channelJoined);
6776
createDynamicVoiceChannel(event, channelJoined.asVoiceChannel());
6877
}
78+
}
6979

70-
if (channelLeft != null && !eventHappenOnDynamicRootChannel(channelLeft)) {
80+
private void handleVoiceChannelLeave(AudioChannelUnion channelLeft) {
81+
if (!eventHappenOnDynamicRootChannel(channelLeft)) {
7182
logger.debug("Event happened on left channel {}", channelLeft);
7283

73-
MessageHistory messageHistory = channelLeft.asVoiceChannel().getHistory();
74-
messageHistory.retrievePast(2).queue(messages -> {
75-
// Don't forget that there is always one
76-
// embed message sent by the bot every time.
84+
channelLeft.asVoiceChannel().getHistory().retrievePast(2).queue(messages -> {
7785
if (messages.size() > 1) {
7886
archiveDynamicVoiceChannel(channelLeft);
7987
} else {
@@ -89,8 +97,7 @@ private boolean eventHappenOnDynamicRootChannel(AudioChannelUnion channel) {
8997
.anyMatch(pattern -> pattern.matcher(channel.getName()).matches());
9098
}
9199

92-
private void createDynamicVoiceChannel(@NotNull GuildVoiceUpdateEvent event,
93-
VoiceChannel channel) {
100+
private void createDynamicVoiceChannel(GuildVoiceUpdateEvent event, VoiceChannel channel) {
94101
Guild guild = event.getGuild();
95102
Member member = event.getMember();
96103
String newChannelName = "%s's %s".formatted(member.getEffectiveName(), channel.getName());
@@ -140,11 +147,8 @@ private void archiveDynamicVoiceChannel(AudioChannelUnion channel) {
140147
.and(channel.getPermissionContainer().getManager().clearOverridesAdded());
141148

142149
if (archiveCategoryOptional.isEmpty()) {
143-
logger.warn("Could not find archive category. Attempting to create one...");
144-
channel.getGuild()
145-
.createCategory(dynamicVoiceChannelConfig.archiveCategoryPattern())
146-
.queue(newCategory -> restActionChain.and(channelManager.setParent(newCategory))
147-
.queue());
150+
logger.error("Could not find category matching {}",
151+
dynamicVoiceChannelConfig.archiveCategoryPattern());
148152
return;
149153
}
150154

@@ -155,17 +159,22 @@ private void archiveDynamicVoiceChannel(AudioChannelUnion channel) {
155159
err -> logger.error("Could not archive dynamic voice chat", err)));
156160
}
157161

158-
private void sendWarningEmbed(VoiceChannel channel) {
159-
MessageEmbed messageEmbed = new EmbedBuilder()
160-
.addField("👋 Heads up!",
161-
"""
162-
This is a **temporary** voice chat channel. Messages sent here will be *cleared* once \
163-
the channel is deleted when everyone leaves. If you need to keep something important, \
164-
make sure to save it elsewhere. 💬
165-
""",
166-
false)
167-
.build();
168-
169-
channel.sendMessageEmbeds(messageEmbed).queue();
162+
private static void sendWarningEmbed(VoiceChannel channel) {
163+
channel
164+
.sendMessageEmbeds(
165+
new EmbedBuilder()
166+
.addField("👋 Heads up!",
167+
"""
168+
This is a **temporary** voice chat channel. Messages sent here will be *cleared* once \
169+
the channel is deleted when everyone leaves. If you need to keep something important, \
170+
make sure to save it elsewhere. 💬
171+
""",
172+
false)
173+
.build())
174+
.queue();
175+
}
176+
177+
private static boolean isVoiceChannel(AudioChannelUnion channel) {
178+
return channel.getType() == ChannelType.VOICE;
170179
}
171180
}

0 commit comments

Comments
 (0)