diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2018-07-01 20:32:23 -0400 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2018-07-01 20:32:23 -0400 |
commit | 6ffac3806e3369649ae26a7c9a58baaf52c35829 (patch) | |
tree | 94ee566fe597e9b93f97b0566f9b7fef7342291f | |
parent | 7a23679a99f692e4dc7ee2ab109f66fe96054ba2 (diff) | |
download | SoundChan-6ffac3806e3369649ae26a7c9a58baaf52c35829.tar.gz SoundChan-6ffac3806e3369649ae26a7c9a58baaf52c35829.tar.bz2 SoundChan-6ffac3806e3369649ae26a7c9a58baaf52c35829.zip |
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
-rw-r--r-- | soundchan.properties.example | 5 | ||||
-rw-r--r-- | src/main/java/soundchan/BotListener/BotListener.java | 6 | ||||
-rw-r--r-- | src/main/java/soundchan/LocalAudioManager.java | 75 | ||||
-rw-r--r-- | usersound.properties.example | 4 |
4 files changed, 86 insertions, 4 deletions
diff --git a/soundchan.properties.example b/soundchan.properties.example index 6848d18..2ba3290 100644 --- a/soundchan.properties.example +++ b/soundchan.properties.example @@ -7,5 +7,8 @@ localFilePath=C:\\PATH\\TO\\SOUNDS\\DIRECTORY //The user for the followingUser=USERNAME -//If you want SoundChan to play an audio file whit their name when a user joins the channel +//If you want SoundChan to play an audio file whit their name when a user joins the channel or have that information come from below file audioOnUserJoin=on/off + +//The file where users and sound clips are related, see usersound.properties.example for more info +userAudioFilePath=C:\\PATH\\TO\\USER\\SOUND\\FILE 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<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; + } + } diff --git a/usersound.properties.example b/usersound.properties.example new file mode 100644 index 0000000..9575f01 --- /dev/null +++ b/usersound.properties.example @@ -0,0 +1,4 @@ +//Here we can map a user to a sound file, so many users can share the same intro soundbite and/or so you don't have to have extra copies of the same sound file + +//Discord user and the file (with extension) +Queen=WeAreTheChampions.mp3 |