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

feat(chat): add Discord reply messages (closes #50) #57

Merged
merged 3 commits into from
Aug 11, 2022
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
Expand Up @@ -243,8 +243,11 @@ public String format(final @Nullable String template)
try {
// If a format was specified, use it against its class type
if (format != null && !format.isBlank()) {
// String
if (value instanceof String) {
matcher.appendReplacement(builder, String.format(format, value));
// Number
if (value instanceof Number) {
} else if (value instanceof Number) {
matcher.appendReplacement(builder, new DecimalFormat(format).format(value));
// Date & Time
} else if (value instanceof Temporal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public void erroneousLazyVariables()
}, "Exceptions raised when evaluating lazy variables should be caught");
}

@Test
@DisplayName("Format strings")
public void formatStrings()
{
st.add("lorem", "Lorem ipsum dolor sit amet, consectetur.");
assertEquals("Lorem ipsum dolor sit amet...", st.format("${lorem:%.26s}..."));
assertEquals("Lorem ipsum dolor sit amet, consectetur.", st.format("${lorem:%.255s}"));
}

@Test
@DisplayName("Format decimals")
public void formatDecimals()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public static final class Discord
}
});

/**
* Called when a user sent a message in reply to another.
*/
public static final Event<ReplyReceived> REPLY_RECEIVED =
EventFactory.createArrayBacked(ReplyReceived.class, callbacks -> (st, event) -> {
for (ReplyReceived callback : callbacks) {
callback.onReplyReceivedPlaceholder(st, event);
}
});

/**
* Called when a user edited their recently sent message.
*/
Expand Down Expand Up @@ -92,6 +102,18 @@ public interface MessageReceived
void onMessageReceivedPlaceholder(StringTemplate template, MessageReceivedEvent event);
}

@FunctionalInterface
public interface ReplyReceived
{
/**
* Called when a user sent a message in reply to another.
*
* @param template mutable string template
* @param event JDA message received event instance
*/
void onReplyReceivedPlaceholder(StringTemplate template, MessageReceivedEvent event);
}

@FunctionalInterface
public interface MessageUpdated
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ public void onGenericMessageReaction(GenericMessageReactionEvent event)

final StringTemplate st = new StringTemplate();

