diff --git a/pom.xml b/pom.xml index 46efea0c4..02a8c40bb 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.jagrosh JMusicBot - 0.2.7 + Snapshot jar @@ -28,7 +28,7 @@ com.sedmelluq lavaplayer - 1.3.38 + 1.3.46 com.sedmelluq diff --git a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java index 098cc85fa..48d676e34 100644 --- a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java +++ b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java @@ -64,7 +64,7 @@ public void load() try { // get the path to the config, default config.txt - path = Paths.get(System.getProperty("config.file", System.getProperty("config", "config.txt"))); + path = OtherUtil.getPath(System.getProperty("config.file", System.getProperty("config", "config.txt"))); if(path.toFile().exists()) { if(System.getProperty("config.file") == null) diff --git a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java index 533d3cdb0..683df91d3 100644 --- a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java +++ b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java @@ -167,7 +167,7 @@ else if(config.getGame().getName().equalsIgnoreCase("none")) } } - log.info("Loaded config from "+config.getConfigLocation()); + log.info("Loaded config from " + config.getConfigLocation()); // attempt to log in and start try diff --git a/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java b/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java index da678b915..e32ef5c5a 100644 --- a/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java +++ b/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java @@ -16,6 +16,7 @@ package com.jagrosh.jmusicbot.playlist; import com.jagrosh.jmusicbot.BotConfig; +import com.jagrosh.jmusicbot.utils.OtherUtil; import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; @@ -24,7 +25,6 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; -import java.nio.file.Paths; import java.util.*; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -61,29 +61,29 @@ public void createFolder() { try { - Files.createDirectory(Paths.get(config.getPlaylistsFolder())); + Files.createDirectory(OtherUtil.getPath(config.getPlaylistsFolder())); } catch (IOException ignore) {} } public boolean folderExists() { - return Files.exists(Paths.get(config.getPlaylistsFolder())); + return Files.exists(OtherUtil.getPath(config.getPlaylistsFolder())); } public void createPlaylist(String name) throws IOException { - Files.createFile(Paths.get(config.getPlaylistsFolder()+File.separator+name+".txt")); + Files.createFile(OtherUtil.getPath(config.getPlaylistsFolder()+File.separator+name+".txt")); } public void deletePlaylist(String name) throws IOException { - Files.delete(Paths.get(config.getPlaylistsFolder()+File.separator+name+".txt")); + Files.delete(OtherUtil.getPath(config.getPlaylistsFolder()+File.separator+name+".txt")); } public void writePlaylist(String name, String text) throws IOException { - Files.write(Paths.get(config.getPlaylistsFolder()+File.separator+name+".txt"), text.trim().getBytes()); + Files.write(OtherUtil.getPath(config.getPlaylistsFolder()+File.separator+name+".txt"), text.trim().getBytes()); } public Playlist getPlaylist(String name) @@ -96,7 +96,7 @@ public Playlist getPlaylist(String name) { boolean[] shuffle = {false}; List list = new ArrayList<>(); - Files.readAllLines(Paths.get(config.getPlaylistsFolder()+File.separator+name+".txt")).forEach(str -> + Files.readAllLines(OtherUtil.getPath(config.getPlaylistsFolder()+File.separator+name+".txt")).forEach(str -> { String s = str.trim(); if(s.isEmpty()) diff --git a/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java b/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java index bfb2d8e98..0087f46ac 100644 --- a/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java +++ b/src/main/java/com/jagrosh/jmusicbot/settings/SettingsManager.java @@ -16,6 +16,7 @@ package com.jagrosh.jmusicbot.settings; import com.jagrosh.jdautilities.command.GuildSettingsManager; +import com.jagrosh.jmusicbot.utils.OtherUtil; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -37,7 +38,7 @@ public SettingsManager() { this.settings = new HashMap<>(); try { - JSONObject loadedSettings = new JSONObject(new String(Files.readAllBytes(Paths.get("serversettings.json")))); + JSONObject loadedSettings = new JSONObject(new String(Files.readAllBytes(OtherUtil.getPath("serversettings.json")))); loadedSettings.keySet().forEach((id) -> { JSONObject o = loadedSettings.getJSONObject(id); settings.put(Long.parseLong(id), new Settings(this, @@ -99,7 +100,7 @@ protected void writeSettings() obj.put(Long.toString(key), o); }); try { - Files.write(Paths.get("serversettings.json"), obj.toString(4).getBytes()); + Files.write(OtherUtil.getPath("serversettings.json"), obj.toString(4).getBytes()); } catch(IOException ex){ LoggerFactory.getLogger("Settings").warn("Failed to write to file: "+ex); } diff --git a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java index c0d479001..8b2880bf8 100644 --- a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java +++ b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java @@ -18,8 +18,11 @@ import com.jagrosh.jmusicbot.JMusicBot; import com.jagrosh.jmusicbot.entities.Prompt; import java.io.*; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; +import java.nio.file.Path; +import java.nio.file.Paths; import net.dv8tion.jda.core.OnlineStatus; import net.dv8tion.jda.core.entities.Game; import okhttp3.*; @@ -37,7 +40,38 @@ public class OtherUtil + "Current version: %s\n" + "New Version: %s\n\n" + "Please visit https://github.com/jagrosh/MusicBot/releases/latest to get the latest release."; + private final static String WINDOWS_INVALID_PATH = "c:\\windows\\system32\\"; + /** + * gets a Path from a String + * also fixes the windows tendency to try to start in system32 + * any time the bot tries to access this path, it will instead start in the location of the jar file + * + * @param path the string path + * @return the Path object + */ + public static Path getPath(String path) + { + // special logic to prevent trying to access system32 + if(path.toLowerCase().startsWith(WINDOWS_INVALID_PATH)) + { + String filename = path.substring(WINDOWS_INVALID_PATH.length()); + try + { + path = new File(JMusicBot.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile().getPath() + File.separator + filename; + } + catch(URISyntaxException ex) {} + } + return Paths.get(path); + } + + /** + * Loads a resource from the jar as a string + * + * @param clazz class base object + * @param name name of resource + * @return string containing the contents of the resource + */ public static String loadResource(Object clazz, String name) { try(BufferedReader reader = new BufferedReader(new InputStreamReader(clazz.getClass().getResourceAsStream(name)))) @@ -52,6 +86,12 @@ public static String loadResource(Object clazz, String name) } } + /** + * Loads image data from a URL + * + * @param url url of image + * @return inputstream of url + */ public static InputStream imageFromUrl(String url) { if(url==null) @@ -67,29 +107,40 @@ public static InputStream imageFromUrl(String url) return null; } + /** + * Parses an activity from a string + * + * @param game the game, including the action such as 'playing' or 'watching' + * @return the parsed activity + */ public static Game parseGame(String game) { if(game==null || game.trim().isEmpty() || game.trim().equalsIgnoreCase("default")) return null; String lower = game.toLowerCase(); if(lower.startsWith("playing")) - return Game.playing(game.substring(7).trim()); + return Game.playing(makeNonEmpty(game.substring(7).trim())); if(lower.startsWith("listening to")) - return Game.listening(game.substring(12).trim()); + return Game.listening(makeNonEmpty(game.substring(12).trim())); if(lower.startsWith("listening")) - return Game.listening(game.substring(9).trim()); + return Game.listening(makeNonEmpty(game.substring(9).trim())); if(lower.startsWith("watching")) - return Game.watching(game.substring(8).trim()); + return Game.watching(makeNonEmpty(game.substring(8).trim())); if(lower.startsWith("streaming")) { String[] parts = game.substring(9).trim().split("\\s+", 2); if(parts.length == 2) { - return Game.streaming(parts[1], "https://twitch.tv/"+parts[0]); + return Game.streaming(makeNonEmpty(parts[1]), "https://twitch.tv/"+parts[0]); } } return Game.playing(game); } + + public static String makeNonEmpty(String str) + { + return str == null || str.isEmpty() ? "\u200B" : str; + } public static OnlineStatus parseStatus(String status) {