Skip to content

Commit

Permalink
feat(chat): send Discord stickers into Minecraft (closes #74) (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
axieum authored Jun 6, 2023
1 parent f17a48d commit 8c8ca3e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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<String, PlaceholderHandler> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <ul>
* <li>{@code ${author}} &mdash; the author's nickname or username</li>
* <li>{@code ${tag}} &mdash; the author's tag (i.e. username#discriminator), e.g. Axieum#1001</li>
* <li>{@code ${username}} &mdash; the author's username, e.g. Axieum</li>
* <li>{@code ${discriminator}} &mdash; the author's username discriminator, e.g. 1001</li>
* <li>{@code ${url}} &mdash; the link to the sticker image</li>
* <li>{@code ${name}} &mdash; the name of the sticker</li>
* </ul>
*/
@Comment("""
A user sent a message that contained stickers
Usages: ${author}, ${tag}, ${username}, ${discriminator}, ${url} and ${name}""")
public String sticker = "<cmd:'@${tag} '><hover:show_text:'<i>Sent from Discord</i>'><color:#00aaff>${author}</color></hover></cmd> <dark_gray>></dark_gray> <url:'${url}'><hover:show_text:'Sticker'><underline><blue>${name}</blue></underline></hover></url>";

/** Pre-parsed 'sticker' text node. */
public transient TextNode stickerNode;

/**
* A user sent a message that contained attachments.
*
Expand Down Expand Up @@ -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);
});
}
Expand Down

0 comments on commit 8c8ca3e

Please sign in to comment.