From 37ee7a926d657d04e180b734e84cc9cc6c290c58 Mon Sep 17 00:00:00 2001 From: yl60lepu Date: Thu, 8 Apr 2021 18:40:45 +0200 Subject: [PATCH] =?UTF-8?q?Bissl=20was=20gefixt,=20unn=C3=B6tige=20Kommand?= =?UTF-8?q?os=20rausgenommen=20und=20vor=20allem=20das=20senden=20von=20Me?= =?UTF-8?q?ssages=20vereinheitlicht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yoshibot/command/YoshiCommand.java | 54 +++++++++++++------ .../command/YoshiCommandDistributor.java | 8 --- .../command/commands/BonkCommand.java | 4 +- .../command/commands/DeleteCommand.java | 4 -- .../command/commands/HelpCommand.java | 2 +- .../command/commands/JokeCommand.java | 5 +- .../yoshibot/command/commands/PatCommand.java | 8 +-- .../command/commands/PlayCommand.java | 30 ++++++----- .../command/commands/SauceCommand.java | 6 +-- .../command/commands/StopCommand.java | 23 -------- .../command/commands/WikipediaCommand.java | 2 +- 11 files changed, 66 insertions(+), 80 deletions(-) delete mode 100644 app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/DeleteCommand.java delete mode 100644 app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/StopCommand.java diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java index d3160bf..f432158 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java @@ -1,20 +1,15 @@ package de.yannicpunktdee.yoshibot.command; -import de.yannicpunktdee.yoshibot.audio.AudioLoadResultHandlerImpl; -import de.yannicpunktdee.yoshibot.command.commands.SayCommand; import de.yannicpunktdee.yoshibot.main.YoshiBot; import de.yannicpunktdee.yoshibot.utils.Logger; import de.yannicpunktdee.yoshibot.utils.Resources; -import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message.Attachment; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.VoiceChannel; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import java.io.BufferedReader; +import java.awt.*; import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -55,19 +50,44 @@ public abstract class YoshiCommand { */ public boolean execute() { if (!context.containsArguments(requiredArguments)) { - sendMessage("Fehlende Argumente"); + sendErrorMessage("Fehlende Argumente"); return false; } return true; } - - protected final void sendMessage(String message) { - context.getEvent().getTextChannel().sendMessage(message).queue(); + + protected final void sendMessage(String message){ + EmbedBuilder eb = new EmbedBuilder(); + eb.setTitle("INFO"); + eb.setColor(Color.pink); + eb.setDescription(message); + context.getEvent().getTextChannel().sendMessage(eb.build()).queue(); } - - protected final void sendMessage(MessageEmbed messageEmbed) { + protected final void sendFile(File file, String description){ + EmbedBuilder eb = new EmbedBuilder(); + eb.setTitle("INFO"); + eb.setColor(Color.pink); + if(description != null) eb.setDescription(description); + context.getEvent().getTextChannel().sendFile(file).queue(); + } + protected final void sendInfoMessage(String message){ + EmbedBuilder eb = new EmbedBuilder(); + eb.setTitle("INFO"); + eb.setColor(Color.blue); + eb.setDescription(message); + context.getEvent().getTextChannel().sendMessage(eb.build()).queue(); + } + protected final void sendErrorMessage(String message) { + EmbedBuilder eb = new EmbedBuilder(); + eb.setTitle("ERROR"); + eb.setColor(Color.red); + eb.setDescription(message); + context.getEvent().getTextChannel().sendMessage(eb.build()).queue(); + } + protected final void sendCustomMessage(MessageEmbed messageEmbed) { context.getEvent().getTextChannel().sendMessage(messageEmbed).queue(); } + protected File downloadAttachmentToFile(String directoryPath, String name) { if (directoryPath == null) directoryPath = Resources.getTempPath(); @@ -75,7 +95,7 @@ public abstract class YoshiCommand { if (!(new File(directoryPath)).isDirectory()) { Logger.logError("Das Download-Verzeichnis wurde nicht gefunden."); - sendMessage("Der Anhang konnte nicht gedownloaded werden."); + sendErrorMessage("Der Anhang konnte nicht gedownloaded werden."); return null; } @@ -88,17 +108,17 @@ public abstract class YoshiCommand { File file = new File(directoryPath + name + "." + attachment.getFileExtension()); CompletableFuture future = attachment.downloadToFile(file); future.exceptionally(e -> { - sendMessage("Ein Anhang konnte nicht gedownloaded werden."); + sendErrorMessage("Ein Anhang konnte nicht gedownloaded werden."); return null; }); try { future.get(); } catch (InterruptedException | ExecutionException e) { - sendMessage("Ein Anhang konnte nicht gedownloaded werden."); + sendErrorMessage("Ein Anhang konnte nicht gedownloaded werden."); return null; } if (!file.exists()) { - sendMessage("Ein Anhang konnte nicht gedownloaded werden."); + sendErrorMessage("Ein Anhang konnte nicht gedownloaded werden."); return null; } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java index 32207c8..1c70863 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java @@ -38,9 +38,6 @@ public class YoshiCommandDistributor { YoshiCommand command = null; switch (context.getAction()) { - case STOP: - command = new StopCommand(context); - break; case HELP: command = new HelpCommand(context); break; @@ -83,10 +80,6 @@ public class YoshiCommandDistributor { * Sende eine Hilfe-Nachricht, in der die Benutzung des Yoshi-Bots dokumentiert ist. */ HELP, - /** - * Fahre den Yoshi-Bot herunter (nur mit speziellen Berechtigungen m�glich). - */ - STOP, /** * Erzählt einen Jokus. */ @@ -108,7 +101,6 @@ public class YoshiCommandDistributor { /** * L�scht die Ressource, die �ber -name spezifiziert wurde. Mit -type wird der Ressourcentyp festgelegt. */ - DELETE, SAUCE, PAT, BONK, diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java index 22bba04..8f4e540 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java @@ -38,11 +38,11 @@ public class BonkCommand extends YoshiCommand { writer.close(); output.close(); } catch (IOException e) { - sendMessage("GIF konnte nicht erstellt werden."); + sendErrorMessage("GIF konnte nicht erstellt werden."); return false; } - context.getEvent().getTextChannel().sendFile(outFile).queue(); + sendFile(outFile, null); return true; } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/DeleteCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/DeleteCommand.java deleted file mode 100644 index b3b9a65..0000000 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/DeleteCommand.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.yannicpunktdee.yoshibot.command.commands; - -public class DeleteCommand { -} diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/HelpCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/HelpCommand.java index eaabb6c..d3ec73c 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/HelpCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/HelpCommand.java @@ -35,7 +35,7 @@ public class HelpCommand extends YoshiCommand { "die tags und ab geht der rechte Arm", false); eb.addField("pat", "Sendet ein Pat-Gif mit dem angehangenen oder alternativ mit -name spezifiziertem " + "(muss dann aber auf dem Server vorhanden sein) Bild.)", false); - sendMessage(eb.build()); + sendCustomMessage(eb.build()); return true; } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java index 80eb910..5c6e73c 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java @@ -7,7 +7,6 @@ import net.dv8tion.jda.api.entities.TextChannel; import org.json.JSONException; import org.json.JSONObject; -import java.io.IOException; import java.util.List; import java.util.Random; @@ -54,13 +53,13 @@ public class JokeCommand extends YoshiCommand { if (context.containsArguments(new String[]{"channel"})) { String arg = context.getArgument("channel"); if (arg == null) { - sendMessage("Es wurde kein channel angegeben."); + sendErrorMessage("Es wurde kein channel angegeben."); return false; } List channels = YoshiBot.getInstance().jda .getTextChannelsByName(context.getArgument("channel"), true); if (channels.isEmpty()) { - sendMessage("Der Kanalname konnte nicht gefunden werden."); + sendErrorMessage("Der Kanalname konnte nicht gefunden werden."); return false; } channels.get(0).sendMessage(message).queue(); diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java index 47805f9..647da69 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java @@ -4,7 +4,6 @@ import de.yannicpunktdee.yoshibot.command.YoshiCommand; import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.utils.GifSequenceWriter; import de.yannicpunktdee.yoshibot.utils.Resources; -import net.dv8tion.jda.api.entities.Message; import javax.imageio.ImageIO; import javax.imageio.stream.FileImageOutputStream; @@ -13,10 +12,7 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.util.List; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; public class PatCommand extends YoshiCommand { @@ -44,12 +40,12 @@ public class PatCommand extends YoshiCommand { writer.close(); output.close(); } catch (IOException e) { - sendMessage("Gif konnte nicht erstellt werden,"); + sendErrorMessage("Gif konnte nicht erstellt werden,"); e.printStackTrace(); return false; } - context.getEvent().getTextChannel().sendFile(outFile).queue(); + sendFile(outFile, null); return true; } 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 a9503a0..f744583 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 @@ -3,14 +3,17 @@ package de.yannicpunktdee.yoshibot.command.commands; import de.yannicpunktdee.yoshibot.command.YoshiCommand; import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.main.YoshiBot; -import de.yannicpunktdee.yoshibot.utils.Logger; import de.yannicpunktdee.yoshibot.utils.Resources; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.VoiceChannel; import java.awt.*; import java.io.File; public class PlayCommand extends YoshiCommand { + + protected String[] requiredArguments = {"name"}; + public PlayCommand(YoshiCommandContext context) { super(context); @@ -21,8 +24,10 @@ public class PlayCommand extends YoshiCommand { public boolean execute() { if (!super.execute()) return false; - if (context.containsArguments(new String[]{"add", "name"})) { - downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name")); + if (context.containsArguments(new String[]{"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."); } else if (context.containsArguments(new String[]{"list"})) { File audioDirectory = new File(Resources.getAudioPath()); StringBuilder sb = new StringBuilder(); @@ -34,21 +39,22 @@ public class PlayCommand extends YoshiCommand { } EmbedBuilder eb = new EmbedBuilder(); eb.setTitle("Es sind folgende Audios verf\u00fcgbar:"); - eb.setColor(Color.blue); + eb.setColor(Color.cyan); eb.setDescription(sb.toString()); - sendMessage(eb.build()); - } else if (context.containsArguments(new String[]{"name"})) { + sendCustomMessage(eb.build()); + } else { File file = new File(Resources.getPathToAudioFile(context.getArgument("name"))); if(!file.isFile()){ - sendMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!", + sendErrorMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!", context.getArgument("name"))); return false; } - - Logger.logDebug(getVoiceChannelByParam().getName()); - YoshiBot.getInstance().playSound(file, getVoiceChannelByParam()); - }else { - sendMessage("Unzureichende Parameter."); + VoiceChannel vc = getVoiceChannelByParam(); + if(vc == null){ + sendErrorMessage("Konnte keinen Audiochannel auswählen."); + return false; + } + YoshiBot.getInstance().playSound(file, vc); } return true; diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SauceCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SauceCommand.java index 4ee8195..b4e4e31 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SauceCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SauceCommand.java @@ -26,14 +26,14 @@ public class SauceCommand extends YoshiCommand { public boolean execute() { if (!super.execute()) return false; if (!context.getEvent().getTextChannel().isNSFW()) { - sendMessage("Dieser Kanal is nix gut, weil vong nsfw her. Geh woanders hin du kek"); + sendErrorMessage("Dieser Kanal is nix gut, weil vong nsfw her. Geh woanders hin du kek"); return true; } List arguments = Arrays.asList(context.getArgument("tags").split(" ")); try { - arguments.stream().map(Integer::parseInt).map(this::byIndex).forEach(this::sendMessage); + arguments.stream().map(Integer::parseInt).map(this::byIndex).forEach(this::sendCustomMessage); } catch (Exception e) { - sendMessage(byTags(arguments)); + sendCustomMessage(byTags(arguments)); } return true; } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/StopCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/StopCommand.java deleted file mode 100644 index a29de19..0000000 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/StopCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.yannicpunktdee.yoshibot.command.commands; - -import de.yannicpunktdee.yoshibot.command.YoshiCommand; -import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; -import de.yannicpunktdee.yoshibot.main.YoshiBot; - -public class StopCommand extends YoshiCommand { - - protected final String[] requiredArguments = {"please", "uwu"}; - - - public StopCommand(YoshiCommandContext context) { - super(context); - } - - @Override - public boolean execute() { - if(!super.execute()) return false; - YoshiBot.getInstance().stop(); - return true; - } - -} diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java index 71ab287..fcaa078 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java @@ -31,7 +31,7 @@ public class WikipediaCommand extends YoshiCommand { JSONObject articleBase = new JSONObject(RestHelper.getFromURL(url)); JSONObject pages = articleBase.getJSONObject("query").getJSONObject("pages"); if (pages.has("-1")) { - sendMessage("Kein Artikel namens " + context.getArgument("name") + " gefunden!"); + sendErrorMessage("Kein Artikel namens " + context.getArgument("name") + " gefunden!"); Logger.logWarning("Konnte Artikel " + context.getArgument("name") + " nicht finden!"); } assert pages.keySet().stream().findFirst().isPresent();