From 80b91340496beee231b3d18160d63031f05198d6 Mon Sep 17 00:00:00 2001 From: Dave Furrer Date: Tue, 10 May 2016 22:18:44 -0500 Subject: [PATCH] Added code that takes an attached mp3 or wav file from received PM and adds it to the soundboard. --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +-- .../ChatSoundBoardListener.java | 23 +++++++++++++++-- .../service/SoundPlayerImpl.java | 25 ++++++++++++++++--- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 0eb3c32..04730bb 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'idea' apply plugin: 'distribution' apply plugin: 'spring-boot' -version = '1.3.2' +version = '1.4.0' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 54bf800..d959f70 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 04 09:06:00 CDT 2016 +#Tue May 10 20:35:45 CDT 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-all.zip diff --git a/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java b/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java index ae57079..64e2031 100644 --- a/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java +++ b/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java @@ -2,10 +2,12 @@ import net.dirtydeeds.discordsoundboard.beans.SoundFile; import net.dirtydeeds.discordsoundboard.service.SoundPlayerImpl; +import net.dv8tion.jda.entities.Message; import net.dv8tion.jda.events.message.MessageReceivedEvent; import net.dv8tion.jda.hooks.ListenerAdapter; import net.dv8tion.jda.utils.SimpleLog; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -24,6 +26,7 @@ public class ChatSoundBoardListener extends ListenerAdapter { private String commandCharacter = "?"; private Integer messageSizeLimit = 2000; private boolean muted; + private static final int MAX_FILE_SIZE_IN_BYTES = 1000000; // 1 MB public ChatSoundBoardListener(SoundPlayerImpl soundPlayer, String commandCharacter, String messageSizeLimit) { this.soundPlayer = soundPlayer; @@ -109,8 +112,24 @@ public void onMessageReceived(MessageReceivedEvent event) { LOG.info("Attempting to play a sound file while muted. Requested by " + requestingUser + "."); } } else { - if (message.startsWith(commandCharacter) || event.isPrivate()) { - nonRecognizedCommand(event, requestingUser); + List attachments = event.getMessage().getAttachments(); + if (attachments.size() > 0 && event.isPrivate()) { + for (Message.Attachment attachment : attachments) { + String name = attachment.getFileName(); + String extension = name.substring(name.indexOf(".") + 1); + if (extension.equals("wav") || extension.equals("mp3")) { + if (attachment.getSize() < MAX_FILE_SIZE_IN_BYTES) { + attachment.download(new File(soundPlayer.getSoundsPath(), name)); + event.getChannel().sendMessage("Downloaded file `" + name + "` and added to list of sounds " + event.getAuthor().getAsMention() + "."); + } else { + replyByPrivateMessage(event, "File `" + name + "` is too large to add to library."); + } + } + } + } else { + if (message.startsWith(commandCharacter) || event.isPrivate()) { + nonRecognizedCommand(event, requestingUser); + } } } } diff --git a/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java b/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java index 2b32d63..d257e1a 100644 --- a/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java +++ b/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java @@ -8,8 +8,7 @@ import net.dv8tion.jda.JDABuilder; import net.dv8tion.jda.OnlineStatus; import net.dv8tion.jda.audio.player.FilePlayer; -import net.dv8tion.jda.entities.Guild; -import net.dv8tion.jda.entities.VoiceChannel; +import net.dv8tion.jda.entities.*; import net.dv8tion.jda.events.message.MessageReceivedEvent; import net.dv8tion.jda.managers.AudioManager; import net.dv8tion.jda.player.MusicPlayer; @@ -52,6 +51,7 @@ public class SoundPlayerImpl implements Observer { private MusicPlayer musicPlayer; private FilePlayer player; private String playerSetting; + private String soundFileDir; @Inject public SoundPlayerImpl(MainWatch mainWatch) { @@ -174,6 +174,14 @@ public List getUsers() { return users; } + /** + * Get the path the application is using for sound files. + * @return String representation of the sound file path. + */ + public String getSoundsPath() { + return soundFileDir; + } + /** * Find the "author" of the event and join the voice channel they are in. * @param event - The event @@ -204,6 +212,17 @@ private void moveToChannel(VoiceChannel channel, Guild guild){ } else { audioManager.openAudioConnection(channel); } + + //Wait for the audio connection to be ready before proceeding. + synchronized (this) { + while(audioManager.isAttemptingToConnect()) { + try { + wait(100); + } catch (InterruptedException e) { + LOG.warn("Waiting for audio connection was interrupted."); + } + } + } } /** @@ -325,7 +344,7 @@ private Map getFileList() { Map returnFiles = new TreeMap<>(); try { - String soundFileDir = appProperties.getProperty("sounds_directory"); + soundFileDir = appProperties.getProperty("sounds_directory"); if (soundFileDir == null || soundFileDir.isEmpty()) { soundFileDir = System.getProperty("user.dir") + "/sounds"; }