From 9cf1f0ae8e98b47ce3333f16d41b68a08b544ae3 Mon Sep 17 00:00:00 2001 From: Jonathan Hiles Date: Tue, 6 Jun 2023 21:37:04 +1000 Subject: [PATCH] feat(chat): send Discord stickers into Minecraft (closes #74) --- .../discord/MessageReceivedListener.java | 50 ++++++++++++++++++- .../minecord/impl/chat/config/ChatConfig.java | 21 ++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/discord/MessageReceivedListener.java b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/discord/MessageReceivedListener.java index 19155f8..9467851 100644 --- a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/discord/MessageReceivedListener.java +++ b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/discord/MessageReceivedListener.java @@ -6,6 +6,7 @@ import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderHandler; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.sticker.StickerItem; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.Nullable; @@ -35,9 +36,11 @@ public void onMessageReceived(MessageReceivedEvent event) if (!event.getMessage().getContentRaw().isEmpty()) onText(event); + // Link any stickers + event.getMessage().getStickers().forEach(sticker -> onSticker(event, sticker)); + // Link any attachments - for (Message.Attachment attachment : event.getMessage().getAttachments()) - onAttachment(event, attachment); + event.getMessage().getAttachments().forEach(attachment -> onAttachment(event, attachment)); } /** @@ -118,6 +121,49 @@ public void onText(MessageReceivedEvent event) } } + /** + * Handles a sticker of a given message received event. + * + * @param event message received event + * @param sticker message sticker + */ + public void onSticker(MessageReceivedEvent event, StickerItem sticker) + { + final long channelId = event.getChannel().getIdLong(); + + /* + * Prepare the message placeholders. + */ + + final @Nullable PlaceholderContext ctx = PlaceholdersExt.getMinecordServerContext(); + final Map placeholders = Map.of( + // The author's tag (i.e. username#discriminator), e.g. Axieum#1001 + "tag", string(event.getAuthor().getAsTag()), + // The author's username, e.g. Axieum + "username", string(event.getAuthor().getName()), + // The author's username discriminator, e.g. 1001 + "discriminator", string(event.getAuthor().getDiscriminator()), + // The author's nickname or username + "author", string( + event.getMember() != null ? event.getMember().getEffectiveName() : event.getAuthor().getName() + ), + // The link to the sticker image + "url", string(sticker.getIconUrl()), + // The name of the sticker + "name", string(sticker.getName()) + ); + + /* + * Dispatch the message. + */ + + MinecraftDispatcher.dispatch( + entry -> PlaceholdersExt.parseText(entry.minecraft.stickerNode, ctx, placeholders), + entry -> entry.minecraft.sticker != null && entry.id == channelId + ); + LOGGER.info(PlaceholdersExt.parseString("@${tag} sent sticker ${name}", ctx, placeholders)); + } + /** * Handles an attachment of a given message received event. * diff --git a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java index 4abdc45..1e6a3f0 100644 --- a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java +++ b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java @@ -440,6 +440,26 @@ public static class MinecraftSchema /** Pre-parsed 'unreact' text node. */ public transient TextNode unreactNode; + /** + * A user sent a message that contained stickers. + * + * + */ + @Comment(""" + A user sent a message that contained stickers + Usages: ${author}, ${tag}, ${username}, ${discriminator}, ${url} and ${name}""") + public String sticker = "Sent from Discord'>${author} > ${name}"; + + /** Pre-parsed 'sticker' text node. */ + public transient TextNode stickerNode; + /** * A user sent a message that contained attachments. * @@ -517,6 +537,7 @@ public void validatePostLoad() entry.minecraft.editNode = parseNode(entry.minecraft.edit); entry.minecraft.reactNode = parseNode(entry.minecraft.react); entry.minecraft.unreactNode = parseNode(entry.minecraft.unreact); + entry.minecraft.stickerNode = parseNode(entry.minecraft.sticker); entry.minecraft.attachmentNode = parseNode(entry.minecraft.attachment); }); }