Skip to content

Commit 6758aa0

Browse files
authored
Add hugemojis even for unicode emojis (#978)
* Add hugemojis even for default emojis * Fix tests... * Refactor
1 parent 8e755a2 commit 6758aa0

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.nincodedo.ninbot.components.hugemoji;
2+
3+
import org.springframework.cloud.openfeign.FeignClient;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
7+
@FeignClient(name = "ninbot-utils-emojis", url = "https://ninbot-utils.nincodedo.dev/v1/emojis", configuration =
8+
EmojisFeignConfiguration.class)
9+
public interface EmojisFeign {
10+
11+
@GetMapping
12+
feign.Response getEmoji(@RequestParam String emoji);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.nincodedo.ninbot.components.hugemoji;
2+
3+
import feign.RequestInterceptor;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
7+
public class EmojisFeignConfiguration {
8+
9+
@Bean
10+
public RequestInterceptor addAccess(@Value("${nincodedo.utils.access.id}") String accessId,
11+
@Value("${nincodedo.utils.access.secret}") String accessSecret) {
12+
return template -> template.header("CF-Access-Client-Id", accessId)
13+
.header("CF-Access-Client-Secret", accessSecret)
14+
.header("User-Agent", "Ninbot (https://github.com/Nincodedo/Ninbot)");
15+
}
16+
}

ninbot-app/src/main/java/dev/nincodedo/ninbot/components/hugemoji/HugemojiCommand.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public class HugemojiCommand implements SlashCommand {
3535

3636
private SecureRandom random;
3737
private SupporterCheck supporterCheck;
38+
private EmojisFeign emojisFeign;
3839

39-
public HugemojiCommand(SupporterCheck supporterCheck) {
40+
public HugemojiCommand(SupporterCheck supporterCheck, EmojisFeign emojisFeign) {
4041
random = new SecureRandom();
4142
this.supporterCheck = supporterCheck;
43+
this.emojisFeign = emojisFeign;
4244
}
4345

4446
@Override
@@ -51,7 +53,22 @@ public MessageExecutor execute(@NotNull SlashCommandInteractionEvent event,
5153
var emojiUnion = Emoji.fromFormatted(emojiOption);
5254
var emojiType = emojiUnion.getType();
5355
if (emojiType == Type.UNICODE) {
54-
messageExecutor.addMessageResponse(emojiUnion.asUnicode().getName());
56+
var emojiCode = emojiUnion.asUnicode().getAsCodepoints().substring(2);
57+
var emojiResponse = emojisFeign.getEmoji(emojiCode);
58+
if (emojiResponse.status() == 200) {
59+
try {
60+
event.replyFiles(FileUpload.fromData(emojiResponse.body().asInputStream(), STR."\{emojiCode}.png"))
61+
.queue();
62+
} catch (IOException e) {
63+
log.error("Failed to get emoji image with emoji code {}. Falling back to just posting the emoji."
64+
, emojiCode, e);
65+
messageExecutor.addMessageResponse(emojiUnion.asUnicode().getName());
66+
}
67+
} else {
68+
log.error("Did not get a successful response from emojis API for emoji code {}, response code {}."
69+
+ " Falling back to just posting the emoji.", emojiCode, emojiResponse.status());
70+
messageExecutor.addMessageResponse(emojiUnion.asUnicode().getName());
71+
}
5572
} else if (emojiType == Type.CUSTOM) {
5673
var customEmoji = emojiUnion.asCustom();
5774
var imageFileType = customEmoji.getImageUrl().substring(customEmoji.getImageUrl().lastIndexOf('.'));
@@ -99,8 +116,7 @@ private Optional<InputStream> getImageInputStream(@NotNull SlashCommandInteracti
99116
return Optional.of(inputStream);
100117
}
101118

102-
private InputStream biggifyImage(BufferedImage image, String imageFileType, boolean isSupporter)
103-
throws IOException {
119+
private InputStream biggifyImage(BufferedImage image, String imageFileType, boolean isSupporter) throws IOException {
104120
var lowerBound = (int) (Math.max(image.getHeight(), image.getWidth()) * getLowerMultiplier(isSupporter));
105121
var upperBound = Math.max(image.getHeight(), image.getWidth()) * getUpperMultiplier(isSupporter);
106122
var randomNewSize = random.nextInt(lowerBound, upperBound);
@@ -128,7 +144,7 @@ public String getName() {
128144

129145
@Override
130146
public List<OptionData> getCommandOptions() {
131-
return List.of(new OptionData(OptionType.STRING, HugemojiCommandName.Option.EMOTE.get(), "The emote to "
132-
+ "biggify.", true));
147+
return List.of(new OptionData(OptionType.STRING, HugemojiCommandName.Option.EMOTE.get(),
148+
"The emote to " + "biggify.", true));
133149
}
134150
}

ninbot-app/src/test/resources/application.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ nincodedo:
3232
clientId: 1
3333
clientSecret: 1
3434
steamgriddbapikey: 1
35+
utils:
36+
access:
37+
id: 1
38+
secret: 1

0 commit comments

Comments
 (0)