Skip to content

Commit

Permalink
Added code that takes an attached mp3 or wav file from received PM an…
Browse files Browse the repository at this point in the history
…d adds it to the soundboard.
  • Loading branch information
Darkside138 committed May 11, 2016
1 parent 9a96be8 commit 80b9134
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Message.Attachment> 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -174,6 +174,14 @@ public List<net.dirtydeeds.discordsoundboard.beans.User> 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
Expand Down Expand Up @@ -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.");
}
}
}
}

/**
Expand Down Expand Up @@ -325,7 +344,7 @@ private Map<String,SoundFile> getFileList() {
Map<String,SoundFile> 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";
}
Expand Down

0 comments on commit 80b9134

Please sign in to comment.