|
| 1 | +package org.mangorage.mangobotplugin.commands.internal; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.Message; |
| 4 | +import org.mangorage.commonutils.misc.Arguments; |
| 5 | +import org.mangorage.mangobotcore.jda.command.api.CommandResult; |
| 6 | +import org.mangorage.mangobotcore.jda.command.api.ICommand; |
| 7 | + |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.URL; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +public final class HomeDepotAlertCommand implements ICommand { |
| 18 | + private final ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 19 | + |
| 20 | + @Override |
| 21 | + public String id() { |
| 22 | + return "homedepot"; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public List<String> commands() { |
| 27 | + return List.of(id()); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public String usage() { |
| 32 | + return "!homedepot <locationID>"; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public CommandResult execute(Message message, Arguments arguments) { |
| 37 | + if (message.getAuthor().getIdLong() != 194596094200643584L) return CommandResult.DEVELOPERS_ONLY; |
| 38 | + if (message.isFromGuild()) return CommandResult.PASS; |
| 39 | + executor.submit(() -> { |
| 40 | + message.reply( |
| 41 | + createAssociateTask( |
| 42 | + arguments.getOrDefault(0, "") |
| 43 | + ) |
| 44 | + ).queue(); |
| 45 | + }); |
| 46 | + return CommandResult.PASS; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Sends a POST request to Home Depot API to create an associate help task. |
| 51 | + * @param taskId The identifier for the task (e.g., "TRA128") |
| 52 | + * @return The server response as a String |
| 53 | + */ |
| 54 | + public static String createAssociateTask(String taskId) { |
| 55 | + String targetUrl = "https://apionline.homedepot.com/v1/callForAssociateHelp/createTask/" + taskId; |
| 56 | + |
| 57 | + try { |
| 58 | + URL url = new URL(targetUrl); |
| 59 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 60 | + |
| 61 | + // Setup Request |
| 62 | + conn.setRequestMethod("POST"); |
| 63 | + conn.setRequestProperty("Accept", "application/json, text/plain, */*"); |
| 64 | + conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9"); |
| 65 | + conn.setRequestProperty("Origin", "https://www.homedepot.com"); |
| 66 | + conn.setRequestProperty("Referer", "https://www.homedepot.com/"); |
| 67 | + 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"); |
| 68 | + |
| 69 | + conn.setFixedLengthStreamingMode(0); |
| 70 | + conn.setDoOutput(true); |
| 71 | + |
| 72 | + int responseCode = conn.getResponseCode(); |
| 73 | + |
| 74 | + // Read Response |
| 75 | + try (BufferedReader br = new BufferedReader(new InputStreamReader( |
| 76 | + (responseCode < 400) ? conn.getInputStream() : conn.getErrorStream()))) { |
| 77 | + return br.lines().collect(Collectors.joining("\n")); |
| 78 | + } |
| 79 | + |
| 80 | + } catch (Exception e) { |
| 81 | + return "Error: " + e.getMessage(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments