11package org .mangorage .mangobotplugin .commands .internal ;
22
3+ import com .google .zxing .BinaryBitmap ;
4+ import com .google .zxing .LuminanceSource ;
5+ import com .google .zxing .MultiFormatReader ;
6+ import com .google .zxing .Result ;
7+ import com .google .zxing .client .j2se .BufferedImageLuminanceSource ;
8+ import com .google .zxing .common .HybridBinarizer ;
39import net .dv8tion .jda .api .entities .Message ;
410import org .mangorage .commonutils .misc .Arguments ;
511import org .mangorage .mangobotcore .jda .command .api .CommandResult ;
612import org .mangorage .mangobotcore .jda .command .api .ICommand ;
713
14+ import javax .imageio .ImageIO ;
15+ import java .awt .image .BufferedImage ;
816import java .io .BufferedReader ;
17+ import java .io .InputStream ;
918import java .io .InputStreamReader ;
1019import java .net .HttpURLConnection ;
1120import java .net .URL ;
@@ -36,6 +45,24 @@ public String usage() {
3645 public CommandResult execute (Message message , Arguments arguments ) {
3746 if (message .getAuthor ().getIdLong () != 194596094200643584L ) return CommandResult .DEVELOPERS_ONLY ;
3847 if (message .isFromGuild ()) return CommandResult .PASS ;
48+ if (!arguments .has (0 )) {
49+
50+ final var attachment = message .getAttachments ().getFirst ();
51+ if (attachment != null ) {
52+ executor .submit (() -> {
53+ try {
54+ message .reply (
55+ readQrFromUrl (attachment .getUrl ())
56+ ).queue ();
57+ } catch (Exception e ) {
58+ message .reply (e .getMessage ()).queue ();
59+ }
60+ });
61+ }
62+
63+ return CommandResult .PASS ;
64+ }
65+
3966 executor .submit (() -> {
4067 message .reply (
4168 createAssociateTask (
@@ -46,6 +73,23 @@ public CommandResult execute(Message message, Arguments arguments) {
4673 return CommandResult .PASS ;
4774 }
4875
76+ public static String readQrFromUrl (String imageUrl ) throws Exception {
77+ URL url = new URL (imageUrl );
78+
79+ try (InputStream is = url .openStream ()) {
80+ BufferedImage image = ImageIO .read (is );
81+ if (image == null ) {
82+ throw new IllegalArgumentException ("Not an image. Try again." );
83+ }
84+
85+ LuminanceSource source = new BufferedImageLuminanceSource (image );
86+ BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source ));
87+
88+ Result result = new MultiFormatReader ().decode (bitmap );
89+ return result .getText ();
90+ }
91+ }
92+
4993 /**
5094 * Sends a POST request to Home Depot API to create an associate help task.
5195 * @param taskId The identifier for the task (e.g., "TRA128")
@@ -65,9 +109,11 @@ public static String createAssociateTask(String taskId) {
65109 conn .setRequestProperty ("Origin" , "https://www.homedepot.com" );
66110 conn .setRequestProperty ("Referer" , "https://www.homedepot.com/" );
67111 conn .setRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36" );
112+ conn .setRequestProperty ("Content-Type" , "application/json" );
113+ conn .setRequestProperty ("Connection" , "keep-alive" );
68114
69- conn .setFixedLengthStreamingMode (0 );
70115 conn .setDoOutput (true );
116+ conn .getOutputStream ().write (new byte [0 ]);
71117
72118 int responseCode = conn .getResponseCode ();
73119
0 commit comments