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

Message interface declutter: Message#getMentions() #2015

Merged
merged 31 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
29a5cad
Reworked mentions into Message#getMentions
DV8FromTheWorld Feb 5, 2022
e8e9d3b
Fixed examples
DV8FromTheWorld Feb 5, 2022
50e0214
Removed unneeded TODOs
DV8FromTheWorld Feb 5, 2022
b4c4734
Merge remote-tracking branch 'origin/master' into feature/mentions-re…
MinnDevelopment Apr 17, 2022
e931305
Finish mentions rework implementation
MinnDevelopment Apr 17, 2022
17185e4
Add interaction mentions
MinnDevelopment Apr 17, 2022
1c5b136
Fix errors
MinnDevelopment Apr 17, 2022
181fc16
Sort mentions in getMentions(...)
MinnDevelopment Apr 17, 2022
83e38bc
Add back synchronization and guild check
MinnDevelopment Apr 17, 2022
ea60fb7
Cleanup EntityBuilder#createMessage0
MinnDevelopment Apr 20, 2022
b09ce8f
Handle user mentions in interactions
MinnDevelopment Apr 20, 2022
88f7213
Fix parsing bug
MinnDevelopment Apr 20, 2022
8cfbad4
Improve mention parsing for members and update cache eagerly
MinnDevelopment Apr 20, 2022
8b7a4e1
Add missing null check
MinnDevelopment Apr 20, 2022
3597015
Parse user mentions on messages too
MinnDevelopment Apr 20, 2022
387bd73
Role mentions are only an id
MinnDevelopment Apr 20, 2022
c7392ad
Move matchEmote
MinnDevelopment Apr 20, 2022
370fbf3
Merge remote-tracking branch 'origin/master' into continued/mentions-…
MinnDevelopment Apr 21, 2022
c65d134
Make use of UserSnowflake and fix bags
MinnDevelopment Apr 21, 2022
00e6c95
Reduce code duplication
MinnDevelopment Apr 21, 2022
fd74ff3
Improve getMentions and isMentioned code
MinnDevelopment Apr 21, 2022
aa1647e
Add missing overrides
MinnDevelopment Apr 21, 2022
d7bdd6c
Rename interface
MinnDevelopment Apr 21, 2022
4702a18
Improve isRoleMentioned
MinnDevelopment Apr 21, 2022
2f5e2e4
Add missing documentation and fix a bunch of docs
MinnDevelopment Apr 22, 2022
ce61f92
Fix InteractionMentions#isUserMentioned
MinnDevelopment Apr 22, 2022
b7dbd5b
Remove Message#getEmotes
MinnDevelopment Apr 22, 2022
2053454
Fix wrong link tag
MinnDevelopment Apr 22, 2022
56bd13d
Added type-specific getChannels getters
DV8FromTheWorld Apr 27, 2022
5b99414
Apply suggestions from code review
DV8FromTheWorld Apr 28, 2022
cb24d6d
Added an example for type-specific channel list getter
DV8FromTheWorld Apr 28, 2022
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
4 changes: 2 additions & 2 deletions src/examples/java/MessageListenerExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ else if (msg.startsWith("!kick")) //Note, I used "startsWith, not equals.
if (message.isFromType(ChannelType.TEXT))
{
//If no users are provided, we can't kick anyone!
if (message.getMentionedUsers().isEmpty())
if (message.getMentions().getUsers().isEmpty())
{
channel.sendMessage("You must mention 1 or more Users to be kicked!").queue();
}
Expand All @@ -188,7 +188,7 @@ else if (msg.startsWith("!kick")) //Note, I used "startsWith, not equals.
}

//Loop over all mentioned users, kicking them one at a time. Mwauahahah!
List<User> mentionedUsers = message.getMentionedUsers();
List<User> mentionedUsers = message.getMentions().getUsers();
for (User user : mentionedUsers)
{
Member member = guild.getMember(user); //We get the member object for each mentioned user to kick them!
Expand Down
241 changes: 2 additions & 239 deletions src/main/java/net/dv8tion/jda/api/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,246 +245,9 @@ default Message getReferencedMessage()
: null;
}

