Skip to content

Commit eca2b9a

Browse files
committed
add
1 parent 8ab103e commit eca2b9a

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version = "1.0"
33

44
[versions]
55

6-
jda = "5.0.0-beta.3"
6+
jda = "5.0.0-beta.4"
77
json = "20220320"
88
lang3 = "3.12.0"
99
jackson = "2.14.1"

src/main/java/io/github/readonly/common/util/Embed.java

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
package io.github.readonly.common.util;
22

3+
import java.util.function.Predicate;
4+
import java.util.function.Supplier;
5+
36
import io.github.readonly.command.event.Event;
7+
import lombok.NonNull;
48
import net.dv8tion.jda.api.EmbedBuilder;
59
import net.dv8tion.jda.api.entities.MessageEmbed;
10+
import net.dv8tion.jda.api.entities.MessageEmbed.Field;
611
import net.dv8tion.jda.api.entities.User;
712
import okhttp3.HttpUrl;
813

914
public 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
}

src/main/java/io/github/readonly/common/util/RGB.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package io.github.readonly.common.util;
2121

22+
import java.awt.Color;
2223
import java.util.HashMap;
2324
import java.util.Locale;
2425
import java.util.Map;
@@ -262,6 +263,11 @@ public static RGB parse(String str) {
262263
return new RGB(color);
263264
}
264265

266+
public static RGB colorToRGB(Color c)
267+
{
268+
return new RGB(c.getRGB());
269+
}
270+
265271
/**
266272
* Attempt to parse a color, returning the default if it is not valid.
267273
*

0 commit comments

Comments
 (0)