diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java index 3bba353..f9815b6 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java @@ -5,21 +5,23 @@ import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.main.YoshiBot; import de.yannicpunktdee.yoshibot.utils.Resources; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.VoiceChannel; import org.apache.commons.text.similarity.JaccardDistance; import java.awt.Color; import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; public class PlayCommand extends YoshiCommand { protected String[] requiredArguments = {"name"}; + private static final Map cooldownChecker = new HashMap<>(); + public PlayCommand(YoshiCommandContext context) { super(context); @@ -30,40 +32,19 @@ public class PlayCommand extends YoshiCommand { public boolean execute() { if (!super.execute()) return false; - if (context.containsArgument("add")) { - File download = downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name")); - if (download.isFile()) sendInfoMessage("Audio erfolgreich hinzugefügt."); - else sendErrorMessage("Audio konnte nicht hinzugefügt werden."); + for (User user : cooldownChecker.keySet()){ + Long timestamp = cooldownChecker.get(user); + if (timestamp - System.currentTimeMillis() > 60000){ + cooldownChecker.remove(user); + } + } + + if (context.containsArguments(new String[]{"add", "name"})) { + return addSound(context.getArgument("name")); } else if (context.containsArgument("list")) { - StringBuilder sb = new StringBuilder(); - getAllFiles().forEach(name -> sb.append(name).append("\n")); - EmbedBuilder eb = new EmbedBuilder(); - eb.setTitle("Es sind folgende Audios verf\u00fcgbar:"); - eb.setColor(Color.cyan); - eb.setDescription(sb.toString()); - sendCustomMessage(eb.build()); + return listSounds(); } else if (context.containsArgument("name")) { - String requestedFile = getBestMatch(context.getArgument("name"), getAllFiles()); - if(requestedFile == null){ - sendErrorMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!", - context.getArgument("name"))); - return false; - } - File file = new File(Resources.getPathToAudioFile(requestedFile)); - if (!file.isFile()) { - sendErrorMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!", - context.getArgument("name"))); - return false; - } - VoiceChannel vc = getVoiceChannelByParam(); - if (vc == null) { - sendErrorMessage("Konnte keinen Audiochannel auswählen."); - return false; - } - context.getEvent().getMessage().getTextChannel() - .sendMessage("Danke, " + context.getEvent().getMessage().getAuthor().getName() + ". Spiele '" + - requestedFile + "' in '" + vc.getName() + "' ab").queue(); - YoshiBot.getInstance().playSound(file, vc); + return playSound(context.getArgument("name")); } else { context.getEvent().getMessage().getTextChannel().sendMessage("Blyat, keine Ahnung was du willst. Gib mal " + "Parameter").queue(); @@ -72,17 +53,73 @@ public class PlayCommand extends YoshiCommand { return true; } + private boolean addSound(String filename) { + File download = downloadAttachmentToFile(Resources.getAudioPath(), filename); + if (download.isFile()) { + sendInfoMessage("Audio erfolgreich hinzugefügt."); + return true; + } else { + sendErrorMessage("Audio konnte nicht hinzugefügt werden."); + return false; + } + } + + private boolean listSounds() { + EmbedBuilder eb = new EmbedBuilder(); + eb.setTitle("Es sind folgende Audios verf\u00fcgbar:"); + eb.setColor(Color.cyan); + eb.setDescription(String.join("\n", getAllFiles())); + sendCustomMessage(eb.build()); + return true; + } + + private boolean playSound(String requestedFile) { + if (requestedFile == null) { + sendErrorMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!", + context.getArgument("name"))); + return false; + } + requestedFile = getBestMatch(requestedFile, getAllFiles()); + File file = new File(Resources.getPathToAudioFile(requestedFile)); + if (!file.isFile()) { + sendErrorMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!", + context.getArgument("name"))); + return false; + } + VoiceChannel vc = getVoiceChannelByParam(); + if (vc == null) { + sendErrorMessage("Konnte keinen Audiochannel auswählen."); + return false; + } + User author = context.getEvent().getAuthor(); + if (YoshiBot.getInstance().jda.getVoiceChannels().parallelStream() + .flatMap(vcs -> vcs.getMembers().parallelStream()).map( + Member::getUser).noneMatch(user -> user == author)) { + if (PlayCommand.cooldownChecker.containsKey(author)){ + sendErrorMessage("Иди нахуй (geh auf Schwanz), du musst 60s warten, wenn du nicht in einem Channel " + + "bist"); + return false; + } + PlayCommand.cooldownChecker.put(author, System.currentTimeMillis()); + } + context.getEvent().getMessage().getTextChannel() + .sendMessage("Danke, " + context.getEvent().getMessage().getAuthor().getName() + ". Spiele '" + + requestedFile + "' in '" + vc.getName() + "' ab").queue(); + YoshiBot.getInstance().playSound(file, vc); + return true; + } + private String getBestMatch(String word, List choices) { double bestScore = 1.0; String bestMatching = null; - for(String file : choices){ + for (String file : choices) { double score = (new JaccardDistance()).apply(word, file); - if(score < bestScore){ - bestScore = score; + if (score < bestScore) { + bestScore = score; bestMatching = file; } } - + return bestMatching; }