aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/soundchan
diff options
context:
space:
mode:
authorBrandon <bwaggone@umich.edu>2018-07-07 19:48:36 -0400
committerGitHub <noreply@github.com>2018-07-07 19:48:36 -0400
commitd72a49fbb2186f5749baa903408843797fab0971 (patch)
treefeba9798b9a2724adc204b109d961ca387cce387 /src/main/java/soundchan
parentc23f6622afe07fff1362494de8ce9890e7767a7e (diff)
parentd6aa077b1eda1ea5694dde0250b8b5fd21f114dd (diff)
downloadSoundChan-d72a49fbb2186f5749baa903408843797fab0971.tar.gz
SoundChan-d72a49fbb2186f5749baa903408843797fab0971.tar.bz2
SoundChan-d72a49fbb2186f5749baa903408843797fab0971.zip
Merge pull request #8 from bwaggone/feature/add-audio-on-join
Feature/add audio on join
Diffstat (limited to 'src/main/java/soundchan')
-rw-r--r--src/main/java/soundchan/BotListener/BotListener.java83
-rw-r--r--src/main/java/soundchan/LocalAudioManager.java75
2 files changed, 153 insertions, 5 deletions
diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java
index b09fffc..b54edbe 100644
--- a/src/main/java/soundchan/BotListener/BotListener.java
+++ b/src/main/java/soundchan/BotListener/BotListener.java
@@ -11,6 +11,7 @@ import net.dv8tion.jda.client.events.call.voice.CallVoiceJoinEvent;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
+import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
@@ -26,13 +27,16 @@ public class BotListener extends ListenerAdapter{
private long monitoredGuildId = -1;
private Guild monitoredGuild;
- private static String followingUser;
- private static String localFilePath;
private static LocalAudioManager localManager;
private final AudioPlayerManager playerManager;
private final Map<Long, GuildMusicManager> musicManagers;
private BotListenerHelpers helper = new BotListenerHelpers();
+ // From configuration file
+ private static String followingUser;
+ private static String localFilePath;
+ private static boolean audioOnUserJoin;
+
public BotListener(Properties properties) {
this.musicManagers = new HashMap<>();
@@ -40,9 +44,22 @@ public class BotListener extends ListenerAdapter{
AudioSourceManagers.registerRemoteSources(playerManager);
AudioSourceManagers.registerLocalSource(playerManager);
+ loadProperties(properties);
+ }
+
+ /**
+ * Loads various properties from config file
+ * @param properties Object holding the contents of the property file
+ */
+ private void loadProperties(Properties properties) {
localFilePath = properties.getProperty("localFilePath");
followingUser = properties.getProperty("followingUser");
- localManager = new LocalAudioManager(localFilePath);
+ audioOnUserJoin = settingEnableCheck(properties.getProperty("audioOnUserJoin"));
+ if(audioOnUserJoin) {
+ localManager = new LocalAudioManager(localFilePath, properties.getProperty("userAudioFilePath"));
+ }
+ else
+ localManager = new LocalAudioManager(localFilePath);
}
private synchronized GuildMusicManager getGuildAudioPlayer() {
@@ -61,7 +78,52 @@ public class BotListener extends ListenerAdapter{
@Override
public void onCallVoiceJoin(CallVoiceJoinEvent event){
+ super.onCallVoiceJoin(event);
+ }
+
+ /**
+ * Plays an audio clip when a user connects to the voice channel if enabled in the config file. For the sound to play,
+ * there needs to be a sound file with the same name as the user, otherwise it won't play anything.
+ * @param event
+ */
+ @Override
+ public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
+ if(audioOnUserJoin) {
+ String filepath = localManager.GetFilePath(event.getMember().getEffectiveName());
+ if (!filepath.contentEquals("")) {
+ GuildMusicManager musicManager = getGuildAudioPlayer();
+
+ playerManager.loadItemOrdered(musicManager, filepath, new AudioLoadResultHandler() {
+ @Override
+ public void trackLoaded(AudioTrack track) {
+ play(monitoredGuild, musicManager, track, true);
+ }
+ @Override
+ public void playlistLoaded(AudioPlaylist playlist) {
+ AudioTrack firstTrack = playlist.getSelectedTrack();
+
+ if (firstTrack == null) {
+ firstTrack = playlist.getTracks().get(0);
+ }
+ play(monitoredGuild, musicManager, firstTrack, false);
+ }
+
+ @Override
+ public void noMatches() {
+ // Needed, but shouldn't be called
+ System.out.println("Nothing found for " + event.getMember().getEffectiveName());
+ }
+
+ @Override
+ public void loadFailed(FriendlyException exception) {
+ // Needed, but shouldn't be called
+ System.out.println("Could not play: " + exception.getMessage());
+ }
+ });
+ }
+ }
+ super.onGuildVoiceJoin(event);
}
@@ -287,4 +349,19 @@ public class BotListener extends ListenerAdapter{
}
}
+ /**
+ * Checks the string for some reason to enable/disable a setting.
+ * @param value A string (probably read in from config file)
+ * @return True if it matches a value to enable, False otherwise
+ */
+ private static boolean settingEnableCheck(String value) {
+ value = value.toLowerCase();
+ if(value.contentEquals("true") || value.contentEquals("1") ||
+ value.contentEquals("yes") || value.contentEquals("on") ||
+ value.contentEquals("enable"))
+ return true;
+ else
+ return false;
+ }
+
}
diff --git a/src/main/java/soundchan/LocalAudioManager.java b/src/main/java/soundchan/LocalAudioManager.java
index 9a4e299..e645fe7 100644
--- a/src/main/java/soundchan/LocalAudioManager.java
+++ b/src/main/java/soundchan/LocalAudioManager.java
@@ -3,6 +3,9 @@ package soundchan;
import net.dv8tion.jda.core.entities.MessageChannel;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.*;
public class LocalAudioManager {
@@ -12,6 +15,7 @@ public class LocalAudioManager {
*/
public Map<String, String> filenameDict;
+ public Map<String, String> usernameDict;
private String filepath;
public LocalAudioManager(String filepath_in){
@@ -20,13 +24,41 @@ public class LocalAudioManager {
PopulateFiles();
}
+ /**
+ * Constructor for when there is a file listing users and sounds to play for them
+ * @param filepath_in Path to folder where sounds are located
+ * @param userSoundFile Path to file with users and sounds
+ */
+ public LocalAudioManager(String filepath_in, String userSoundFile) {
+ filepath = filepath_in;
+ filenameDict = new HashMap<>();
+ usernameDict = new HashMap<>();
+ PopulateFiles();
+ MapUserAudio(userSoundFile);
+ }
+
+ /**
+ * Gives filepath to sound to play either from a command or when given a username
+ * @param command Command or username to play soundbite
+ * @return Path to sound, or "" if no sound for given command
+ */
public String GetFilePath(String command){
+ String path;
try{
- return filepath + "/" + filenameDict.get(command);
+ path = filepath + "/" + filenameDict.get(command);
}catch(Exception ex){
System.out.println("File " + command + " not found!");
+ path = "";
+ }
+ if(path.contentEquals("") || path.contentEquals(filepath + "/null")) {
+ try {
+ path = filepath + "/" + usernameDict.get(command);
+ } catch (Exception ex) {
+ System.out.println("File " + command + " not found!");
+ path = "";
+ }
}
- return "";
+ return path;
}
public void ListSounds(MessageChannel channel){
@@ -52,5 +84,44 @@ public class LocalAudioManager {
}
}
+ /**
+ * Reads in users and their respective sounds from file, then builds a map of users to the filenames. This assumes
+ * filenames for the sounds are valid, but doesn't check for them.
+ * @param userSoundFile The file (with path if required) with listing of users and the sounds to play when they join
+ */
+ private void MapUserAudio(String userSoundFile) {
+ Properties userSoundProp = LoadProperties(userSoundFile);
+ Set<String> users = userSoundProp.stringPropertyNames();
+ for(String user : users) {
+ String soundFile = userSoundProp.getProperty(user);
+ usernameDict.put(user, soundFile);
+ }
+
+ }
+
+ /**
+ * Builds a property object from a file.
+ * @param filename File to be read
+ * @return Property object with information from file
+ */
+ private static Properties LoadProperties(String filename){
+ Properties properties = new Properties();
+ InputStream input = null;
+ try{
+ input = new FileInputStream(filename);
+ properties.load(input);
+
+ }catch (IOException ex){
+ ex.printStackTrace();
+ } finally {
+ try {
+ input.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+ return properties;
+ }
+
}