From 8ef0634fff740d10668c80a0c57a35ff5e9f635c Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Sat, 30 Jun 2018 18:01:00 -0400 Subject: Adding the ability to play an audio clip when a user joins the voice channel Not the prettiest thing in the world --- soundchan.properties.example | 3 +++ 1 file changed, 3 insertions(+) (limited to 'soundchan.properties.example') diff --git a/soundchan.properties.example b/soundchan.properties.example index f862cc5..6848d18 100644 --- a/soundchan.properties.example +++ b/soundchan.properties.example @@ -6,3 +6,6 @@ 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 +audioOnUserJoin=on/off -- cgit v1.2.3 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 --- soundchan.properties.example | 5 +- .../java/soundchan/BotListener/BotListener.java | 6 +- src/main/java/soundchan/LocalAudioManager.java | 75 +++++++++++++++++++++- usersound.properties.example | 4 ++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 usersound.properties.example (limited to 'soundchan.properties.example') 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 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; + } + } 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 -- cgit v1.2.3 From d6aa077b1eda1ea5694dde0250b8b5fd21f114dd Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Fri, 6 Jul 2018 21:43:41 -0400 Subject: Cleaning things up a bit --- soundchan.properties.example | 5 +++++ src/main/java/soundchan/BotListener/BotListener.java | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'soundchan.properties.example') diff --git a/soundchan.properties.example b/soundchan.properties.example index 2ba3290..c15a89a 100644 --- a/soundchan.properties.example +++ b/soundchan.properties.example @@ -7,7 +7,12 @@ localFilePath=C:\\PATH\\TO\\SOUNDS\\DIRECTORY //The user for the followingUser=USERNAME +//Flag conditions enabled with any of the following values: +// true, on, enable, yes, 1 +//any other string (or empty) will leave the condition disabled + //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 +//This is a flag condition audioOnUserJoin=on/off //The file where users and sound clips are related, see usersound.properties.example for more info diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index 7a65f78..b54edbe 100644 --- a/src/main/java/soundchan/BotListener/BotListener.java +++ b/src/main/java/soundchan/BotListener/BotListener.java @@ -54,8 +54,7 @@ public class BotListener extends ListenerAdapter{ private void loadProperties(Properties properties) { localFilePath = properties.getProperty("localFilePath"); followingUser = properties.getProperty("followingUser"); - String temp = properties.getProperty("audioOnUserJoin"); - audioOnUserJoin = settingEnableCheck(temp); + audioOnUserJoin = settingEnableCheck(properties.getProperty("audioOnUserJoin")); if(audioOnUserJoin) { localManager = new LocalAudioManager(localFilePath, properties.getProperty("userAudioFilePath")); } -- cgit v1.2.3