From ee9c435b5dfd6086e1cf4971999e0611efe05a26 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Sun, 21 Apr 2024 16:48:49 +0100 Subject: [PATCH] Add soundboard functionality. --- src/main/java/moe/nekojimi/chords/Chords.java | 32 +++++- .../java/moe/nekojimi/chords/Soundboard.java | 92 +++++++++++++++++ .../chords/commands/SoundboardCommand.java | 98 +++++++++++++++++++ 3 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 src/main/java/moe/nekojimi/chords/Soundboard.java create mode 100644 src/main/java/moe/nekojimi/chords/commands/SoundboardCommand.java diff --git a/src/main/java/moe/nekojimi/chords/Chords.java b/src/main/java/moe/nekojimi/chords/Chords.java index 8f17d04..2258fdc 100644 --- a/src/main/java/moe/nekojimi/chords/Chords.java +++ b/src/main/java/moe/nekojimi/chords/Chords.java @@ -52,11 +52,13 @@ public final class Chords extends ListenerAdapter private final File dataDirectory; private final File playlistsDirectory; + private final File soundboardFile; private MusicHandler musicHandler; private final Downloader downloader; private final Searcher searcher; private final QueueManager queueManager; + private final Soundboard soundboard; // private Recommender recommender; private JDA jda; @@ -141,6 +143,7 @@ public final class Chords extends ListenerAdapter // init dirs dataDirectory = new File(System.getProperty("user.dir")); playlistsDirectory = initDirectory(dataDirectory, "playlists"); + soundboardFile = new File("soundboard.yml"); // init downloader downloader = new Downloader(); @@ -153,6 +156,10 @@ public final class Chords extends ListenerAdapter queueManager = new QueueManager(); queueManager.addSource(downloader); + // init soundboard + soundboard = new Soundboard(this); + soundboard.loadYAML(soundboardFile); + // init commands addCommand(new JoinCommand(this)); addCommand(new LeaveCommand(this)); @@ -162,6 +169,7 @@ public final class Chords extends ListenerAdapter addCommand(new RemoveCommand(this)); addCommand(new RestartCommand(this)); addCommand(new SkipCommand(this)); + addCommand(new SoundboardCommand(this)); helpCommand = new HelpCommand(this); addCommand(helpCommand); @@ -201,7 +209,7 @@ public final class Chords extends ListenerAdapter super.onMessageReceived(event); //To change body of generated methods, choose Tools | Templates. Message message = event.getMessage(); User author = message.getAuthor(); - String content = message.getContentRaw(); + String content = message.getContentRaw().trim(); Guild guild = event.getGuild(); // Ignore message if it's not in a music channel @@ -220,7 +228,7 @@ public final class Chords extends ListenerAdapter { try { - URL parseURL = new URL(content.trim()); + URL parseURL = new URL(content); invocation = new Invocation(event, List.of(parseURL.toExternalForm())); playCommand.call(invocation); return; @@ -268,6 +276,10 @@ public final class Chords extends ListenerAdapter } } } + } else if (soundboard != null) + { + // try looking it up with the soundboard + soundboard.play(cmd); } } catch (Exception ex) { @@ -445,6 +457,22 @@ public final class Chords extends ListenerAdapter return playlists; } + public Soundboard getSoundboard() + { + return soundboard; + } + + public void saveSoundboard() + { + try + { + soundboard.saveYAML(soundboardFile); + } catch (IOException ex) + { + + } + } + // public Recommender getRecommender() // { // return recommender; diff --git a/src/main/java/moe/nekojimi/chords/Soundboard.java b/src/main/java/moe/nekojimi/chords/Soundboard.java new file mode 100644 index 0000000..7aea52c --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/Soundboard.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2024 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords; + +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlInput; +import com.amihaiemil.eoyaml.YamlMapping; +import com.amihaiemil.eoyaml.YamlPrinter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.*; + +/** + * + * @author jimj316 + */ +public class Soundboard +{ + + private final Chords bot; + private final Map sounds = new HashMap<>(); + + public Soundboard(Chords bot) + { + this.bot = bot; + } + + public void loadYAML(File file) throws IOException + { + if (!file.exists()) + return; + YamlInput input = Yaml.createYamlInput(file); + YamlMapping mapping = input.readYamlMapping(); + sounds.putAll(Util.yamlMappingToMap(mapping, (t) -> Track.fromYaml(t.asMapping()))); + } + + public void saveYAML(File file) throws IOException + { + YamlPrinter printer = Yaml.createYamlPrinter(new FileWriter(file)); + printer.print(Util.mapToMapping(sounds, (t) -> t.toYaml())); + } + + public void add(String emoji, Track t) + { + t.setKept(true); + sounds.put(emoji, t); + } + + public boolean remove(String emoji) + { + if (!sounds.containsKey(emoji)) + return false; + Track t = sounds.get(emoji); + t.setKept(false); + t.delete(); + sounds.remove(emoji); + return true; + } + + public boolean play(String emoji) + { + if (!sounds.containsKey(emoji)) + return false; + if (bot.getMusicHandler() == null) + return false; + final Track track = sounds.get(emoji); + track.setKept(true); + bot.getMusicHandler().play(track); + return true; + } + + public List getEmoji() + { + return new ArrayList<>(sounds.keySet()); + } + +} diff --git a/src/main/java/moe/nekojimi/chords/commands/SoundboardCommand.java b/src/main/java/moe/nekojimi/chords/commands/SoundboardCommand.java new file mode 100644 index 0000000..fc92b36 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/commands/SoundboardCommand.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2024 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords.commands; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; +import moe.nekojimi.chords.Chords; +import moe.nekojimi.chords.TrackRequest; + +/** + * + * @author jimj316 + */ +public class SoundboardCommand extends Command +{ + + public SoundboardCommand(Chords bot) + { + super(bot, "board"); + } + + @Override + public void call(Invocation invocation) + { + String action = invocation.getArgs().isEmpty() ? "" : invocation.getArgs().get(0); + String emoji = invocation.getArgs().size() < 2 ? "" : invocation.getArgs().get(1); + if (action.equals("add")) + { + String urlString = invocation.getArgs().get(2); + if (emoji.isBlank()) + { + invocation.respond("You must specify a trigger!"); + return; + } + try + { + URL url; + if (invocation.getArgs().size() < 3) + { + invocation.respond("No URL given!"); + return; + } + url = new URL(urlString); + TrackRequest request = new TrackRequest(); + request.setUrl(url); + request.setInvocation(invocation); + request.setKeepTracks(true); + + bot.getDownloader().requestSpecific(List.of(request), (t) -> + { + bot.getSoundboard().add(emoji, t); + bot.saveSoundboard(); + }); + } catch (MalformedURLException ex) + { + invocation.respond("That's not a valid URL! " + ex.getMessage()); + return; + } + } else if (action.equals("remove")) + { + if (emoji.isBlank()) + { + invocation.respond("You must specify a trigger!"); + return; + } + bot.getSoundboard().remove(emoji); + bot.saveSoundboard(); + } else if (action.equals("list")) + { + List list = bot.getSoundboard().getEmoji(); + String response = "__Soundboard commands available:__\n"; + for (String item : list) + { + response += item + " "; + } + invocation.respond(response); + } else + { + invocation.respond("Specify \"add\" or \"remove\" after the command!"); + } + } + +}