Skip to content

Commit

Permalink
feat: migrate execution and editing to action classes
Browse files Browse the repository at this point in the history
  • Loading branch information
blueysh committed Jul 7, 2023
1 parent fbffc58 commit 33b8cc6
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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<Embed> embeds;
private List<Attachment> 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<Embed> embeds, List<Attachment> 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<Embed> embeds() {
return embeds;
}

public void setEmbeds(List<Embed> embeds) {
this.embeds = embeds;
}

public List<Attachment> attachments() {
return attachments;
}

public void setAttachments(List<Attachment> 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<Message> run() {
Response<Message> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Embed> embeds;
private List<Attachment> 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<Embed> embeds, List<Attachment> 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<Embed> embeds() {
return embeds;
}

public void setEmbeds(List<Embed> embeds) {
this.embeds = embeds;
}

public List<Attachment> attachments() {
return attachments;
}

public void setAttachments(List<Attachment> 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<Message> run() {
Response<Message> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Embed> embeds, List<Attachment> 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<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName){
return new WebhookExecuteAction(content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, discordJar, channelId, id, guildId, token);
}

/**
Expand All @@ -117,29 +97,8 @@ public void execute(String content, List<Embed> embeds, List<Attachment> 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<Embed> embeds, List<Attachment> 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<Embed> embeds, List<Attachment> attachments, String usernameOverride, String avatarUrlOverride, String threadName){
return new EditWebhookMessageAction(messageId, content, embeds, attachments, usernameOverride, avatarUrlOverride, threadName, id, token, discordJar);
}

/**
Expand Down Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/seailz/discordjar/utils/URLS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

Expand Down

0 comments on commit 33b8cc6

Please sign in to comment.