Browse Source

Wikipedia

pull/2/head
Paul Glaß 4 years ago
parent
commit
cbffa3beb1
4 changed files with 51 additions and 29 deletions
  1. +31
    -12
      app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java
  2. +0
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/PlayCommand.java
  3. +1
    -10
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/SayCommand.java
  4. +19
    -4
      app/src/main/java/de/yannicpunktdee/yoshibot/command/commands/WikipediaCommand.java

+ 31
- 12
app/src/main/java/de/yannicpunktdee/yoshibot/command/YoshiCommand.java View File

@ -1,10 +1,14 @@
package de.yannicpunktdee.yoshibot.command;
import de.yannicpunktdee.yoshibot.audio.AudioLoadResultHandlerImpl;
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.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.File;
import java.util.List;
@ -58,23 +62,23 @@ public abstract class YoshiCommand {
protected final void sendMessage(MessageEmbed messageEmbed) {
context.getEvent().getTextChannel().sendMessage(messageEmbed).queue();
}
protected File downloadAttachmentToFile(String directoryPath, String name){
if(directoryPath == null) directoryPath = Resources.getTempPath();
if(name == null) name = UUID.randomUUID().toString();
if(!(new File(directoryPath)).isDirectory()){
protected File downloadAttachmentToFile(String directoryPath, String name) {
if (directoryPath == null) directoryPath = Resources.getTempPath();
if (name == null) name = UUID.randomUUID().toString();
if (!(new File(directoryPath)).isDirectory()) {
Logger.logError("Das Download-Verzeichnis wurde nicht gefunden.");
sendMessage("Der Anhang konnte nicht gedownloaded werden.");
return null;
}
List<Attachment> attachments = context.getEvent().getMessage().getAttachments();
if(attachments.size() == 0){
if (attachments.size() == 0) {
return null;
}
Attachment attachment = attachments.get(0);
File file = new File(directoryPath + name + "." + attachment.getFileExtension());
CompletableFuture<File> future = attachment.downloadToFile(file);
future.exceptionally(e -> {
@ -87,12 +91,27 @@ public abstract class YoshiCommand {
sendMessage("Ein Anhang konnte nicht gedownloaded werden.");
return null;
}
if(!file.exists()) {
if (!file.exists()) {
sendMessage("Ein Anhang konnte nicht gedownloaded werden.");
return null;
}
return file;
}
public static boolean sayToChannel(String path, String channel, MessageReceivedEvent event) {
List<VoiceChannel> channels = YoshiBot.getInstance().jda
.getVoiceChannelsByName(channel, true);
if (!(channels.size() > 0)) {
event.getTextChannel().sendMessage("Der Kanalname konnte nicht gefunden werden.").queue();
return false;
}
VoiceChannel vc = channels.get(0);
YoshiBot.getInstance().audioPlayerManager.loadItem(path, new AudioLoadResultHandlerImpl());
vc.getGuild().getAudioManager().openAudioConnection(vc);
return true;
}
}

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

@ -6,14 +6,11 @@ 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.Message;
import net.dv8tion.jda.api.entities.VoiceChannel;
import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class PlayCommand extends YoshiCommand {


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

@ -68,16 +68,7 @@ public class SayCommand extends YoshiCommand {
String path = buildTTSAudio(context.getArgument("text"));
List<VoiceChannel> channels = YoshiBot.getInstance().jda
.getVoiceChannelsByName(context.getArgument("channel"), true);
if (!(channels.size() > 0)) {
context.getEvent().getTextChannel().sendMessage("Der Kanalname konnte nicht gefunden werden.").queue();
return false;
}
VoiceChannel vc = channels.get(0);
YoshiBot.getInstance().audioPlayerManager.loadItem(path, new AudioLoadResultHandlerImpl());
vc.getGuild().getAudioManager().openAudioConnection(vc);
if (WikipediaCommand.sayToChannel(path, context.getArgument("channel"), context.getEvent())) return false;
return false;
}


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

@ -1,12 +1,18 @@
package de.yannicpunktdee.yoshibot.command.commands;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
import de.yannicpunktdee.yoshibot.audio.AudioLoadResultHandlerImpl;
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.RestHelper;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.List;
public class WikipediaCommand extends YoshiCommand {
/**
* Erzeugt ein neues Kommando, führt es aber noch nicht aus. Es wird ermittelt, ob die Argumentenkombination valide
@ -27,10 +33,19 @@ public class WikipediaCommand extends YoshiCommand {
JSONObject articleBase = new JSONObject(RestHelper.getFromURL(url));
JSONObject pages = articleBase.getJSONObject("query").getJSONObject("pages");
if (pages.has("-1")){
if (pages.has("-1")) {
sendMessage("Kein Artikel namens " + context.getArgument("name") + " gefunden!");
Logger.log("Konnte Artikel " + context.getArgument("name") + " nicht finden!", Type.WARNING);
Logger.logWarning("Konnte Artikel " + context.getArgument("name") + " nicht finden!");
}
return true;
assert pages.keySet().stream().findFirst().isPresent();
JSONObject page = pages.getJSONObject(pages.keySet().stream().findFirst().get());
String text = page.getString("extract");
text = String.join(" ", Arrays.asList(text.split(" ")).subList(0, 50));
sendMessage(text);
String path = SayCommand.buildTTSAudio(text);
return YoshiCommand.sayToChannel(path, context.getArgument("channel"), context.getEvent());
}
}

Loading…
Cancel
Save