diff options
author | Matt <mattkohls13@gmail.com> | 2018-04-10 22:11:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-10 22:11:08 -0400 |
commit | 442bf1ea125f6fa2209e9c20306f604125f612fe (patch) | |
tree | 6327a37aa53a70c368c103b866a3cfccac454635 | |
parent | e9010ccdbd1600065bba34a08e78c9938dc93a6e (diff) | |
parent | e9ff559c08fa0529b5c6da9cf82d0beb6e7e7e17 (diff) | |
download | SoundChan-442bf1ea125f6fa2209e9c20306f604125f612fe.tar.gz SoundChan-442bf1ea125f6fa2209e9c20306f604125f612fe.tar.bz2 SoundChan-442bf1ea125f6fa2209e9c20306f604125f612fe.zip |
Merge pull request #2 from bwaggone/feature/LocalAudioFiles
Add local audio file support, including listing files
-rw-r--r-- | src/main/java/soundchan/LocalAudioManager.java | 55 | ||||
-rw-r--r-- | src/main/java/soundchan/Main.java | 40 |
2 files changed, 76 insertions, 19 deletions
diff --git a/src/main/java/soundchan/LocalAudioManager.java b/src/main/java/soundchan/LocalAudioManager.java new file mode 100644 index 0000000..314f6b9 --- /dev/null +++ b/src/main/java/soundchan/LocalAudioManager.java @@ -0,0 +1,55 @@ +package soundchan; + +import net.dv8tion.jda.core.entities.MessageChannel; + +import java.io.File; +import java.util.*; + +public class LocalAudioManager { + + /* + A list of local files for the sound bot to play. + */ + + public Map<String, String> filenameDict; + private String filepath; + + public LocalAudioManager(String filepath_in){ + filepath = filepath_in; + filenameDict = new HashMap<>(); + PopulateFiles(); + } + + public String GetFilePath(String command){ + try{ + return filepath + "/" + filenameDict.get(command); + }catch(Exception ex){ + System.out.println("File " + command + " not found!"); + } + return ""; + } + + public void ListSounds(MessageChannel channel){ + Set<String> localSounds = filenameDict.keySet(); + String toPrint = "The following sounds you can play are:\n"; + for (String sound: + localSounds) { + toPrint = toPrint + " * " + sound + "\n"; + } + channel.sendMessage(toPrint).queue(); + } + + private void PopulateFiles(){ + File folder = new File(filepath); + File[] listOfFiles = folder.listFiles(); + + for (File file : listOfFiles) { + if (file.isFile()) { + String filename = file.getName(); + filenameDict.put(filename.substring(0, filename.indexOf('.')), filename); + } + } + } + + +} diff --git a/src/main/java/soundchan/Main.java b/src/main/java/soundchan/Main.java index 3d0b9b6..2b34aca 100644 --- a/src/main/java/soundchan/Main.java +++ b/src/main/java/soundchan/Main.java @@ -33,10 +33,11 @@ public class Main extends ListenerAdapter { .setToken(properties.getProperty("botToken")) .buildBlocking(); - localFilePath = properties.getProperty("localFilePath"); jda.addEventListener(new Main()); + localFilePath = properties.getProperty("localFilePath"); followingUser = properties.getProperty("followingUser"); + localManager = new LocalAudioManager(localFilePath); } private static Properties LoadProperties(){ @@ -62,6 +63,7 @@ public class Main extends ListenerAdapter { 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; @@ -73,20 +75,6 @@ public class Main extends ListenerAdapter { AudioSourceManagers.registerLocalSource(playerManager); } - private synchronized GuildMusicManager getGuildAudioPlayer(Guild guild) { - long guildId = Long.parseLong(guild.getId()); - GuildMusicManager musicManager = musicManagers.get(guildId); - - if (musicManager == null) { - musicManager = new GuildMusicManager(playerManager); - musicManagers.put(guildId, musicManager); - } - - guild.getAudioManager().setSendingHandler(musicManager.getSendHandler()); - - return musicManager; - } - private synchronized GuildMusicManager getGuildAudioPlayer() { long guildId = monitoredGuildId; GuildMusicManager musicManager = musicManagers.get(guildId); @@ -141,7 +129,13 @@ public class Main extends ListenerAdapter { // "!" Signifies that you're looking to play a sound effect if(command[0].startsWith("!") && command[0].length() > 1){ - loadAndPlay(channel, localFilePath + "\\" + command[0].substring(1) + ".mp3"); + String filepath = localManager.GetFilePath(command[0].substring(1)); + if(!filepath.contentEquals("")) { + loadAndPlay(channel, filepath); + } + else{ + channel.sendMessage("File \"" + command[0].substring(1) + "\" not found!").queue(); + } } // "~" Signifies that you're looking to play a song/sound from a url @@ -157,7 +151,14 @@ public class Main extends ListenerAdapter { } else if ("~unpause".equals(command[0])) { unpauseTrack(channel); } else if ("~list".equals(command[0])) { - listTracks(channel); + if(command.length == 2){ + if(command[1].equals("queue")){ + listTracks(channel); + } + else if(command[1].equals("sounds")){ + localManager.ListSounds(channel); + } + } } } @@ -240,7 +241,7 @@ public class Main extends ListenerAdapter { } private void play(Guild guild, GuildMusicManager musicManager, AudioTrack track) { - connectToFirstVoiceChannel(guild.getAudioManager()); + connectToFollowingVoiceChannel(guild.getAudioManager()); musicManager.scheduler.queue(track); } @@ -252,7 +253,8 @@ public class Main extends ListenerAdapter { channel.sendMessage("Skipped to next track.").queue(); } - private static void connectToFirstVoiceChannel(AudioManager audioManager) { + + private static void connectToFollowingVoiceChannel(AudioManager audioManager) { if (!audioManager.isConnected() && !audioManager.isAttemptingToConnect()) { for (VoiceChannel voiceChannel : audioManager.getGuild().getVoiceChannels()) { for(int i = 0; i < voiceChannel.getMembers().size(); i++) { |