From 6ffac3806e3369649ae26a7c9a58baaf52c35829 Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Sun, 1 Jul 2018 20:32:23 -0400 Subject: Adding in user/sound relation from a file You can now have a file with all the users and the intro sound to play without having to have multiple copies of the same sound --- .../java/soundchan/BotListener/BotListener.java | 6 +- src/main/java/soundchan/LocalAudioManager.java | 75 +++++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) (limited to 'src/main') diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index 33b9b7a..cccb39a 100644 --- a/src/main/java/soundchan/BotListener/BotListener.java +++ b/src/main/java/soundchan/BotListener/BotListener.java @@ -48,7 +48,11 @@ public class BotListener extends ListenerAdapter{ followingUser = properties.getProperty("followingUser"); String temp = properties.getProperty("audioOnUserJoin"); audioOnUserJoin = settingEnableCheck(temp); - localManager = new LocalAudioManager(localFilePath); + if(audioOnUserJoin) { + localManager = new LocalAudioManager(localFilePath, properties.getProperty("userAudioFilePath")); + } + else + localManager = new LocalAudioManager(localFilePath); } private synchronized GuildMusicManager getGuildAudioPlayer() { 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 filenameDict; + public Map 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 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; + } + } -- cgit v1.2.3