11package io .github .readonly .common .util ;
22
3+ import java .util .function .Predicate ;
4+ import java .util .function .Supplier ;
5+
36import io .github .readonly .command .event .Event ;
7+ import lombok .NonNull ;
48import net .dv8tion .jda .api .EmbedBuilder ;
59import net .dv8tion .jda .api .entities .MessageEmbed ;
10+ import net .dv8tion .jda .api .entities .MessageEmbed .Field ;
611import net .dv8tion .jda .api .entities .User ;
712import okhttp3 .HttpUrl ;
813
914public class Embed
1015{
11- private final String NEWLINE = "\n " ;
12- private final EmbedBuilder builder ;
13- private Event <?> event ;
16+ private final String NEWLINE = "\n " ;
17+ private final EmbedBuilder builder ;
1418
15- public static < T extends Event < T >> Embed newBuilder ( T event )
19+ public static Embed fromMessageEmbed ( MessageEmbed m )
1620 {
17- return new Embed ();
21+ return new Embed (m );
1822 }
1923
2024 public static Embed newBuilder ()
2125 {
2226 return new Embed ();
2327 }
2428
25- private < T extends Event < T >> Embed (T event )
29+ private Embed (MessageEmbed messageEmbed )
2630 {
27- this .builder = new EmbedBuilder ();
28- this .event = event ;
31+ this .builder = new EmbedBuilder (messageEmbed );
2932 }
3033
3134 private Embed ()
@@ -75,9 +78,21 @@ public Embed image(HttpUrl url)
7578 return this ;
7679 }
7780
78- public Embed setAuthor ()
81+ public Embed setAuthor (String author )
82+ {
83+ this .builder .setAuthor (author );
84+ return this ;
85+ }
86+
87+ public Embed setAuthor (User user )
7988 {
80- if (event != null )
89+ this .builder .setAuthor (user .getAsTag (), null , user .getEffectiveAvatarUrl ());
90+ return this ;
91+ }
92+
93+ public <T extends Event <T >> Embed setAuthor (T event )
94+ {
95+ if (event != null )
8196 {
8297 User author = event .getAuthor ();
8398 this .builder .setAuthor (author .getAsTag (), null , author .getEffectiveAvatarUrl ());
@@ -97,6 +112,22 @@ public Embed footer(String text)
97112 return this ;
98113 }
99114
115+ public Embed addFieldConditionally (Condition <Field > predicate )
116+ {
117+ if (predicate .pass ())
118+ {
119+ field (predicate .get ());
120+ }
121+
122+ return this ;
123+ }
124+
125+ public Embed field (Field field )
126+ {
127+ this .builder .addField (field );
128+ return this ;
129+ }
130+
100131 public Embed field (String title , String content )
101132 {
102133 this .builder .addField (title , content , false );
@@ -119,4 +150,32 @@ public final MessageEmbed toEmbed()
119150 {
120151 return this .builder .build ();
121152 }
153+
154+ public static class Condition <T > implements Supplier <T >
155+ {
156+ private final T obj ;
157+ private final Predicate <T > predicate ;
158+
159+ private Condition (@ NonNull T obj , @ NonNull Predicate <T > predicate )
160+ {
161+ this .obj = obj ;
162+ this .predicate = predicate ;
163+ }
164+
165+ public static <T > Condition <T > NotNull (@ NonNull T obj )
166+ {
167+ return new Condition <T >(obj , p -> p != null );
168+ }
169+
170+ public boolean pass ()
171+ {
172+ return predicate .test (obj );
173+ }
174+
175+ @ Override
176+ public T get ()
177+ {
178+ return obj ;
179+ }
180+ }
122181}
0 commit comments