Skip to content

Commit 373e85c

Browse files
committed
* Embed builder
* Added support for embeds in reply
1 parent 4d32ce6 commit 373e85c

File tree

3 files changed

+170
-32
lines changed

3 files changed

+170
-32
lines changed

core/src/main/java/com/javadiscord/jdi/core/interaction/SlashCommandEvent.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.javadiscord.jdi.core.interaction;
22

3+
import java.util.List;
34
import java.util.Optional;
45

56
import com.javadiscord.jdi.core.Discord;
@@ -14,6 +15,7 @@
1415
import com.javadiscord.jdi.core.models.guild.Interaction;
1516
import com.javadiscord.jdi.core.models.guild.InteractionData;
1617
import com.javadiscord.jdi.core.models.guild.InteractionType;
18+
import com.javadiscord.jdi.core.models.message.embed.Embed;
1719
import com.javadiscord.jdi.core.models.user.User;
1820
import com.javadiscord.jdi.internal.api.DiscordResponseFuture;
1921

@@ -60,49 +62,54 @@ public void deferReplyWithoutSpinner() {
6062
deferred = true;
6163
}
6264

63-
public AsyncResponse<String> reply(String message) {
64-
AsyncResponse<String> asyncResponse = new AsyncResponse<>();
65+
public AsyncResponse<String> reply(CallbackMessageBuilder builder) {
66+
return sendReply(builder);
67+
}
6568

66-
if (!deferred) {
67-
CallbackMessageBuilder builder =
68-
new CallbackMessageBuilder(
69-
CallbackResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
70-
interaction.id(),
71-
interaction.token()
72-
);
73-
builder.message(new CallbackMessage().setContent(message));
74-
DiscordResponseFuture future = discord.sendRequest(builder.build());
75-
future.onSuccess(res -> {
76-
if (res.status() >= 200 && res.status() < 300) {
77-
asyncResponse.setResult(res.body());
78-
} else {
79-
asyncResponse.setException(
80-
new Exception("Received " + res.status() + "\n" + res.body())
81-
);
82-
}
83-
});
84-
future.onError(asyncResponse::setException);
85-
return asyncResponse;
86-
}
69+
public AsyncResponse<String> reply(Embed... embed) {
70+
return sendReply(
71+
new CallbackMessageBuilder(
72+
CallbackResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
73+
interaction.id(),
74+
interaction.token()
75+
)
76+
.message(new CallbackMessage().setEmbeds(List.of(embed)))
77+
);
78+
}
8779

88-
CallbackMessageBuilder builder =
80+
public AsyncResponse<String> reply(String message) {
81+
return sendReply(
8982
new CallbackMessageBuilder(
9083
CallbackResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
9184
interaction.id(),
9285
interaction.token()
93-
);
94-
builder.applicationId(discord.getApplicationId());
95-
builder.message(new CallbackMessage().setContent(message));
96-
DiscordResponseFuture future = discord.sendRequest(builder.buildEdit());
86+
)
87+
.message(new CallbackMessage().setContent(message))
88+
);
89+
}
90+
91+
private AsyncResponse<String> sendReply(CallbackMessageBuilder builder) {
92+
AsyncResponse<String> asyncResponse = new AsyncResponse<>();
93+
DiscordResponseFuture future;
94+
95+
if (deferred) {
96+
builder.applicationId(discord.getApplicationId());
97+
future = discord.sendRequest(builder.buildEdit());
98+
} else {
99+
future = discord.sendRequest(builder.build());
100+
}
101+
97102
future.onSuccess(res -> {
98103
if (res.status() >= 200 && res.status() < 300) {
99104
asyncResponse.setResult(res.body());
100105
} else {
101-
asyncResponse
102-
.setException(new Exception("Received " + res.status() + "\n" + res.body()));
106+
asyncResponse.setException(
107+
new Exception("Received " + res.status() + "\n" + res.body())
108+
);
103109
}
104110
});
105111
future.onError(asyncResponse::setException);
112+
106113
return asyncResponse;
107114
}
108115

example/echo-bot/src/main/java/com/javadiscord/jdi/example/ExampleSlashCommand.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.javadiscord.jdi.example;
22

3+
import java.awt.*;
34
import java.util.Optional;
45

