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

Implement inline replies feature #1408

Merged
merged 14 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public interface Message extends ISnowflake, Formattable
"(?:\\?\\S*)?(?:#\\S*)?", // Useless query or URN appendix
Pattern.CASE_INSENSITIVE);

@Nullable
Message getReferencedMessage();

/**
* An immutable list of all mentioned {@link net.dv8tion.jda.api.entities.User Users}.
* <br>If no user was mentioned, this list is empty. Elements are sorted in order of appearance. This only
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public enum MessageType
*/
CHANNEL_FOLLOW_ADD(12),

INLINE_REPLY(19),

/**
* Unknown MessageType.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.AttachmentOption;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.internal.requests.restaction.MessageActionImpl;
import net.dv8tion.jda.internal.utils.Checks;

Expand Down Expand Up @@ -181,6 +182,24 @@ static EnumSet<Message.MentionType> getDefaultMentions()
@CheckReturnValue
MessageAction apply(@Nullable final Message message);

@Nonnull
@CheckReturnValue
MessageAction referenceById(long messageId);

@Nonnull
@CheckReturnValue
default MessageAction referenceById(@Nonnull String messageId)
{
return referenceById(MiscUtil.parseSnowflake(messageId));
}

@Nonnull
@CheckReturnValue
default MessageAction reference(@Nonnull Message message)
{
return referenceById(message.getIdLong());
}

/**
* Enable/Disable Text-To-Speech for the resulting message.
* <br>This is only relevant to MessageActions that are not {@code isEdit() == true}!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ protected void appendFormat(Formatter formatter, int width, int precision, boole
}
}

@Override
public Message getReferencedMessage()
{
unsupported();
return null;
}

@Nonnull
@Override
public Bag<User> getMentionedUsersBag()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1139,10 +1139,18 @@ public Message createMessage(DataObject jsonObject, MessageChannel chan, boolean

MessageType type = MessageType.fromId(jsonObject.getInt("type"));
ReceivedMessage message;
Message referencedMessage = null;
if (!jsonObject.isNull("referenced_message"))
{
referencedMessage = createMessage(jsonObject.getObject("referenced_message"), chan, false);
if (type == MessageType.DEFAULT)
type = MessageType.INLINE_REPLY;
}
switch (type)
{
case INLINE_REPLY:
case DEFAULT:
message = new ReceivedMessage(id, chan, type, fromWebhook,
message = new ReceivedMessage(id, chan, type, referencedMessage, fromWebhook,
mentionsEveryone, mentionedUsers, mentionedRoles, tts, pinned,
content, nonce, user, member, activity, editTime, reactions, attachments, embeds, flags);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class ReceivedMessage extends AbstractMessage
protected final long id;
protected final MessageType type;
protected final MessageChannel channel;
protected final Message referencedMessage;
protected final boolean fromWebhook;
protected final boolean mentionsEveryone;
protected final boolean pinned;
Expand All @@ -81,14 +82,15 @@ public class ReceivedMessage extends AbstractMessage
protected List<String> invites = null;

public ReceivedMessage(
long id, MessageChannel channel, MessageType type,
long id, MessageChannel channel, MessageType type, Message referencedMessage,
boolean fromWebhook, boolean mentionsEveryone, TLongSet mentionedUsers, TLongSet mentionedRoles, boolean tts, boolean pinned,
String content, String nonce, User author, Member member, MessageActivity activity, OffsetDateTime editTime,
List<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds, int flags)
{
super(content, nonce, tts);
this.id = id;
this.channel = channel;
this.referencedMessage = referencedMessage;
this.type = type;
this.api = (channel != null) ? (JDAImpl) channel.getJDA() : null;
this.fromWebhook = fromWebhook;
Expand All @@ -113,6 +115,12 @@ public JDA getJDA()
return api;
}

@Override
public Message getReferencedMessage()
{
return referencedMessage;
}

@Override
public boolean isPinned()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SystemMessage(
String content, String nonce, User author, Member member, MessageActivity activity, OffsetDateTime editTime,
List<MessageReaction> reactions, List<Attachment> attachments, List<MessageEmbed> embeds, int flags)
{
super(id, channel, type, fromWebhook, mentionsEveryone, mentionedUsers, mentionedRoles,
super(id, channel, type, null, fromWebhook, mentionsEveryone, mentionedUsers, mentionedRoles,
tts, pinned, content, nonce, author, member, activity, editTime, reactions, attachments, embeds, flags);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class MessageActionImpl extends RestActionImpl<Message> implements Messag
protected EnumSet<Message.MentionType> allowedMentions;
protected Set<String> mentionableUsers = new HashSet<>();
protected Set<String> mentionableRoles = new HashSet<>();
protected long messageReference;
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved

public static void setDefaultMentions(@Nullable Collection<Message.MentionType> allowedMentions)
{
Expand Down Expand Up @@ -185,6 +186,14 @@ public MessageActionImpl apply(final Message message)
return content(content).tts(message.isTTS());
}

@Nonnull
@Override
public MessageActionImpl referenceById(long messageId)
{
messageReference = messageId;
return this;
}

@Nonnull
@Override
@CheckReturnValue
Expand Down Expand Up @@ -480,6 +489,8 @@ protected DataObject getJSON()
if (nonce != null)
obj.put("nonce", nonce);
}
if (messageReference != 0)
obj.put("message_reference", messageReference);
obj.put("tts", tts);
if (allowedMentions != null || !mentionableUsers.isEmpty() || !mentionableRoles.isEmpty())
{
Expand Down