Browse Source

Bissl was gefixt, unnötige Kommandos rausgenommen und vor allem das senden von Messages vereinheitlicht

pull/2/head
yl60lepu 4 years ago
parent
commit
37ee7a926d
11 changed files with 66 additions and 80 deletions
  1. +37
    -17
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java
  2. +0
    -8
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java
  3. +2
    -2
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java
  4. +0
    -4
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/DeleteCommand.java
  5. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/HelpCommand.java
  6. +2
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java
  7. +2
    -6
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java
  8. +18
    -12
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java
  9. +3
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SauceCommand.java
  10. +0
    -23
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/StopCommand.java
  11. +1
    -1
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java

+ 37
- 17
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java View File

@ -1,20 +1,15 @@
package de.yannicpunktdee.yoshibot.command; 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.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Logger; import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Resources; 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.Message.Attachment;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.VoiceChannel; 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.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -55,19 +50,44 @@ public abstract class YoshiCommand {
*/ */
public boolean execute() { public boolean execute() {
if (!context.containsArguments(requiredArguments)) { if (!context.containsArguments(requiredArguments)) {
sendMessage("Fehlende Argumente");
sendErrorMessage("Fehlende Argumente");
return false; return false;
} }
return true; 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(); context.getEvent().getTextChannel().sendMessage(messageEmbed).queue();
} }
protected File downloadAttachmentToFile(String directoryPath, String name) { protected File downloadAttachmentToFile(String directoryPath, String name) {
if (directoryPath == null) directoryPath = Resources.getTempPath(); if (directoryPath == null) directoryPath = Resources.getTempPath();
@ -75,7 +95,7 @@ public abstract class YoshiCommand {
if (!(new File(directoryPath)).isDirectory()) { if (!(new File(directoryPath)).isDirectory()) {
Logger.logError("Das Download-Verzeichnis wurde nicht gefunden."); Logger.logError("Das Download-Verzeichnis wurde nicht gefunden.");
sendMessage("Der Anhang konnte nicht gedownloaded werden.");
sendErrorMessage("Der Anhang konnte nicht gedownloaded werden.");
return null; return null;
} }
@ -88,17 +108,17 @@ public abstract class YoshiCommand {
File file = new File(directoryPath + name + "." + attachment.getFileExtension()); File file = new File(directoryPath + name + "." + attachment.getFileExtension());
CompletableFuture<File> future = attachment.downloadToFile(file); CompletableFuture<File> future = attachment.downloadToFile(file);
future.exceptionally(e -> { future.exceptionally(e -> {
sendMessage("Ein Anhang konnte nicht gedownloaded werden.");
sendErrorMessage("Ein Anhang konnte nicht gedownloaded werden.");
return null; return null;
}); });
try { try {
future.get(); future.get();
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
sendMessage("Ein Anhang konnte nicht gedownloaded werden.");
sendErrorMessage("Ein Anhang konnte nicht gedownloaded werden.");
return null; return null;
} }
if (!file.exists()) { if (!file.exists()) {
sendMessage("Ein Anhang konnte nicht gedownloaded werden.");
sendErrorMessage("Ein Anhang konnte nicht gedownloaded werden.");
return null; return null;
} }


+ 0
- 8
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommandDistributor.java View File

@ -38,9 +38,6 @@ public class YoshiCommandDistributor {
YoshiCommand command = null; YoshiCommand command = null;
switch (context.getAction()) { switch (context.getAction()) {
case STOP:
command = new StopCommand(context);
break;
case HELP: case HELP:
command = new HelpCommand(context); command = new HelpCommand(context);
break; break;
@ -83,10 +80,6 @@ public class YoshiCommandDistributor {
* Sende eine Hilfe-Nachricht, in der die Benutzung des Yoshi-Bots dokumentiert ist. * Sende eine Hilfe-Nachricht, in der die Benutzung des Yoshi-Bots dokumentiert ist.
*/ */
HELP, HELP,
/**
* Fahre den Yoshi-Bot herunter (nur mit speziellen Berechtigungen mglich).
*/
STOP,
/** /**
* Erzählt einen Jokus. * Erzählt einen Jokus.
*/ */
@ -108,7 +101,6 @@ public class YoshiCommandDistributor {
/** /**
* Lscht die Ressource, die ber -name spezifiziert wurde. Mit -type wird der Ressourcentyp festgelegt. * Lscht die Ressource, die ber -name spezifiziert wurde. Mit -type wird der Ressourcentyp festgelegt.
*/ */
DELETE,
SAUCE, SAUCE,
PAT, PAT,
BONK, BONK,


+ 2
- 2
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/BonkCommand.java View File

@ -38,11 +38,11 @@ public class BonkCommand extends YoshiCommand {
writer.close(); writer.close();
output.close(); output.close();
} catch (IOException e) { } catch (IOException e) {
sendMessage("GIF konnte nicht erstellt werden.");
sendErrorMessage("GIF konnte nicht erstellt werden.");
return false; return false;
} }
context.getEvent().getTextChannel().sendFile(outFile).queue();
sendFile(outFile, null);
return true; return true;
} }


+ 0
- 4
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/DeleteCommand.java View File

@ -1,4 +0,0 @@
package de.yannicpunktdee.yoshibot.command.commands;
public class DeleteCommand {
}

+ 1
- 1
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/HelpCommand.java View File

@ -35,7 +35,7 @@ public class HelpCommand extends YoshiCommand {
"die tags und ab geht der rechte Arm", false); "die tags und ab geht der rechte Arm", false);
eb.addField("pat", "Sendet ein Pat-Gif mit dem angehangenen oder alternativ mit -name spezifiziertem " + 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); "(muss dann aber auf dem Server vorhanden sein) Bild.)", false);
sendMessage(eb.build());
sendCustomMessage(eb.build());
return true; return true;
} }


+ 2
- 3
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/JokeCommand.java View File

@ -7,7 +7,6 @@ import net.dv8tion.jda.api.entities.TextChannel;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -54,13 +53,13 @@ public class JokeCommand extends YoshiCommand {
if (context.containsArguments(new String[]{"channel"})) { if (context.containsArguments(new String[]{"channel"})) {
String arg = context.getArgument("channel"); String arg = context.getArgument("channel");
if (arg == null) { if (arg == null) {
sendMessage("Es wurde kein channel angegeben.");
sendErrorMessage("Es wurde kein channel angegeben.");
return false; return false;
} }
List<TextChannel> channels = YoshiBot.getInstance().jda List<TextChannel> channels = YoshiBot.getInstance().jda
.getTextChannelsByName(context.getArgument("channel"), true); .getTextChannelsByName(context.getArgument("channel"), true);
if (channels.isEmpty()) { if (channels.isEmpty()) {
sendMessage("Der Kanalname konnte nicht gefunden werden.");
sendErrorMessage("Der Kanalname konnte nicht gefunden werden.");
return false; return false;
} }
channels.get(0).sendMessage(message).queue(); channels.get(0).sendMessage(message).queue();


+ 2
- 6
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PatCommand.java View File

@ -4,7 +4,6 @@ import de.yannicpunktdee.yoshibot.command.YoshiCommand;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.utils.GifSequenceWriter; import de.yannicpunktdee.yoshibot.utils.GifSequenceWriter;
import de.yannicpunktdee.yoshibot.utils.Resources; import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.entities.Message;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream; import javax.imageio.stream.FileImageOutputStream;
@ -13,10 +12,7 @@ import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class PatCommand extends YoshiCommand { public class PatCommand extends YoshiCommand {
@ -44,12 +40,12 @@ public class PatCommand extends YoshiCommand {
writer.close(); writer.close();
output.close(); output.close();
} catch (IOException e) { } catch (IOException e) {
sendMessage("Gif konnte nicht erstellt werden,");
sendErrorMessage("Gif konnte nicht erstellt werden,");
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
context.getEvent().getTextChannel().sendFile(outFile).queue();
sendFile(outFile, null);
return true; return true;
} }


+ 18
- 12
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java View File

@ -3,14 +3,17 @@ package de.yannicpunktdee.yoshibot.command.commands;
import de.yannicpunktdee.yoshibot.command.YoshiCommand; import de.yannicpunktdee.yoshibot.command.YoshiCommand;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext; import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.main.YoshiBot; import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Resources; import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.VoiceChannel;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.File;
public class PlayCommand extends YoshiCommand { public class PlayCommand extends YoshiCommand {
protected String[] requiredArguments = {"name"};
public PlayCommand(YoshiCommandContext context) { public PlayCommand(YoshiCommandContext context) {
super(context); super(context);
@ -21,8 +24,10 @@ public class PlayCommand extends YoshiCommand {
public boolean execute() { public boolean execute() {
if (!super.execute()) return false; 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"})) { } else if (context.containsArguments(new String[]{"list"})) {
File audioDirectory = new File(Resources.getAudioPath()); File audioDirectory = new File(Resources.getAudioPath());
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -34,21 +39,22 @@ public class PlayCommand extends YoshiCommand {
} }
EmbedBuilder eb = new EmbedBuilder(); EmbedBuilder eb = new EmbedBuilder();
eb.setTitle("Es sind folgende Audios verf\u00fcgbar:"); eb.setTitle("Es sind folgende Audios verf\u00fcgbar:");
eb.setColor(Color.blue);
eb.setColor(Color.cyan);
eb.setDescription(sb.toString()); 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"))); File file = new File(Resources.getPathToAudioFile(context.getArgument("name")));
if(!file.isFile()){ 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"))); context.getArgument("name")));
return false; 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; return true;


+ 3
- 3
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SauceCommand.java View File

@ -26,14 +26,14 @@ public class SauceCommand extends YoshiCommand {
public boolean execute() { public boolean execute() {
if (!super.execute()) return false; if (!super.execute()) return false;
if (!context.getEvent().getTextChannel().isNSFW()) { 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; return true;
} }
List<String> arguments = Arrays.asList(context.getArgument("tags").split(" ")); List<String> arguments = Arrays.asList(context.getArgument("tags").split(" "));
try { 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) { } catch (Exception e) {
sendMessage(byTags(arguments));
sendCustomMessage(byTags(arguments));
} }
return true; return true;
} }


+ 0
- 23
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/StopCommand.java View File

@ -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;
}
}

+ 1
- 1
app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java View File

@ -31,7 +31,7 @@ public class WikipediaCommand extends YoshiCommand {
JSONObject articleBase = new JSONObject(RestHelper.getFromURL(url)); JSONObject articleBase = new JSONObject(RestHelper.getFromURL(url));
JSONObject pages = articleBase.getJSONObject("query").getJSONObject("pages"); JSONObject pages = articleBase.getJSONObject("query").getJSONObject("pages");
if (pages.has("-1")) { 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!"); Logger.logWarning("Konnte Artikel " + context.getArgument("name") + " nicht finden!");
} }
assert pages.keySet().stream().findFirst().isPresent(); assert pages.keySet().stream().findFirst().isPresent();


Loading…
Cancel
Save