56
import com.javadiscord.jdi.core.CommandOptionType;
@@ -9,6 +10,7 @@
910
import com.javadiscord.jdi.core.annotations.SlashCommand;
1011
import com.javadiscord.jdi.core.interaction.SlashCommandEvent;
1112
import com.javadiscord.jdi.core.models.application.ApplicationCommandOption;
13+
import com.javadiscord.jdi.core.models.message.embed.Embed;
1214

1315
public class ExampleSlashCommand {
1416

@@ -112,7 +114,15 @@ public void handle(SlashCommandEvent event, Guild guild) {
112114
feedback.append("\nCongratulations! You you all the questions right!\n");
113115
}
114116

115-
event.reply(feedback.toString());
117+
Embed embed =
118+
new Embed.Builder()
119+
.color(Color.CYAN)
120+
.description(feedback.toString())
121+
.build();
122+
123+
event.reply(embed)
124+
.onSuccess(System.out::println)
125+
.onError(System.err::println);
116126
}
117127

118128
private static int score(String str) {

models/src/main/java/com/javadiscord/jdi/core/models/message/embed/Embed.java

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.javadiscord.jdi.core.models.message.embed;
22

3+
import java.awt.*;
4+
import java.util.ArrayList;
35
import java.util.Date;
46
import java.util.List;
57

@@ -24,4 +26,123 @@ public record Embed(
2426
@JsonProperty("provider") EmbedProvider provider,
2527
@JsonProperty("author") EmbedAuthor author,
2628
@JsonProperty("fields") List<EmbedField> fields
27-
) {}
29+
) {
30+
public static class Builder {
31+
private String title;
32+
private String type;
33+
private String description;
34+
private String url;
35+
private Date timestamp;
36+
private Integer color;
37+
private EmbedFooter footer;
38+
private EmbedImage image;
39+
private EmbedThumbnail thumbnail;
40+
private EmbedVideo video;
41+
private EmbedProvider provider;
42+
private EmbedAuthor author;
43+
private List<EmbedField> fields = new ArrayList<>();
44+
45+
public Builder title(String title) {
46+
this.title = title;
47+
return this;
48+
}
49+
50+
public Builder type(String type) {
51+
this.type = type;
52+
return this;
53+
}
54+
55+
public Builder description(String description) {
56+
this.description = description;
57+
return this;
58+
}
59+
60+
public Builder url(String url) {
61+
this.url = url;
62+
return this;
63+
}
64+
65+
public Builder timestamp(Date timestamp) {
66+
this.timestamp = timestamp;
67+
return this;
68+
}
69+
70+
public Builder color(Color color) {
71+
this.color = color.getRGB() & 0xFFFFFF;
72+
return this;
73+
}
74+
75+
public Builder footer(EmbedFooter footer) {
76+
this.footer = footer;
77+
return this;
78+
}
79+
80+
public Builder footer(String content) {
81+
this.footer = new EmbedFooter(content, null, null);
82+
return this;
83+
}
84+
85+
public Builder image(String imageURL) {
86+
this.image = new EmbedImage(imageURL, null, null, null);
87+
return this;
88+
}
89+
90+
public Builder image(EmbedImage image) {
91+
this.image = image;
92+
return this;
93+
}
94+
95+
public Builder thumbnail(String imageURL) {
96+
this.thumbnail = new EmbedThumbnail(imageURL, null, null, null);
97+
return this;
98+
}
99+
100+
public Builder thumbnail(EmbedThumbnail thumbnail) {
101+
this.thumbnail = thumbnail;
102+
return this;
103+
}
104+
105+
public Builder video(EmbedVideo video) {
106+
this.video = video;
107+
return this;
108+
}
109+
110+
public Builder provider(EmbedProvider provider) {
111+
this.provider = provider;
112+
return this;
113+
}
114+
115+
public Builder author(EmbedAuthor author) {
116+
this.author = author;
117+
return this;
118+
}
119+
120+
public Builder fields(List<EmbedField> fields) {
121+
this.fields = fields;
122+
return this;
123+
}
124+
125+
public Builder addField(EmbedField field) {
126+
this.fields.add(field);
127+
return this;
128+
}
129+
130+
public Embed build() {
131+
return new Embed(
132+
title,
133+
type,
134+
description,
135+
url,
136+
timestamp,
137+
color,
138+
footer,
139+
image,
140+
thumbnail,
141+
video,
142+
provider,
143+
author,
144+
fields
145+
);
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)