Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3e13a9

Browse files
committed
fixed but where autocrop wouldnt work if it took too long to process
1 parent 39baf0d commit e3e13a9

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

bot/autocrop/info.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this folder is where autocropped videos will temporarily be stored. all files in it will be deleted regularly!

src/MyEventListener.java

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.List;
1111
import java.util.Random;
1212
import java.util.Scanner;
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.Executors;
1315

1416
import net.dv8tion.jda.api.entities.emoji.Emoji;
1517
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
@@ -1489,59 +1491,65 @@ else if (mentionsList.size() < 1) {
14891491
}
14901492

14911493
private void AutoCropVideo(Message inputMessage, boolean autoautocrop) throws InterruptedException, IOException {
1492-
String filename;
1493-
final Emoji converting = Emoji.fromUnicode("U+1F504");
1494-
final Emoji uploading = Emoji.fromUnicode("U+1F4E4");
1495-
String autocropmessage = "";
1496-
if(autoautocrop)
1497-
autocropmessage = "Your video has large black bars in it that can be autocropped out. " +
1498-
"I will run s!autocrop on it, but if this is incorrect please report this to othello7. ";
1499-
1500-
try { //get attachments
1501-
Attachment attachment = inputMessage.getAttachments().get(0);
1502-
if(attachment.isVideo()) {
1503-
filename = (DownloadFile(attachment, "autocrop"));
1504-
} else {
1505-
channel.sendMessage("Attachment is not a video").queue();
1506-
return;
1507-
}
1508-
} catch (Exception e) { //get link
1509-
String content = inputMessage.getContentRaw();
1510-
if (content.contains("http")) {
1511-
// Extract link from message
1512-
String link = content.substring(content.indexOf("http"));
1513-
1514-
// Check if link leads to a video
1515-
//Connection.Response response = Jsoup.connect(link).execute();
1516-
String contentType = "video";//response.contentType();
1517-
1518-
if (contentType.startsWith("video")) {
1519-
filename = DownloadFile(link, "autocrop");
1494+
CompletableFuture.runAsync(() -> {
1495+
DeleteFiles("autocrop");
1496+
String filename;
1497+
final Emoji converting = Emoji.fromUnicode("U+1F504");
1498+
final Emoji uploading = Emoji.fromUnicode("U+1F4E4");
1499+
String autocropmessage = "";
1500+
if(autoautocrop)
1501+
autocropmessage = "Your video has large black bars in it that can be autocropped out. " +
1502+
"I will run s!autocrop on it, but if this is incorrect please report this to othello7. ";
1503+
1504+
try { //get attachments
1505+
Attachment attachment = inputMessage.getAttachments().get(0);
1506+
if(attachment.isVideo()) {
1507+
filename = (DownloadFile(attachment, "autocrop"));
15201508
} else {
1521-
channel.sendMessage("Link doesn't lead to a video!").queue();
1509+
channel.sendMessage("Attachment is not a video").queue();
1510+
return;
1511+
}
1512+
} catch (Exception e) { //get link
1513+
String content = inputMessage.getContentRaw();
1514+
if (content.contains("http")) {
1515+
// Extract link from message
1516+
String link = content.substring(content.indexOf("http"));
1517+
1518+
// Check if link leads to a video
1519+
//Connection.Response response = Jsoup.connect(link).execute();
1520+
String contentType = "video";//response.contentType();
1521+
1522+
if (contentType.startsWith("video")) {
1523+
filename = DownloadFile(link, "autocrop");
1524+
} else {
1525+
channel.sendMessage("Link doesn't lead to a video!").queue();
1526+
return;
1527+
}
1528+
}
1529+
else{
1530+
channel.sendMessage("No attachment or link").queue();
15221531
return;
15231532
}
15241533
}
1525-
else{
1526-
channel.sendMessage("No attachment or link").queue();
1527-
return;
1528-
}
1529-
}
1530-
1531-
//convert
1532-
inputMessage.addReaction(converting).queue();
1533-
ProcessBuilder pb = new ProcessBuilder("sh", "autocrop.sh", "autocrop/" + filename);
1534-
pb.directory(new File(System.getProperty("user.dir") + "/bot/"));
1535-
pb.start().waitFor();
15361534

1537-
//upload
1538-
inputMessage.addReaction(uploading).queue();
1539-
int dotindex = filename.lastIndexOf('.');
1540-
String newfilename = filename.substring(0, dotindex) + "_autocrop" + filename.substring(dotindex);
1541-
File cropDir = new File(System.getProperty("user.dir") + "/bot/autocrop/");
1542-
channel.sendMessage(autocropmessage + "Cropped:").addFiles(FileUpload.fromData(new File(cropDir, newfilename))).queue();
1543-
1544-
DeleteFiles("autocrop");
1535+
try {
1536+
//convert
1537+
inputMessage.addReaction(converting).queue();
1538+
ProcessBuilder pb = new ProcessBuilder("sh", "autocrop.sh", "autocrop/" + filename);
1539+
pb.directory(new File(System.getProperty("user.dir") + "/bot/"));
1540+
pb.start().waitFor();
1541+
} catch (IOException | InterruptedException e) {
1542+
channel.sendMessage("Error processing your video :(").queue();
1543+
}
1544+
1545+
1546+
//upload
1547+
inputMessage.addReaction(uploading).queue();
1548+
int dotindex = filename.lastIndexOf('.');
1549+
String newfilename = filename.substring(0, dotindex) + "_autocrop" + filename.substring(dotindex);
1550+
File cropDir = new File(System.getProperty("user.dir") + "/bot/autocrop/");
1551+
channel.sendMessage(autocropmessage + "Cropped:").addFiles(FileUpload.fromData(new File(cropDir, newfilename))).queue();
1552+
}, Executors.newSingleThreadExecutor());
15451553
}
15461554

15471555
private void UpdateEconomy() {

0 commit comments

Comments
 (0)