/**
* 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
* counts direct mentions of the user and not mentions through roles or the everyone tag.
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return immutable list of mentioned users
*/
@Nonnull
List<User> getMentionedUsers();

/**
* A {@link org.apache.commons.collections4.Bag Bag} of mentioned users.
* <br>This can be used to retrieve the amount of times a user was mentioned in this message. This only
* counts direct mentions of the user and not mentions through roles or the everyone tag.
*
* <h2>Example</h2>
* <pre>{@code
* public void sendCount(Message msg)
* {
* List<User> mentions = msg.getMentionedUsers(); // distinct list, in order of appearance
* Bag<User> count = msg.getMentionedUsersBag();
* StringBuilder content = new StringBuilder();
* for (User user : mentions)
* {
* content.append(user.getAsTag())
* .append(": ")
* .append(count.getCount(user))
* .append("\n");
* }
* msg.getChannel().sendMessage(content.toString()).queue();
* }
* }</pre>
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return {@link org.apache.commons.collections4.Bag Bag} of mentioned users
*
* @see #getMentionedUsers()
*/
@Nonnull
Bag<User> getMentionedUsersBag();

/**
* A immutable list of all mentioned {@link net.dv8tion.jda.api.entities.TextChannel TextChannels}.
* <br>If none were mentioned, this list is empty. Elements are sorted in order of appearance.
*
* <p><b>This may include TextChannels from other {@link net.dv8tion.jda.api.entities.Guild Guilds}</b>
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return immutable list of mentioned TextChannels
*/
@Nonnull
List<TextChannel> getMentionedChannels();

/**
* A {@link org.apache.commons.collections4.Bag Bag} of mentioned channels.
* <br>This can be used to retrieve the amount of times a channel was mentioned in this message.
*
* <h2>Example</h2>
* <pre>{@code
* public void sendCount(Message msg)
* {
* List<TextChannel> mentions = msg.getMentionedTextChannels(); // distinct list, in order of appearance
* Bag<TextChannel> count = msg.getMentionedTextChannelsBag();
* StringBuilder content = new StringBuilder();
* for (TextChannel channel : mentions)
* {
* content.append("#")
* .append(channel.getName())
* .append(": ")
* .append(count.getCount(channel))
* .append("\n");
* }
* msg.getChannel().sendMessage(content.toString()).queue();
* }
* }</pre>
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return {@link org.apache.commons.collections4.Bag Bag} of mentioned channels
*
* @see #getMentionedChannels()
*/
@Nonnull
Bag<TextChannel> getMentionedChannelsBag();

/**
* A immutable list of all mentioned {@link net.dv8tion.jda.api.entities.Role Roles}.
* <br>If none were mentioned, this list is empty. Elements are sorted in order of appearance. This only
* counts direct mentions of the role and not mentions through the everyone tag.
*
* <p><b>This may include Roles from other {@link net.dv8tion.jda.api.entities.Guild Guilds}</b>
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return immutable list of mentioned Roles
*/
//TODO | Docs
@Nonnull
List<Role> getMentionedRoles();

/**
* A {@link org.apache.commons.collections4.Bag Bag} of mentioned roles.
* <br>This can be used to retrieve the amount of times a role was mentioned in this message. This only
* counts direct mentions of the role and not mentions through the everyone tag.
* If a role is not {@link net.dv8tion.jda.api.entities.Role#isMentionable() mentionable} it will not be included.
*
* <h2>Example</h2>
* <pre>{@code
* public void sendCount(Message msg)
* {
* List<Role> mentions = msg.getMentionedRoles(); // distinct list, in order of appearance
* Bag<Role> count = msg.getMentionedRolesBag();
* StringBuilder content = new StringBuilder();
* for (Role role : mentions)
* {
* content.append(role.getName())
* .append(": ")
* .append(count.getCount(role))
* .append("\n");
* }
* msg.getChannel().sendMessage(content.toString()).queue();
* }
* }</pre>
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return {@link org.apache.commons.collections4.Bag Bag} of mentioned roles
*
* @see #getMentionedRoles()
*/
@Nonnull
Bag<Role> getMentionedRolesBag();

