|
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
4 | 4 | import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.JsonObject; |
5 | 6 | import de.cerus.jdasc.command.ApplicationCommand; |
6 | 7 | import de.cerus.jdasc.command.ApplicationCommandOptionType; |
7 | 8 | import de.cerus.jdasc.gson.ApplicationCommandOptionTypeTypeAdapter; |
8 | 9 | import de.cerus.jdasc.gson.InteractionResponseTypeAdapter; |
9 | 10 | import de.cerus.jdasc.gson.InteractionResponseTypeTypeAdapter; |
10 | 11 | import de.cerus.jdasc.gson.MessageEmbedTypeAdapter; |
11 | 12 | import de.cerus.jdasc.interaction.Interaction; |
| 13 | +import de.cerus.jdasc.interaction.followup.FollowupMessage; |
| 14 | +import de.cerus.jdasc.interaction.response.InteractionApplicationCommandCallbackData; |
12 | 15 | import de.cerus.jdasc.interaction.response.InteractionResponse; |
13 | 16 | import de.cerus.jdasc.interaction.response.InteractionResponseType; |
14 | 17 | import java.io.IOException; |
|
27 | 30 | public class DiscordHttpClient { |
28 | 31 |
|
29 | 32 | private final Gson gson; |
30 | | - |
31 | 33 | private final String applicationId; |
32 | 34 | private final String botToken; |
33 | 35 | private final ExecutorService executorService; |
@@ -60,7 +62,57 @@ public DiscordHttpClient(final JDA jda, final String applicationId, final String |
60 | 62 | .create(); |
61 | 63 | } |
62 | 64 |
|
63 | | - public CompletableFuture<Response> submitInteractionReply(final Interaction interaction, final InteractionResponse response) { |
| 65 | + public CompletableFuture<Response> deleteFollowupMessage(final Interaction interaction, final long messageId) { |
| 66 | + return this.execute(new Request.Builder() |
| 67 | + .url(String.format("https://discord.com/api/v8/webhooks/%s/%s/messages/%d", this.applicationId, interaction.getToken(), messageId)) |
| 68 | + .delete() |
| 69 | + .addHeader("Authorization", "Bot " + this.botToken) |
| 70 | + .build(), 200, 204); |
| 71 | + } |
| 72 | + |
| 73 | + public CompletableFuture<Response> editFollowupMessage(final Interaction interaction, final long messageId, final FollowupMessage message) { |
| 74 | + final JsonObject jsonObject = this.gson.toJsonTree(message).getAsJsonObject(); |
| 75 | + jsonObject.remove("tts"); |
| 76 | + jsonObject.remove("username"); |
| 77 | + jsonObject.remove("avatar_url"); |
| 78 | + final String body = jsonObject.toString(); |
| 79 | + |
| 80 | + return this.execute(new Request.Builder() |
| 81 | + .url(String.format("https://discord.com/api/v8/webhooks/%s/%s/messages/%d", this.applicationId, interaction.getToken(), messageId)) |
| 82 | + .patch(RequestBody.create(body, MediaType.get("application/json; charset=utf-8"))) |
| 83 | + .addHeader("Authorization", "Bot " + this.botToken) |
| 84 | + .build(), 200, 204); |
| 85 | + } |
| 86 | + |
| 87 | + public CompletableFuture<Response> submitFollowupMessage(final Interaction interaction, final FollowupMessage message) { |
| 88 | + final String body = this.gson.toJson(message); |
| 89 | + return this.execute(new Request.Builder() |
| 90 | + .url(String.format("https://discord.com/api/v8/webhooks/%s/%s", this.applicationId, interaction.getToken())) |
| 91 | + .post(RequestBody.create(body, MediaType.get("application/json; charset=utf-8"))) |
| 92 | + .addHeader("Authorization", "Bot " + this.botToken) |
| 93 | + .build(), 200, 204, 201); |
| 94 | + } |
| 95 | + |
| 96 | + public CompletableFuture<Response> deleteInteractionResponse(final Interaction interaction) { |
| 97 | + return this.execute(new Request.Builder() |
| 98 | + .url(String.format("https://discord.com/api/v8/webhooks/%s/%s/messages/@original", this.applicationId, interaction.getToken())) |
| 99 | + .delete() |
| 100 | + .addHeader("Authorization", "Bot " + this.botToken) |
| 101 | + .build(), 204); |
| 102 | + } |
| 103 | + |
| 104 | + public CompletableFuture<Response> editInteractionResponse(final Interaction interaction, final InteractionApplicationCommandCallbackData data) { |
| 105 | + final JsonObject payload = this.gson.toJsonTree(data).getAsJsonObject(); |
| 106 | + payload.remove("tts"); |
| 107 | + final String body = payload.toString(); |
| 108 | + return this.execute(new Request.Builder() |
| 109 | + .url(String.format("https://discord.com/api/v8/webhooks/%s/%s/messages/@original", this.applicationId, interaction.getToken())) |
| 110 | + .patch(RequestBody.create(body, MediaType.get("application/json; charset=utf-8"))) |
| 111 | + .addHeader("Authorization", "Bot " + this.botToken) |
| 112 | + .build(), 200, 204); |
| 113 | + } |
| 114 | + |
| 115 | + public CompletableFuture<Response> submitInteractionResponse(final Interaction interaction, final InteractionResponse response) { |
64 | 116 | final String body = this.gson.toJson(response); |
65 | 117 | return this.execute(new Request.Builder() |
66 | 118 | .url(String.format("https://discord.com/api/v8/interactions/%d/%s/callback", interaction.getId(), interaction.getToken())) |
|
0 commit comments