Skip to content

Commit 0fac86e

Browse files
committed
Fix latest channel being null
1 parent 07d0413 commit 0fac86e

File tree

3 files changed

+60
-67
lines changed

3 files changed

+60
-67
lines changed

bot/src/main/java/me/duncte123/skybot/audio/GuildMusicManager.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import me.duncte123.skybot.utils.GuildSettingsUtils;
2424
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
2525

26-
import javax.annotation.Nullable;
26+
import javax.annotation.Nonnull;
27+
import java.util.Optional;
2728
import java.util.concurrent.atomic.AtomicLong;
2829
import java.util.function.Supplier;
2930

@@ -64,15 +65,17 @@ public void stopAndClear() {
6465
this.scheduler.getQueue().clear();
6566
}
6667

67-
@Nullable
68-
public MessageChannel getLatestChannel() {
68+
@Nonnull
69+
public Optional<MessageChannel> getLatestChannel() {
6970
final long last = this.getLatestChannelId();
7071

7172
if (last == -1 || last == 0) {
72-
return null;
73+
return Optional.empty();
7374
}
7475

75-
return SkyBot.getInstance().getShardManager().getChannelById(MessageChannel.class, last);
76+
return Optional.ofNullable(
77+
SkyBot.getInstance().getShardManager().getChannelById(MessageChannel.class, last)
78+
);
7679
}
7780

7881
public LocalPlayer getPlayer() {

bot/src/main/java/me/duncte123/skybot/audio/TrackScheduler.java

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import me.duncte123.skybot.extensions.AudioTrackKt;
2828
import me.duncte123.skybot.objects.TrackUserData;
2929
import me.duncte123.skybot.utils.Debouncer;
30-
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
3130
import net.dv8tion.jda.api.exceptions.ErrorHandler;
3231
import org.slf4j.Logger;
3332
import org.slf4j.LoggerFactory;
@@ -56,17 +55,12 @@ public class TrackScheduler {
5655
/* package */ TrackScheduler(GuildMusicManager guildMusicManager) {
5756
this.guildMusicManager = guildMusicManager;
5857
this.messageDebouncer = new Debouncer<>((msg) -> {
59-
final MessageChannel latestChannel = guildMusicManager.getLatestChannel();
60-
61-
if (latestChannel == null) {
62-
return;
63-
}
64-
65-
sendMsg(new MessageConfig.Builder()
66-
.setChannel(latestChannel)
67-
.setMessage(msg)
68-
.setFailureAction(new ErrorHandler().ignore(UNKNOWN_CHANNEL, MISSING_PERMISSIONS))
69-
.build());
58+
guildMusicManager.getLatestChannel()
59+
.ifPresent((latestChannel) -> sendMsg(new MessageConfig.Builder()
60+
.setChannel(latestChannel)
61+
.setMessage(msg)
62+
.setFailureAction(new ErrorHandler().ignore(UNKNOWN_CHANNEL, MISSING_PERMISSIONS))
63+
.build()));
7064
}, DEBOUNCE_INTERVAL);
7165
}
7266

@@ -92,10 +86,10 @@ public void addToQueue(Track track, boolean isPatron) throws LimitReachedExcepti
9286

9387
/**
9488
*
95-
* @param tracks
96-
* @param isPatron
89+
* @param tracks The tracks to be added to the queue.
90+
* @param isPatron if this user is a supporter of the bot.
9791
* @return The total amount of tracks that were added to the queue.
98-
* @throws LimitReachedException
92+
* @throws LimitReachedException When the free limit has been reached.
9993
*/
10094
public int queuePlaylistTracks(List<Track> tracks, boolean isPatron) throws LimitReachedException {
10195
final List<Track> tmpQueue = new ArrayList<>();
@@ -136,11 +130,13 @@ public void skipTracks(int count, boolean forceAnnounce) {
136130

137131
if (nextTrack == null) {
138132
this.guildMusicManager.getPlayer().stopPlayback();
139-
sendMsg(
140-
new MessageConfig.Builder()
141-
.setChannel(guildMusicManager.getLatestChannel())
142-
.setMessage("Queue concluded")
143-
);
133+
134+
guildMusicManager.getLatestChannel()
135+
.ifPresent((channel) -> sendMsg(
136+
new MessageConfig.Builder()
137+
.setChannel(channel)
138+
.setMessage("Queue concluded")
139+
));
144140
} else {
145141
// Make sure to cary over the skip state, we want to announce skipped tracks
146142
getUserData(nextTrack).setForceAnnounce(forceAnnounce);
@@ -158,21 +154,22 @@ public void onTrackStart(Track track) {
158154
data.setForceAnnounce(false);
159155
}
160156

161-
AudioTrackKt.toEmbed(
162-
track,
163-
this.guildMusicManager,
164-
getInstance().getShardManager(),
165-
false,
166-
(message) -> {
167-
sendMsg(
168-
new MessageConfig.Builder()
169-
.setChannel(guildMusicManager.getLatestChannel())
170-
.setEmbeds(false, message)
171-
);
172-
173-
return null;
174-
}
175-
);
157+
guildMusicManager.getLatestChannel()
158+
.ifPresent((latestChannel) -> AudioTrackKt.toEmbed(
159+
track,
160+
this.guildMusicManager,
161+
getInstance().getShardManager(),
162+
false,
163+
(message) -> {
164+
sendMsg(
165+
new MessageConfig.Builder()
166+
.setChannel(latestChannel)
167+
.setEmbeds(false, message)
168+
);
169+
170+
return null;
171+
}
172+
));
176173
}
177174
}
178175

bot/src/main/java/me/duncte123/skybot/listeners/GuildListener.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,15 @@ public GuildListener(Variables variables) {
5757

5858
@Override
5959
public void onEvent(@Nonnull GenericEvent event) {
60-
if (event instanceof GuildJoinEvent guildJoin) {
61-
this.onGuildJoin(guildJoin);
62-
} else if (event instanceof GuildLeaveEvent guildLeave) {
63-
this.onGuildLeave(guildLeave);
64-
} else if (event instanceof GuildVoiceUpdateEvent guildVoiceMove) {
65-
this.onGuildVoiceMove(guildVoiceMove);
66-
} else if (event instanceof GuildBanEvent guildBan) {
67-
this.onGuildBan(guildBan);
68-
} else if (event instanceof GuildUnbanEvent guildUnban) {
69-
this.onGuildUnban(guildUnban);
70-
} else if (event instanceof GuildReadyEvent guildReady) {
71-
this.onGuildReady(guildReady);
60+
switch (event) {
61+
case GuildJoinEvent guildJoin -> this.onGuildJoin(guildJoin);
62+
case GuildLeaveEvent guildLeave -> this.onGuildLeave(guildLeave);
63+
case GuildVoiceUpdateEvent guildVoiceMove -> this.onGuildVoiceMove(guildVoiceMove);
64+
case GuildBanEvent guildBan -> this.onGuildBan(guildBan);
65+
case GuildUnbanEvent guildUnban -> this.onGuildUnban(guildUnban);
66+
case GuildReadyEvent guildReady -> this.onGuildReady(guildReady);
67+
default -> {
68+
}
7269
}
7370
}
7471

@@ -333,22 +330,18 @@ private void requestToSpeak(Guild guild, Member self, AudioChannel channel) {
333330
guild.requestToSpeak();
334331
} else {
335332
final GuildMusicManager musicManager = this.variables.getAudioUtils().getMusicManager(guild.getIdLong());
336-
final MessageChannel textChan = musicManager.getLatestChannel();
337-
338-
if (textChan == null) {
339-
return;
340-
}
341333

342-
sendMsg(
343-
new MessageConfig.Builder()
344-
.setChannel(textChan)
345-
.setMessageFormat(
346-
"In order for stage channels to work properly, I need to be able to request to speak.\n" +
347-
"Alternatively, you could give me the %s permission so I can unmute myself or invite me to speak on this stage.",
348-
Permission.VOICE_MUTE_OTHERS.getName()
349-
)
350-
.build()
351-
);
334+
musicManager.getLatestChannel()
335+
.ifPresent((textChan) -> sendMsg(
336+
new MessageConfig.Builder()
337+
.setChannel(textChan)
338+
.setMessageFormat(
339+
"In order for stage channels to work properly, I need to be able to request to speak.\n" +
340+
"Alternatively, you could give me the %s permission so I can unmute myself or invite me to speak on this stage.",
341+
Permission.VOICE_MUTE_OTHERS.getName()
342+
)
343+
.build()
344+
));
352345
}
353346
}
354347
}

0 commit comments

Comments
 (0)