/**
* Creates an immutable list of {@link net.dv8tion.jda.api.entities.Member Members}
* representing the users of {@link #getMentionedUsers()} in the specified
* {@link net.dv8tion.jda.api.entities.Guild Guild}.
* <br>This is only a convenience method and will skip all users that are not in the specified
* Guild.
*
* @param guild
* Non-null {@link net.dv8tion.jda.api.entities.Guild Guild}
* that will be used to retrieve Members.
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
* @throws java.lang.IllegalArgumentException
* If the specified Guild is {@code null}
*
* @return Immutable list of mentioned Members
*
* @since 3.4.0
*/
@Nonnull
List<Member> getMentionedMembers(@Nonnull Guild guild);

/**
* Creates an immutable list of {@link net.dv8tion.jda.api.entities.Member Members}
* representing the users of {@link #getMentionedUsers()} in the
* {@link net.dv8tion.jda.api.entities.Guild Guild} this Message was sent in.
* <br>This is only a convenience method and will skip all users that are not in the specified Guild.
* <br>It will provide the {@link #getGuild()} output Guild to {@link #getMentionedMembers(Guild)}.
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
* @throws java.lang.IllegalStateException
* If this message was not sent in a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}
*
* @return Immutable list of mentioned Members
*
* @since 3.4.0
*/
@Nonnull
List<Member> getMentionedMembers();

/**
* Combines all instances of {@link net.dv8tion.jda.api.entities.IMentionable IMentionable}
* filtered by the specified {@link net.dv8tion.jda.api.entities.Message.MentionType MentionType} values.
* <br>This does not include {@link #getMentionedMembers()} to avoid duplicates.
*
* <p>If no MentionType values are given this will fallback to all types.
*
* @param types
* Amount of {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes}
* to include in the list of mentions
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
* @throws java.lang.IllegalArgumentException
* If provided with {@code null}
*
* @return Immutable list of filtered {@link net.dv8tion.jda.api.entities.IMentionable IMentionable} instances
*
* @since 3.4.0
*/
@Nonnull
List<IMentionable> getMentions(@Nonnull MentionType... types);

/**
* Checks if given {@link net.dv8tion.jda.api.entities.IMentionable IMentionable}
* was mentioned in this message in any way (@User, @everyone, @here, @Role).
* <br>If no filtering {@link net.dv8tion.jda.api.entities.Message.MentionType MentionTypes} are
* specified this will fallback to all mention types.
*
* <p>{@link Message.MentionType#HERE MentionType.HERE} and {@link Message.MentionType#EVERYONE MentionType.EVERYONE}
* will only be checked, if the given {@link net.dv8tion.jda.api.entities.IMentionable IMentionable} is of type
* {@link net.dv8tion.jda.api.entities.User User} or {@link net.dv8tion.jda.api.entities.Member Member}.
* <br>Online status of Users/Members is <b>NOT</b> considered when checking {@link Message.MentionType#HERE MentionType.HERE}.
*
* @param mentionable
* The mentionable entity to check on.
* @param types
* The types to include when checking whether this type was mentioned.
* This will be used with {@link #getMentions(Message.MentionType...) getMentions(MentionType...)}
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return True, if the given mentionable was mentioned in this message
*/
boolean isMentioned(@Nonnull IMentionable mentionable, @Nonnull MentionType... types);

/**
* Indicates if this Message mentions everyone using @everyone or @here.
*
* @throws java.lang.UnsupportedOperationException
* If this is a system message
*
* @return True, if message is mentioning everyone
*/
boolean mentionsEveryone();
MessageMentions getMentions();

/**
* Returns whether or not this Message has been edited before.
Expand Down
Loading