Skip to content

Commit 56dc8ae

Browse files
committed
Merge branch 'feature/jdabeta5' into master
# Conflicts: # build.gradle # gradle.properties
2 parents 3adb374 + 6d11286 commit 56dc8ae

File tree

15 files changed

+139
-121
lines changed

15 files changed

+139
-121
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ tasks.named("bootJar") {
5858
}
5959

6060
dependencies {
61-
implementation 'net.dv8tion:JDA:4.4.1_353'
61+
implementation 'net.dv8tion:JDA:5.0.0-beta.6'
6262
implementation 'com.github.walkyst:lavaplayer-fork:1.3.97'
6363
implementation 'com.github.aikaterna:lavaplayer-natives:original-SNAPSHOT'
6464
implementation 'pw.chew:jda-chewtils:1.24.1'

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import net.dv8tion.jda.api.JDA;
77
import net.dv8tion.jda.api.JDABuilder;
88
import net.dv8tion.jda.api.entities.Activity;
9+
import net.dv8tion.jda.api.requests.GatewayIntent;
10+
import net.dv8tion.jda.api.utils.MemberCachePolicy;
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
1113
import org.springframework.util.ObjectUtils;
@@ -33,25 +35,28 @@ public JDABot(BotConfig botConfig) {
3335
return;
3436
}
3537

36-
jda = JDABuilder.createDefault(botToken)
38+
jda = JDABuilder.createDefault(botToken, GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MEMBERS,
39+
GatewayIntent.MESSAGE_CONTENT, GatewayIntent.DIRECT_MESSAGES,
40+
GatewayIntent.GUILD_VOICE_STATES)
41+
.setMemberCachePolicy(MemberCachePolicy.ALL)
3742
.setAutoReconnect(true)
3843
.addEventListeners(new OnReadyListener(this))
3944
.build();
4045
jda.awaitReady();
4146

4247
String activityString = botConfig.getActivityString();
4348
if (ObjectUtils.isEmpty(activityString)) {
44-
jda.getPresence().setActivity(Activity.of(Activity.ActivityType.DEFAULT,
49+
jda.getPresence().setActivity(Activity.of(Activity.ActivityType.PLAYING,
4550
"Type " + botConfig.getCommandCharacter() + "help for a list of commands."));
4651
} else {
47-
jda.getPresence().setActivity(Activity.of(Activity.ActivityType.DEFAULT, activityString));
52+
jda.getPresence().setActivity(Activity.of(Activity.ActivityType.PLAYING, activityString));
4853
}
4954

5055
} catch (IllegalArgumentException e) {
5156
LOG.warn("The config was not populated. Please enter an email and password.");
52-
} catch (
53-
LoginException e) {
54-
LOG.warn("The provided bot token was incorrect. Please provide valid details.");
57+
// } catch (
58+
// LoginException e) {
59+
// LOG.warn("The provided bot token was incorrect. Please provide valid details.");
5560
} catch (InterruptedException e) {
5661
LOG.error("Login Interrupted.");
5762
}

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import net.dirtydeeds.discordsoundboard.util.ShutdownManager;
1313
import net.dv8tion.jda.api.JDA;
1414
import net.dv8tion.jda.api.entities.*;
15+
import net.dv8tion.jda.api.entities.channel.ChannelType;
16+
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
1517
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
1618
import net.dv8tion.jda.api.managers.AudioManager;
1719
import org.apache.commons.lang3.StringUtils;
@@ -75,6 +77,8 @@ private void init() {
7577
return;
7678
}
7779

80+
bot.getGuilds().forEach(Guild::loadMembers);
81+
7882
updateFileList();
7983
updateUsersInDb();
8084

@@ -221,7 +225,7 @@ public void playForUser(String fileName, String userName, Integer repeatTimes, S
221225
* @param fileName - The name of the file to play.
222226
* @param channel - The channel to play the file in
223227
*/
224-
public void playFileInChannel(String fileName, VoiceChannel channel) {
228+
public void playFileInChannel(String fileName, AudioChannel channel) {
225229
if (channel == null) return;
226230
moveToChannel(channel, channel.getGuild());
227231
LOG.info("Playing file for user: " + fileName + " in channel: " + channel.getName());
@@ -323,8 +327,12 @@ public void updateUsersInDb() {
323327
userService.saveAll(users);
324328
}
325329

326-
public net.dv8tion.jda.api.entities.User retrieveUserById(String id) {
327-
return bot.retrieveUserById(id).complete();
330+
public net.dv8tion.jda.api.entities.User retrieveUserById(String idOrName) {
331+
try {
332+
return bot.retrieveUserById(idOrName).complete();
333+
} catch (Exception e) {
334+
return bot.getUsersByName(idOrName, true).get(0);
335+
}
328336
}
329337

330338
public boolean isUserAllowed(String username) {
@@ -418,7 +426,7 @@ private Guild getGuildForUserOrChannelId(String userName, String voiceChannelId)
418426
if (!botConfig.isControlByChannel() || StringUtils.isBlank(voiceChannelId)
419427
|| voiceChannelId.equals("undefined")) {
420428
for (Guild guild : bot.getGuilds()) {
421-
for (VoiceChannel channel : guild.getVoiceChannels()) {
429+
for (AudioChannel channel : guild.getVoiceChannels()) {
422430
for (Member user : channel.getMembers()) {
423431
if (user.getEffectiveName().equalsIgnoreCase(userName)
424432
|| user.getUser().getName().equalsIgnoreCase(userName)
@@ -441,7 +449,7 @@ private Guild getGuildForUserOrChannelId(String userName, String voiceChannelId)
441449
* @param event - The event
442450
*/
443451
private void moveToUserIdsChannel(MessageReceivedEvent event, Guild guild) {
444-
VoiceChannel channel = findUsersChannel(event, guild);
452+
AudioChannel channel = findUsersChannel(event, guild);
445453

446454
if (channel == null) {
447455
event.getAuthor().openPrivateChannel().complete()
@@ -460,7 +468,7 @@ private void joinUsersCurrentChannel(String userName, String voiceChannelId) {
460468
moveToChannel(bot.getVoiceChannelById(voiceChannelId), Objects.requireNonNull(bot.getVoiceChannelById(voiceChannelId)).getGuild());
461469
} else {
462470
for (Guild guild : bot.getGuilds()) {
463-
for (VoiceChannel channel : guild.getVoiceChannels()) {
471+
for (AudioChannel channel : guild.getVoiceChannels()) {
464472
for (Member user : channel.getMembers()) {
465473
if (user.getEffectiveName().equalsIgnoreCase(userName)
466474
|| user.getUser().getName().equalsIgnoreCase(userName)
@@ -478,7 +486,7 @@ private void joinUsersCurrentChannel(String userName, String voiceChannelId) {
478486
*
479487
* @param channel - The channel specified.
480488
*/
481-
private void moveToChannel(VoiceChannel channel, Guild guild) {
489+
private void moveToChannel(AudioChannel channel, Guild guild) {
482490
AudioManager audioManager = guild.getAudioManager();
483491

484492
audioManager.openAudioConnection(channel);
@@ -510,11 +518,11 @@ private void moveToChannel(VoiceChannel channel, Guild guild) {
510518
* @param guild - The guild (discord server) to look in for the author.
511519
* @return The VoiceChannel if one is found. Otherwise, return null.
512520
*/
513-
private VoiceChannel findUsersChannel(MessageReceivedEvent event, Guild guild) {
514-
VoiceChannel channel = null;
521+
private AudioChannel findUsersChannel(MessageReceivedEvent event, Guild guild) {
522+
AudioChannel channel = null;
515523

516524
outerLoop:
517-
for (VoiceChannel channel1 : guild.getVoiceChannels()) {
525+
for (AudioChannel channel1 : guild.getVoiceChannels()) {
518526
for (Member user : channel1.getMembers()) {
519527
if (user.getId().equals(event.getAuthor().getId())) {
520528
channel = channel1;

src/main/java/net/dirtydeeds/discordsoundboard/beans/SoundFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package net.dirtydeeds.discordsoundboard.beans;
22

33
import lombok.*;
4+
import org.springframework.lang.Nullable;
45

5-
import javax.annotation.Nullable;
66
import javax.persistence.Entity;
77
import javax.persistence.Id;
88
import java.time.ZonedDateTime;

src/main/java/net/dirtydeeds/discordsoundboard/commands/EntranceCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ protected void execute(CommandEvent event) {
6666
event.replyByPrivateMessage("Could not find user with id or name: " + userNameOrId);
6767
}
6868
} else {
69-
//TODO give a better message. It's likely they don't have permission in this case
70-
event.replyByPrivateMessage("Entrance command incorrect. Required input is " +
69+
event.replyByPrivateMessage("You must have admin on the server to edit entrance for other users " +
7170
event.getPrefix() + "entrance <userid/username> <soundFile>");
7271
}
7372
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import net.dirtydeeds.discordsoundboard.BotConfig;
44
import net.dirtydeeds.discordsoundboard.commands.Command;
55
import net.dirtydeeds.discordsoundboard.commands.CommandEvent;
6-
import net.dv8tion.jda.api.entities.ChannelType;
6+
import net.dv8tion.jda.api.entities.channel.ChannelType;
77
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
88
import net.dv8tion.jda.api.exceptions.PermissionException;
99
import net.dv8tion.jda.api.hooks.ListenerAdapter;

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import net.dirtydeeds.discordsoundboard.SoundPlayer;
77
import net.dirtydeeds.discordsoundboard.service.SoundService;
88
import net.dirtydeeds.discordsoundboard.service.UserService;
9-
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
9+
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
1010
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1111
import org.h2.util.StringUtils;
1212
import org.jetbrains.annotations.NotNull;
@@ -40,32 +40,34 @@ public EntranceSoundBoardListener(SoundPlayer bot, UserService userService,
4040
}
4141

4242
@Override
43-
public void onGuildVoiceJoin(@NotNull GuildVoiceJoinEvent event) {
44-
if (playEntranceOnJoin && !event.getMember().getUser().isBot()) {
45-
String userJoined = event.getMember().getEffectiveName();
46-
String userId = event.getMember().getId();
43+
public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
44+
if (event.getChannelLeft() == null && event.getChannelJoined() != null) {
45+
if (playEntranceOnJoin && !event.getMember().getUser().isBot()) {
46+
String userJoined = event.getMember().getEffectiveName();
47+
String userId = event.getMember().getId();
4748

48-
User user = userService.findOneByIdOrUsernameIgnoreCase(userId, userJoined);
49-
if (user != null) {
50-
if (!StringUtils.isNullOrEmpty(user.getEntranceSound())) {
51-
String entranceSound = user.getEntranceSound();
52-
LOG.info(String.format("Playing entrance sound %s", entranceSound));
53-
bot.playFileInChannel(entranceSound, event.getChannelJoined());
54-
} else if (!StringUtils.isNullOrEmpty(botConfig.getEntranceForAll())) {
55-
LOG.info(String.format("Playing entrance for all sound %s", botConfig.getEntranceForAll()));
56-
bot.playFileInChannel(botConfig.getEntranceForAll(), event.getChannelJoined());
57-
} else {
58-
//If DB doesn't have an entrance sound check for a file with the same name as the user
59-
SoundFile entranceFile = soundService.findOneBySoundFileIdIgnoreCase(user.getUsername());
60-
if (entranceFile != null) {
61-
try {
62-
bot.playFileInChannel(entranceFile.getSoundFileId(), event.getChannelJoined());
63-
LOG.info(String.format("Playing entrance sound %s", entranceFile.getSoundFileId()));
64-
} catch (Exception e) {
65-
LOG.error("Could not play file for entrance of {}", userJoined);
66-
}
49+
User user = userService.findOneByIdOrUsernameIgnoreCase(userId, userJoined);
50+
if (user != null) {
51+
if (!StringUtils.isNullOrEmpty(user.getEntranceSound())) {
52+
String entranceSound = user.getEntranceSound();
53+
LOG.info(String.format("Playing entrance sound %s", entranceSound));
54+
bot.playFileInChannel(entranceSound, event.getChannelJoined());
55+
} else if (!StringUtils.isNullOrEmpty(botConfig.getEntranceForAll())) {
56+
LOG.info(String.format("Playing entrance for all sound %s", botConfig.getEntranceForAll()));
57+
bot.playFileInChannel(botConfig.getEntranceForAll(), event.getChannelJoined());
6758
} else {
68-
LOG.debug("Could not find any sound that starts with {}, so ignoring entrance.", userJoined);
59+
//If DB doesn't have an entrance sound check for a file with the same name as the user
60+
SoundFile entranceFile = soundService.findOneBySoundFileIdIgnoreCase(user.getUsername());
61+
if (entranceFile != null) {
62+
try {
63+
bot.playFileInChannel(entranceFile.getSoundFileId(), event.getChannelJoined());
64+
LOG.info(String.format("Playing entrance sound %s", entranceFile.getSoundFileId()));
65+
} catch (Exception e) {
66+
LOG.error("Could not play file for entrance of {}", userJoined);
67+
}
68+
} else {
69+
LOG.debug("Could not find any sound that starts with {}, so ignoring entrance.", userJoined);
70+
}
6971
}
7072
}
7173
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import net.dirtydeeds.discordsoundboard.BotConfig;
44
import net.dirtydeeds.discordsoundboard.util.BotUtils;
5-
import net.dv8tion.jda.api.entities.ChannelType;
65
import net.dv8tion.jda.api.entities.Message;
6+
import net.dv8tion.jda.api.entities.channel.ChannelType;
77
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
88
import net.dv8tion.jda.api.hooks.ListenerAdapter;
99
import org.jetbrains.annotations.NotNull;
@@ -48,7 +48,7 @@ private void addAttachment(@NotNull MessageReceivedEvent event) {
4848
if (attachment.getSize() < botConfig.getMaxFileSizeInBytes()) {
4949
if (!Files.exists(Paths.get(botConfig.getSoundFileDir() + "/" + fileName))) {
5050
File newSoundFile = new File(botConfig.getSoundFileDir(), fileName);
51-
attachment.downloadToFile(newSoundFile).getNow(null);
51+
attachment.getProxy().downloadToFile(newSoundFile).getNow(null);
5252
event.getChannel().sendMessage("Downloaded file `" + fileName + "` and added to list of sounds " + event.getAuthor().getAsMention() + ".").queue();
5353
} else {
5454
if (event.getMember() != null) {

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import net.dirtydeeds.discordsoundboard.SoundPlayer;
77
import net.dirtydeeds.discordsoundboard.service.SoundService;
88
import net.dirtydeeds.discordsoundboard.service.UserService;
9-
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
9+
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
1010
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1111
import org.h2.util.StringUtils;
1212
import org.slf4j.Logger;
@@ -35,25 +35,27 @@ public LeaveSoundBoardListener(SoundPlayer bot, UserService userService, SoundSe
3535
}
3636

3737
@Override
38-
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
39-
String userDisconnected = event.getMember().getEffectiveName();
40-
String userDisconnectedId = event.getMember().getId();
41-
User user = userService.findOneByIdOrUsernameIgnoreCase(userDisconnectedId, userDisconnected);
42-
if (user != null) {
43-
if (!StringUtils.isNullOrEmpty(user.getLeaveSound())) {
44-
bot.playFileInChannel(user.getLeaveSound(), event.getChannelLeft());
45-
} else {
46-
//If DB doesn't have a leave sound check for a file named with the userName + leave suffix
47-
SoundFile leaveFile = soundService.findOneBySoundFileIdIgnoreCase(
48-
user.getUsername() + botConfig.getLeaveSuffix());
49-
if (leaveFile != null) {
50-
try {
51-
bot.playFileInChannel(leaveFile.getSoundFileId(), event.getChannelLeft());
52-
} catch (Exception e) {
53-
LOG.error("Could not play file for disconnection of {}", userDisconnected);
54-
}
38+
public void onGuildVoiceUpdate(GuildVoiceUpdateEvent event) {
39+
if (event.getChannelJoined() == null && event.getChannelLeft() != null) {
40+
String userDisconnected = event.getMember().getEffectiveName();
41+
String userDisconnectedId = event.getMember().getId();
42+
User user = userService.findOneByIdOrUsernameIgnoreCase(userDisconnectedId, userDisconnected);
43+
if (user != null) {
44+
if (!StringUtils.isNullOrEmpty(user.getLeaveSound())) {
45+
bot.playFileInChannel(user.getLeaveSound(), event.getChannelLeft());
5546
} else {
56-
LOG.debug("Could not find disconnection sound for {}, so ignoring disconnection event.", userDisconnected);
47+
//If DB doesn't have a leave sound check for a file named with the userName + leave suffix
48+
SoundFile leaveFile = soundService.findOneBySoundFileIdIgnoreCase(
49+
user.getUsername() + botConfig.getLeaveSuffix());
50+
if (leaveFile != null) {
51+
try {
52+
bot.playFileInChannel(leaveFile.getSoundFileId(), event.getChannelLeft());
53+
} catch (Exception e) {
54+
LOG.error("Could not play file for disconnection of {}", userDisconnected);
55+
}
56+
} else {
57+
LOG.debug("Could not find disconnection sound for {}, so ignoring disconnection event.", userDisconnected);
58+
}
5759
}
5860
}
5961
}

0 commit comments

Comments
 (0)