Skip to content
This repository has been archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #187 from book000/feat/always-embed-textimg
Browse files Browse the repository at this point in the history
feat: 画像投稿時、同時にtextimgの結果を返すようにする
  • Loading branch information
yuuahp authored Nov 24, 2022
2 parents 2442cec + a3dd560 commit ea5ec20
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
39 changes: 2 additions & 37 deletions src/main/java/com/jaoafa/jdavcspeaker/Command/Cmd_Textimg.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.jaoafa.jdavcspeaker.Framework.Command.CmdDetail;
import com.jaoafa.jdavcspeaker.Framework.Command.CmdSubstrate;
import com.jaoafa.jdavcspeaker.Lib.LibEmbedColor;
import com.jaoafa.jdavcspeaker.Lib.LibFlow;
import com.jaoafa.jdavcspeaker.Lib.MultipleServer;
import com.jaoafa.jdavcspeaker.Lib.VisionAPI;
import com.jaoafa.jdavcspeaker.Lib.*;
import com.jaoafa.jdavcspeaker.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
Expand All @@ -14,16 +11,10 @@
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Cmd_Textimg implements CmdSubstrate {
@Override
Expand Down Expand Up @@ -110,7 +101,7 @@ void generateTextImg(SlashCommandInteractionEvent event) {

File file;
try {
file = getTempimgPath(imageUrl);
file = LibTextImg.getTempimgPath(imageUrl);
} catch (IOException e) {
e.printStackTrace();
event.getHook().editOriginalEmbeds(new EmbedBuilder()
Expand All @@ -131,30 +122,4 @@ void generateTextImg(SlashCommandInteractionEvent event) {

event.getHook().editOriginal(file, "output.png").queue();
}

private File getTempimgPath(String mediaUrl) throws IOException {
File tmp = File.createTempFile("textimg", ".png");
Process p;
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(List.of("php", "external_scripts/image-text.php", mediaUrl, tmp.getAbsolutePath()));
builder.redirectErrorStream(true);
builder.directory(new File("."));
p = builder.start();
boolean bool = p.waitFor(3, TimeUnit.MINUTES);
if (!bool) {
return null;
}
InputStreamReader inputStreamReader = new InputStreamReader(p.getInputStream());
Stream<String> streamOfString = new BufferedReader(inputStreamReader).lines();
String streamToString = streamOfString.collect(Collectors.joining("\n"));
System.out.println(streamToString);
if (p.exitValue() != 0) {
return null;
}
} catch (InterruptedException e) {
return null;
}
return tmp;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/jaoafa/jdavcspeaker/Lib/LibTextImg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.jaoafa.jdavcspeaker.Lib;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LibTextImg {
public static File getTempimgPath(String mediaUrl) throws IOException {
File tmp = File.createTempFile("textimg", ".png");
Process p;
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(List.of("php", "external_scripts/image-text.php", mediaUrl, tmp.getAbsolutePath()));
builder.redirectErrorStream(true);
builder.directory(new File("."));
p = builder.start();
boolean bool = p.waitFor(3, TimeUnit.MINUTES);
if (!bool) {
return null;
}
InputStreamReader inputStreamReader = new InputStreamReader(p.getInputStream());
Stream<String> streamOfString = new BufferedReader(inputStreamReader).lines();
String streamToString = streamOfString.collect(Collectors.joining("\n"));
System.out.println(streamToString);
if (p.exitValue() != 0) {
return null;
}
} catch (InterruptedException e) {
return null;
}
return tmp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.jaoafa.jdavcspeaker.Lib.*;
import com.jaoafa.jdavcspeaker.Main;
import com.jaoafa.jdavcspeaker.Player.TrackInfo;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.requests.restaction.MessageAction;

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -82,12 +85,28 @@ void processImage(Message message, VoiceText vt, Message.Attachment attachment)
if (text != null) {
vt.play(TrackInfo.SpeakFromType.RECEIVED_IMAGE, message, "画像ファイル「%s を含む画像」が送信されました。".formatted(text.length() > 30 ? text.substring(0, 30) : text));

message
.getChannel()
.sendMessage("```\n" + safeSubstring(text.replaceAll("\n", " ")) + "\n```")
File outputFile = null;
try {
// 本来はこのタイミングでダウンロードが完了しているので、PHP側でもダウンロードするのではなくダウンロード済みファイルを利用するべき
// 気が向いたら修正
outputFile = LibTextImg.getTempimgPath(attachment.getUrl());
} catch (IOException e) {
e.printStackTrace();
}

MessageAction action;
EmbedBuilder embed = new EmbedBuilder()
.setDescription("```\n" + safeSubstring(text.replaceAll("\n", " ")) + "\n```");
if (outputFile != null) {
action = message.getChannel().sendFile(outputFile, "output.png");
embed = embed.setThumbnail("attachment://output.png");
} else {
action = message.getChannel().sendMessageEmbeds(embed.build());
}
action
.setEmbeds(embed.build())
.reference(message)
.mentionRepliedUser(false)
.queue();
.mentionRepliedUser(false).queue();
} else {
vt.play(TrackInfo.SpeakFromType.RECEIVED_IMAGE, message, "画像ファイル「 %s 」が送信されました。".formatted(attachment.getFileName()));
}
Expand Down

0 comments on commit ea5ec20

Please sign in to comment.