From bd81104d8765250b19adbe5d56c33a8e547e3674 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 9 Apr 2023 08:29:52 +0700 Subject: [PATCH] add sus autocomplete (works, tested) --- .../chomens_bot/commands/MusicCommand.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java index 35ec08b..7ebf874 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java @@ -101,8 +101,40 @@ public class MusicCommand implements Command { _path = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); path = Path.of(root.toString(), _path); - if (!path.toString().contains("http")) player.loadSong(path); - else player.loadSong(new URL(_path)); + if (path.toString().contains("http")) player.loadSong(new URL(_path)); + else { + // ignore my shitcode for autocomplete + final String separator = File.separator; + + if (_path.contains(separator) && !_path.equals("")) { + final String[] pathSplitted = _path.split(separator); + + final List pathSplittedClone = new ArrayList<>(Arrays.stream(pathSplitted.clone()).toList()); + pathSplittedClone.remove(pathSplittedClone.size() - 1); + + final Path realPath = Path.of(root.toString(), String.join(separator, pathSplittedClone)); + + final String[] songs = realPath.toFile().list(); + + if (songs == null) return Component.text("Directory does not exist").color(NamedTextColor.RED); + + final String lowerCaseFile = pathSplitted[pathSplitted.length - 1].toLowerCase(); + + final String file = Arrays.stream(songs) + .filter(song -> song.toLowerCase().contains(lowerCaseFile)) + .toArray(String[]::new)[0]; + + player.loadSong(Path.of(realPath.toString(), file)); + } else { + final String[] songs = root.toFile().list(); + + final String file = Arrays.stream(songs) + .filter(song -> song.toLowerCase().contains(_path)) + .toArray(String[]::new)[0]; + + player.loadSong(Path.of(root.toString(), file)); + } + } } catch (MalformedURLException e) { return Component.text("Invalid URL").color(NamedTextColor.RED); } catch (Exception e) {