Skip to content

Commit 50b4b7d

Browse files
committed
Small updates to fix warnings. Updated how we grab random numbers to be thread safe.
1 parent 205f93c commit 50b4b7d

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
projectVersion=4.1.50
1+
projectVersion=4.1.51
22
org.gradle.configuration-cache=false

src/main/java/net/dirtydeeds/discordsoundboard/SoundPlayer.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.nio.file.*;
3636
import java.time.ZonedDateTime;
3737
import java.util.*;
38+
import java.util.concurrent.ThreadLocalRandom;
3839
import java.util.concurrent.atomic.AtomicReference;
3940
import java.util.stream.Collectors;
4041
import java.util.stream.StreamSupport;
@@ -212,8 +213,7 @@ public SoundFile playRandomSoundFile(String User, MessageReceivedEvent event, St
212213
try {
213214
Map<String, SoundFile> sounds = getAvailableSoundFiles();
214215
List<String> keysAsArray = new ArrayList<>(sounds.keySet());
215-
Random r = new Random();
216-
SoundFile randomValue = sounds.get(keysAsArray.get(r.nextInt(keysAsArray.size())));
216+
SoundFile randomValue = sounds.get(keysAsArray.get(ThreadLocalRandom.current().nextInt(keysAsArray.size())));
217217

218218
LOG.info("Attempting to play random file: {}, requested by : {}", randomValue.getSoundFileId(), requestingUser);
219219
try {
@@ -371,11 +371,7 @@ private void playFile(String fileName, Guild guild, Integer repeatTimes, String
371371
*/
372372
public String stop(String user, String voiceChannelId) {
373373
Guild guild = getGuildForUserOrChannelId(user, voiceChannelId);
374-
String soundFileId = stopPlayback(guild);
375-
376-
if (soundFileId != null) return soundFileId;
377-
378-
return null;
374+
return stopPlayback(guild);
379375
}
380376

381377
@Nullable
@@ -482,7 +478,12 @@ public net.dv8tion.jda.api.entities.User retrieveUserById(String idOrName) {
482478
try {
483479
return bot.retrieveUserById(idOrName).complete();
484480
} catch (Exception e) {
485-
return bot.getUsersByName(idOrName, true).get(0);
481+
try {
482+
return bot.getUsersByName(idOrName, true).getFirst();
483+
} catch (Exception ex) {
484+
LOG.error(ex.getMessage(), ex);
485+
return null;
486+
}
486487
}
487488
}
488489

src/main/java/net/dirtydeeds/discordsoundboard/listeners/MovedChannelListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
9797
LOG.debug("Could not find entrance or disconnect sound for {}, so ignoring GuildVoiceMoveEvent.", users);
9898
}
9999
}
100+
if (isAlone(event.getGuild()) && botConfig.isLeaveOnEmptyChannel()) {
101+
soundPlayer.disconnectFromChannel(event.getGuild());
102+
}
100103
soundPlayer.updateUsersInDb();
101104
discordUserController.broadcastUpdate();
102105
}

0 commit comments

Comments
 (0)