Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hugemojis even for unicode emojis #978

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.nincodedo.ninbot.components.hugemoji;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "ninbot-utils-emojis", url = "https://ninbot-utils.nincodedo.dev/v1/emojis", configuration =
EmojisFeignConfiguration.class)
public interface EmojisFeign {

@GetMapping
feign.Response getEmoji(@RequestParam String emoji);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.nincodedo.ninbot.components.hugemoji;

import feign.RequestInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

public class EmojisFeignConfiguration {

@Bean
public RequestInterceptor addAccess(@Value("${nincodedo.utils.access.id}") String accessId,
@Value("${nincodedo.utils.access.secret}") String accessSecret) {
return template -> template.header("CF-Access-Client-Id", accessId)
.header("CF-Access-Client-Secret", accessSecret)
.header("User-Agent", "Ninbot (https://github.com/Nincodedo/Ninbot)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public class HugemojiCommand implements SlashCommand {

private SecureRandom random;
private SupporterCheck supporterCheck;
private EmojisFeign emojisFeign;

public HugemojiCommand(SupporterCheck supporterCheck) {
public HugemojiCommand(SupporterCheck supporterCheck, EmojisFeign emojisFeign) {
random = new SecureRandom();
this.supporterCheck = supporterCheck;
this.emojisFeign = emojisFeign;
}

@Override
Expand All @@ -51,7 +53,22 @@ public MessageExecutor execute(@NotNull SlashCommandInteractionEvent event,
var emojiUnion = Emoji.fromFormatted(emojiOption);
var emojiType = emojiUnion.getType();
if (emojiType == Type.UNICODE) {
messageExecutor.addMessageResponse(emojiUnion.asUnicode().getName());
var emojiCode = emojiUnion.asUnicode().getAsCodepoints().substring(2);
var emojiResponse = emojisFeign.getEmoji(emojiCode);
if (emojiResponse.status() == 200) {
try {
event.replyFiles(FileUpload.fromData(emojiResponse.body().asInputStream(), STR."\{emojiCode}.png"))
.queue();
} catch (IOException e) {
log.error("Failed to get emoji image with emoji code {}. Falling back to just posting the emoji."
, emojiCode, e);
messageExecutor.addMessageResponse(emojiUnion.asUnicode().getName());
}
} else {
log.error("Did not get a successful response from emojis API for emoji code {}, response code {}."
+ " Falling back to just posting the emoji.", emojiCode, emojiResponse.status());
messageExecutor.addMessageResponse(emojiUnion.asUnicode().getName());
}
} else if (emojiType == Type.CUSTOM) {
var customEmoji = emojiUnion.asCustom();
var imageFileType = customEmoji.getImageUrl().substring(customEmoji.getImageUrl().lastIndexOf('.'));
Expand Down Expand Up @@ -99,8 +116,7 @@ private Optional<InputStream> getImageInputStream(@NotNull SlashCommandInteracti
return Optional.of(inputStream);
}

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

@Override
public List<OptionData> getCommandOptions() {
return List.of(new OptionData(OptionType.STRING, HugemojiCommandName.Option.EMOTE.get(), "The emote to "
+ "biggify.", true));
return List.of(new OptionData(OptionType.STRING, HugemojiCommandName.Option.EMOTE.get(),
"The emote to " + "biggify.", true));
}
}
4 changes: 4 additions & 0 deletions ninbot-app/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ nincodedo:
clientId: 1
clientSecret: 1
steamgriddbapikey: 1
utils:
access:
id: 1
secret: 1