From 33b8cc67cc140ac6d20df037ff0c6639d5850545 Mon Sep 17 00:00:00 2001 From: itstotallyjan Date: Fri, 7 Jul 2023 01:17:13 -0400 Subject: [PATCH] feat: migrate execution and editing to action classes --- .../webhook/EditWebhookMessageAction.java | 148 ++++++++++++++++++ .../action/webhook/WebhookExecuteAction.java | 147 +++++++++++++++++ .../model/webhook/IncomingWebhook.java | 55 +------ .../com/seailz/discordjar/utils/URLS.java | 9 ++ 4 files changed, 311 insertions(+), 48 deletions(-) create mode 100644 src/main/java/com/seailz/discordjar/action/webhook/EditWebhookMessageAction.java create mode 100644 src/main/java/com/seailz/discordjar/action/webhook/WebhookExecuteAction.java diff --git a/src/main/java/com/seailz/discordjar/action/webhook/EditWebhookMessageAction.java b/src/main/java/com/seailz/discordjar/action/webhook/EditWebhookMessageAction.java new file mode 100644 index 00000000..0bc986c0 --- /dev/null +++ b/src/main/java/com/seailz/discordjar/action/webhook/EditWebhookMessageAction.java @@ -0,0 +1,148 @@ +package com.seailz.discordjar.action.webhook; + +import com.seailz.discordjar.DiscordJar; +import com.seailz.discordjar.model.embed.Embed; +import com.seailz.discordjar.model.message.Attachment; +import com.seailz.discordjar.model.message.Message; +import com.seailz.discordjar.utils.Snowflake; +import com.seailz.discordjar.utils.URLS; +import com.seailz.discordjar.utils.rest.DiscordRequest; +import com.seailz.discordjar.utils.rest.Response; +import org.json.JSONArray; +import org.json.JSONObject; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.HashMap; +import java.util.List; + +public class EditWebhookMessageAction { + private String messageId; + private String content; + private List embeds; + private List attachments; + private String usernameOverride; + private String avatarUrlOverride; + private String threadName; + private Snowflake webhookId; + private String webhookToken; + private final DiscordJar discordJar; + + public EditWebhookMessageAction(String messageId, String content, List embeds, List attachments, String usernameOverride, String avatarUrlOverride, String threadName, Snowflake webhookId, String webhookToken, DiscordJar discordJar) { + this.messageId = messageId; + this.content = content; + this.embeds = embeds; + this.attachments = attachments; + this.usernameOverride = usernameOverride; + this.avatarUrlOverride = avatarUrlOverride; + this.threadName = threadName; + this.webhookId = webhookId; + this.webhookToken = webhookToken; + this.discordJar = discordJar; + } + + public String messageId() { + return messageId; + } + + public void setMessageId(String messageId) { + this.messageId = messageId; + } + + public String content() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public List embeds() { + return embeds; + } + + public void setEmbeds(List embeds) { + this.embeds = embeds; + } + + public List attachments() { + return attachments; + } + + public void setAttachments(List attachments) { + this.attachments = attachments; + } + + public String usernameOverride() { + return usernameOverride; + } + + public void setUsernameOverride(String usernameOverride) { + this.usernameOverride = usernameOverride; + } + + public String avatarUrlOverride() { + return avatarUrlOverride; + } + + public void setAvatarUrlOverride(String avatarUrlOverride) { + this.avatarUrlOverride = avatarUrlOverride; + } + + public String threadName() { + return threadName; + } + + public void setThreadName(String threadName) { + this.threadName = threadName; + } + + public Snowflake webhookId() { + return webhookId; + } + + public void setWebhookId(Snowflake webhookId) { + this.webhookId = webhookId; + } + + public String webhookToken() { + return webhookToken; + } + + public void setWebhookToken(String webhookToken) { + this.webhookToken = webhookToken; + } + + public DiscordJar discordJar() { + return discordJar; + } + + public Response run() { + Response future = new Response<>(); + new Thread(() -> { + JSONObject body = new JSONObject(); + if (content != null) body.put("content", content); + if (embeds != null) body.put("embeds", new JSONArray(embeds)); + if (attachments != null) body.put("attachments", new JSONArray(attachments)); + if (usernameOverride != null) body.put("username", usernameOverride); + if (avatarUrlOverride != null) body.put("avatar_url", avatarUrlOverride); + if (threadName != null) { + body.put("thread_name", threadName); + } + DiscordRequest request = new DiscordRequest( + body, + new HashMap<>(), + URLS.PATCH.WEBHOOK.EDIT_WEBHOOK_MESSAGE.replace("{webhook.id}", webhookId.id()).replace("{webhook.token}", webhookToken).replace("{message.id}", messageId), + discordJar, + URLS.PATCH.WEBHOOK.EDIT_WEBHOOK_MESSAGE, + RequestMethod.PATCH + ); + try { + future.complete(Message.decompile(request.invoke().body(), discordJar)); + } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) { + future.completeError(new Response.Error(e)); + } + }).start(); + + return future; + } +} diff --git a/src/main/java/com/seailz/discordjar/action/webhook/WebhookExecuteAction.java b/src/main/java/com/seailz/discordjar/action/webhook/WebhookExecuteAction.java new file mode 100644 index 00000000..78b58c8c --- /dev/null +++ b/src/main/java/com/seailz/discordjar/action/webhook/WebhookExecuteAction.java @@ -0,0 +1,147 @@ +package com.seailz.discordjar.action.webhook; + +import com.seailz.discordjar.DiscordJar; +import com.seailz.discordjar.model.embed.Embed; +import com.seailz.discordjar.model.message.Attachment; +import com.seailz.discordjar.model.message.Message; +import com.seailz.discordjar.model.webhook.IncomingWebhook; +import com.seailz.discordjar.utils.Snowflake; +import com.seailz.discordjar.utils.URLS; +import com.seailz.discordjar.utils.rest.DiscordRequest; +import com.seailz.discordjar.utils.rest.DiscordResponse; +import com.seailz.discordjar.utils.rest.Response; +import org.json.JSONArray; +import org.json.JSONObject; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.HashMap; +import java.util.List; + +public class WebhookExecuteAction { + private String content; + private List embeds; + private List attachments; + private String usernameOverride; + private String avatarUrlOverride; + private String threadName; + private final DiscordJar discordJar; + private final Snowflake channelId; + private final Snowflake webhookId; + private final Snowflake guildId; + private final String webhookToken; + + public WebhookExecuteAction(String content, List embeds, List attachments, String usernameOverride, String avatarUrlOverride, String threadName, DiscordJar discordJar, Snowflake channelId, Snowflake webhookId, Snowflake guildId, String webhookToken) { + this.content = content; + this.embeds = embeds; + this.attachments = attachments; + this.usernameOverride = usernameOverride; + this.avatarUrlOverride = avatarUrlOverride; + this.threadName = threadName; + this.discordJar = discordJar; + this.channelId = channelId; + this.webhookId = webhookId; + this.guildId = guildId; + this.webhookToken = webhookToken; + } + + public String content() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public List embeds() { + return embeds; + } + + public void setEmbeds(List embeds) { + this.embeds = embeds; + } + + public List attachments() { + return attachments; + } + + public void setAttachments(List attachments) { + this.attachments = attachments; + } + + public String usernameOverride() { + return usernameOverride; + } + + public void setUsernameOverride(String usernameOverride) { + this.usernameOverride = usernameOverride; + } + + public String avatarUrlOverride() { + return avatarUrlOverride; + } + + public void setAvatarUrlOverride(String avatarUrlOverride) { + this.avatarUrlOverride = avatarUrlOverride; + } + + public String threadName() { + return threadName; + } + + public void setThreadName(String threadName) { + this.threadName = threadName; + } + + public DiscordJar discordJar() { + return discordJar; + } + + public Snowflake channelId() { + return channelId; + } + + public Snowflake webhookId() { + return webhookId; + } + + public Snowflake guildId() { + return guildId; + } + + public String webhookToken() { + return webhookToken; + } + + public Response run() { + Response future = new Response<>(); + new Thread(() -> { + + JSONObject body = new JSONObject(); + if (content != null) body.put("content", content); + if (embeds != null) body.put("embeds", new JSONArray(embeds)); + if (attachments != null) body.put("attachments", new JSONArray(attachments)); + if (usernameOverride != null) body.put("username", usernameOverride); + if (avatarUrlOverride != null) body.put("avatar_url", avatarUrlOverride); + if (threadName != null) { + body.put("thread_name", threadName); + } + DiscordRequest request = new DiscordRequest( + body, + new HashMap<>(), + URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK_WITH_WAIT.replace("{guild.id}", guildId.id()).replace("{channel.id}", channelId.id()).replace("{webhook.id}", webhookId.id()).replace("{webhook.token}", webhookToken), + discordJar, + URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK_WITH_WAIT, + RequestMethod.POST + ); + try { + future.complete(Message.decompile(request.invoke().body(), discordJar)); + } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) { + future.completeError(new Response.Error(e)); + return; + } + + }).start(); + + return future; + } +} diff --git a/src/main/java/com/seailz/discordjar/model/webhook/IncomingWebhook.java b/src/main/java/com/seailz/discordjar/model/webhook/IncomingWebhook.java index 6eee4880..6372c785 100644 --- a/src/main/java/com/seailz/discordjar/model/webhook/IncomingWebhook.java +++ b/src/main/java/com/seailz/discordjar/model/webhook/IncomingWebhook.java @@ -1,8 +1,9 @@ package com.seailz.discordjar.model.webhook; import com.seailz.discordjar.DiscordJar; +import com.seailz.discordjar.action.webhook.EditWebhookMessageAction; +import com.seailz.discordjar.action.webhook.WebhookExecuteAction; import com.seailz.discordjar.core.Compilerable; -import com.seailz.discordjar.model.channel.ForumChannel; import com.seailz.discordjar.model.embed.Embed; import com.seailz.discordjar.model.message.Attachment; import com.seailz.discordjar.model.message.Message; @@ -83,29 +84,8 @@ public static IncomingWebhook decompile(JSONObject json, DiscordJar discordJar) * @param avatarUrlOverride An avatar override URL to use when sending the message. The Webhook remains unaffected. Can be skipped by setting to {@code null}. * @param threadName The name of the Thread that will be created with this message. Requires the Webhook channel to be a Forum channel. Can be skipped by setting to {@code null}. */ - public void execute(String content, List embeds, List attachments, String usernameOverride, String avatarUrlOverride, String threadName){ - JSONObject body = new JSONObject(); - if (content != null) body.put("content", content); - if (embeds != null) body.put("embeds", new JSONArray(embeds)); - if (attachments != null) body.put("attachments", new JSONArray(attachments)); - if (usernameOverride != null) body.put("username", usernameOverride); - if (avatarUrlOverride != null) body.put("avatar_url", avatarUrlOverride); - if (threadName != null) { - body.put("thread_name", threadName); - } - DiscordRequest request = new DiscordRequest( - body, - new HashMap<>(), - URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK.replace("{guild.id}", guildId().id()).replace("{channel.id}", channelId().id()).replace("{webhook.id}", id.id()).replace("{webhook.token}", token), - discordJar, - URLS.POST.GUILDS.CHANNELS.EXECUTE_WEBHOOK, - RequestMethod.POST - ); - try { - request.invoke(); - } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) { - throw new DiscordRequest.DiscordAPIErrorException(e); - } + public WebhookExecuteAction execute(String content, List embeds, List attachments, String usernameOverride, String avatarUrlOverride, String threadName){ + return new WebhookExecuteAction(content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, discordJar, channelId, id, guildId, token); } /** @@ -117,29 +97,8 @@ public void execute(String content, List embeds, List attachm * @param avatarUrlOverride An avatar override URL to use when sending the message. The Webhook remains unaffected. Can be skipped by setting to {@code null}. * @param threadName The name of the Thread that will be created with this message. Requires the Webhook channel to be a Forum channel. Can be skipped by setting to {@code null}. */ - public void editMessage(String messageId, String content, List embeds, List attachments, String usernameOverride, String avatarUrlOverride, String threadName){ - JSONObject body = new JSONObject(); - if (content != null) body.put("content", content); - if (embeds != null) body.put("embeds", new JSONArray(embeds)); - if (attachments != null) body.put("attachments", new JSONArray(attachments)); - if (usernameOverride != null) body.put("username", usernameOverride); - if (avatarUrlOverride != null) body.put("avatar_url", avatarUrlOverride); - if (threadName != null) { - body.put("thread_name", threadName); - } - DiscordRequest request = new DiscordRequest( - body, - new HashMap<>(), - URLS.PATCH.WEBHOOK.EDIT_WEBHOOK_MESSAGE.replace("{webhook.id}", id.id()).replace("{webhook.token}", token).replace("{message.id}", messageId), - discordJar, - URLS.PATCH.WEBHOOK.EDIT_WEBHOOK_MESSAGE, - RequestMethod.PATCH - ); - try { - request.invoke(); - } catch (DiscordRequest.UnhandledDiscordAPIErrorException e) { - throw new DiscordRequest.DiscordAPIErrorException(e); - } + public EditWebhookMessageAction editMessage(String messageId, String content, List embeds, List attachments, String usernameOverride, String avatarUrlOverride, String threadName){ + return new EditWebhookMessageAction(messageId, content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, id, token, discordJar); } /** @@ -170,7 +129,7 @@ public Message getMessage(String messageId) { */ public void deleteMessage(String messageId) { try { - DiscordResponse res = new DiscordRequest( + new DiscordRequest( new JSONObject(), new HashMap<>(), URLS.DELETE.CHANNEL.DELETE_WEBHOOK_MESSAGE.replace("{webhook.id}", id.id()).replace("{webhook.token}", token).replace("{message.id}", messageId), diff --git a/src/main/java/com/seailz/discordjar/utils/URLS.java b/src/main/java/com/seailz/discordjar/utils/URLS.java index 7d3d75c1..ec1c34b1 100644 --- a/src/main/java/com/seailz/discordjar/utils/URLS.java +++ b/src/main/java/com/seailz/discordjar/utils/URLS.java @@ -88,6 +88,15 @@ public static class CHANNELS { * @param webhook.token The token of the webhook */ public static final String EXECUTE_WEBHOOK = "/webhooks/{webhook.id}/{webhook.token}"; + /** + * Executes a webhook + * @param guild.id The id of the guild + * @param channel.id The id of the channel + * @param webhook.id The id of the webhook + * @param webhook.token The token of the webhook + * @param wait Whether to wait for server confirmation + */ + public static final String EXECUTE_WEBHOOK_WITH_WAIT = "/webhooks/{webhook.id}/{webhook.token}?wait=true"; } }