// The issuer's nickname or username
st.add("issuer", event.getMember() != null ? event.getMember().getEffectiveName()
: event.getUser().getName());
// The issuer's tag (i.e. username#discriminator), e.g. Axieum#1001
st.add("issuer_tag", event.getUser().getAsTag());
// The issuer's username, e.g. Axieum
st.add("issuer_username", event.getUser().getName());
// The issuer's username discriminator, e.g. 1001
st.add("issuer_discriminator", event.getUser().getDiscriminator());
// The author's nickname or username
st.add("author", event.getMember() != null ? event.getMember().getEffectiveName()
// The issuer's nickname or username
st.add("issuer", event.getMember() != null ? event.getMember().getEffectiveName()
: event.getUser().getName());
// The author's tag (i.e. username#discriminator), e.g. Axieum#1001
st.add("author_tag", event.getUser().getAsTag());
// The author's username, e.g. Axieum
st.add("author_username", event.getUser().getName());
// The author's username discriminator, e.g. 1001
st.add("author_discriminator", event.getUser().getDiscriminator());
// The author's nickname or username
st.add("author", event.getMember() != null ? event.getMember().getEffectiveName()
: event.getUser().getName());
// The emote used to react
st.add("emote", emote);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.Nullable;

import me.axieum.mcmod.minecord.api.chat.event.ChatPlaceholderEvents;
import me.axieum.mcmod.minecord.api.util.StringTemplate;
Expand Down Expand Up @@ -32,6 +33,7 @@ public void onMessageReceived(MessageReceivedEvent event)
public void onText(MessageReceivedEvent event)
{
final long channelId = event.getChannel().getIdLong();
final @Nullable Message replyMessage = event.getMessage().getReferencedMessage();

/*
* Prepare a message template.
Expand All @@ -53,16 +55,41 @@ public void onText(MessageReceivedEvent event)
// The raw message contents
st.add("raw", event.getMessage().getContentRaw());

ChatPlaceholderEvents.Discord.MESSAGE_RECEIVED.invoker().onMessageReceivedPlaceholder(st, event);
// The message is in reply to another
if (replyMessage != null) {
// The replied message author's tag (i.e. username#discriminator), e.g. Axieum#1001
st.add("reply_tag", replyMessage.getAuthor().getAsTag());
// The replied message author's username, e.g. Axieum
st.add("reply_username", replyMessage.getAuthor().getName());
// The replied message author's username discriminator, e.g. 1001
st.add("reply_discriminator", replyMessage.getAuthor().getDiscriminator());
// The replied message author's nickname or username
st.add("reply_author", replyMessage.getMember() != null ? replyMessage.getMember().getEffectiveName()
: replyMessage.getAuthor().getName());
// The replied message formatted message contents
st.add("reply_message", StringUtils.discordToMinecraft(replyMessage.getContentDisplay()));
// The replied message raw message contents
st.add("reply_raw", replyMessage.getContentRaw());
}

/*
* Dispatch the message.
*/

MinecraftDispatcher.json(entry -> st.format(entry.minecraft.chat),
entry -> entry.minecraft.chat != null && entry.id == channelId);

LOGGER.info(st.format("@${tag} > ${raw}"));
// The message is a standalone message
if (replyMessage == null) {
ChatPlaceholderEvents.Discord.MESSAGE_RECEIVED.invoker().onMessageReceivedPlaceholder(st, event);
MinecraftDispatcher.json(entry -> st.format(entry.minecraft.chat),
entry -> entry.minecraft.chat != null && entry.id == channelId);
LOGGER.info(st.format("@${tag} > ${raw}"));

// The message is in reply to another
} else {
ChatPlaceholderEvents.Discord.REPLY_RECEIVED.invoker().onReplyReceivedPlaceholder(st, event);
MinecraftDispatcher.json(entry -> st.format(entry.minecraft.reply),
entry -> entry.minecraft.reply != null && entry.id == channelId);
LOGGER.info(st.format("@${tag} (in reply to @${reply_tag}) > ${raw}"));
}
}

public void onAttachment(MessageReceivedEvent event, Message.Attachment attachment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,27 @@ public static class MinecraftSchema
{
@Comment("""
A user sent a message
Usages: ${author}, ${tag}, ${username}, ${discriminator} and ${message}""")
Usages: ${author}, ${tag}, ${username}, ${discriminator}, ${message} and ${raw}""")
public String chat = "[\"\",{\"text\":\"${author}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${tag} \"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"\",{\"text\":\"Sent from Discord\",\"italic\":true}]}},{\"text\":\" > \",\"color\":\"dark_gray\"},{\"text\":\"${message}\"}]";

@Comment("""
A user sent a message in reply to another
Usages: ${author}, ${tag}, ${username}, ${discriminator}, ${message}, ${reply_author}, ${reply_tag}, ${reply_username}, ${reply_discriminator} and ${reply_message}""")
public String reply = "[\"\",{\"text\":\"${author}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${tag} \"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[{\"text\":\"Sent from Discord\",\"italic\":true}]}},\" \",{\"text\":\"(in reply to ${reply_author})\",\"color\":\"#99dcff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${reply_tag} \"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"${reply_message:%.50s}...\"]}},{\"text\":\" > \",\"color\":\"dark_gray\"},\"${message}\"]";

@Comment("""
A user edited their recently sent message
Usages: ${author}, ${tag}, ${username}, ${discriminator}, ${diff}, ${original} and ${message}""")
Usages: ${author}, ${tag}, ${username}, ${discriminator}, ${diff}, ${message}, ${raw}, ${original} and ${original_raw}""")
public String edit = "[\"\",{\"text\":\"${author}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${tag} \"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"\",{\"text\":\"Sent from Discord\",\"italic\":true}]}},{\"text\":\" > \",\"color\":\"dark_gray\"},{\"text\":\"${diff}\"}]";

@Comment("""
A user reacted to a recent message
Usages: ${reactor}, ${reactor_tag}, ${reactor_username}, ${reactor_discriminator}, ${author}, ${author_tag}, ${author_username}, ${author_discriminator} and ${emote}""")
Usages: ${issuer}, ${issuer_tag}, ${issuer_username}, ${issuer_discriminator}, ${author}, ${author_tag}, ${author_username}, ${author_discriminator} and ${emote}""")
public String react = "[\"\",{\"text\":\"${issuer}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${issuer_tag} \"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"\",{\"text\":\"Sent from Discord\",\"italic\":true}]}},{\"text\":\" reacted with \"},{\"text\":\"${emote}\",\"color\":\"green\"},{\"text\": \" to \"},{\"text\":\"${author}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${author_tag} \"}},{\"text\":\"'s message\"}]";

@Comment("""
A user removed their reaction from a recent message
Usages: ${reactor}, ${reactor_tag}, ${reactor_username}, ${reactor_discriminator}, ${author}, ${author_tag}, ${author_username}, ${author_discriminator} and ${emote}""")
Usages: ${issuer}, ${issuer_tag}, ${issuer_username}, ${issuer_discriminator}, ${author}, ${author_tag}, ${author_username}, ${author_discriminator} and ${emote}""")
public String unreact = "[\"\",{\"text\":\"${issuer}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${issuer_tag} \"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"\",{\"text\":\"Sent from Discord\",\"italic\":true}]}},{\"text\":\" removed their reaction of \"},{\"text\":\"${emote}\",\"color\":\"red\"},{\"text\": \" from \"},{\"text\":\"${author}\",\"color\":\"#00aaff\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"@${author_tag} \"}},{\"text\":\"'s message\"}]";

@Comment("""
Expand Down