Skip to content

Commit

Permalink
Merge pull request Javacord#392 from Bastian/add-convenience-methods
Browse files Browse the repository at this point in the history
Add convenience methods for messages
  • Loading branch information
Bastian Oppermann authored Oct 21, 2018
2 parents 676a776 + ff52d4f commit 193a485
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/README.md.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
251bc060c9eb65a6784e93462b387c09113055857e072f66fb51c3612b055250
b26f413686399afd598db1ef815efabe301f195ce4a7ddaad5b77b4f1814b51c
4 changes: 2 additions & 2 deletions .github/README_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class MyFirstBot {

// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessage().getContent().equalsIgnoreCase("!ping")) {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
Expand All @@ -107,7 +107,7 @@ public class MyFirstBot {
new DiscordApiBuilder().setToken(token).login().thenAccept(api -> {
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessage().getContent().equalsIgnoreCase("!ping")) {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class MyFirstBot {

// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessage().getContent().equalsIgnoreCase("!ping")) {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
Expand All @@ -107,7 +107,7 @@ public class MyFirstBot {
new DiscordApiBuilder().setToken(token).login().thenAccept(api -> {
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessage().getContent().equalsIgnoreCase("!ping")) {
if (event.getMessageContent().equalsIgnoreCase("!ping")) {
event.getChannel().sendMessage("Pong!");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.DiscordEntity;
import org.javacord.api.entity.UpdatableFromCache;
import org.javacord.api.entity.channel.ChannelType;
import org.javacord.api.entity.channel.GroupChannel;
import org.javacord.api.entity.channel.PrivateChannel;
import org.javacord.api.entity.channel.ServerChannel;
Expand Down Expand Up @@ -805,12 +806,41 @@ default List<ServerTextChannel> getMentionedChannels() {
}

/**
* Returns <code>true</code> if the message was sent as a private message, returns <code>false</code> if not.
* Checks if the message was sent in a {@link ChannelType#PRIVATE_CHANNEL private channel}.
*
* @return Whether or not the message was sent as a private message.
* @return Whether or not the message was sent in a private channel.
* @deprecated Use {@link Message#isPrivateMessage()} instead.
*/
@Deprecated // Deprecated to be consistent with #isServerMessage() and #isGroupMessage()
default boolean isPrivate() {
return getChannel() instanceof PrivateChannel;
return getChannel().getType() == ChannelType.PRIVATE_CHANNEL;
}

/**
* Checks if the message was sent in a {@link ChannelType#PRIVATE_CHANNEL private channel}.
*
* @return Whether or not the message was sent in a private channel.
*/
default boolean isPrivateMessage() {
return getChannel().getType() == ChannelType.PRIVATE_CHANNEL;
}

/**
* Checks if the message was sent in a {@link ChannelType#SERVER_TEXT_CHANNEL server channel}.
*
* @return Whether or not the message was sent in a server channel.
*/
default boolean isServerMessage() {
return getChannel().getType() == ChannelType.SERVER_TEXT_CHANNEL;
}

/**
* Checks if the message was sent in a {@link ChannelType#GROUP_CHANNEL group channel}.
*
* @return Whether or not the message was sent in a group channel.
*/
default boolean isGroupMessage() {
return getChannel().getType() == ChannelType.GROUP_CHANNEL;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.javacord.api.event.message;

import org.javacord.api.entity.channel.ChannelType;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageAttachment;
import org.javacord.api.entity.message.MessageAuthor;

import java.util.List;

/**
* A message event where the message is guaranteed to be in the cache.
Expand All @@ -14,4 +19,74 @@ public interface CertainMessageEvent extends MessageEvent {
*/
Message getMessage();

/**
* Checks if the event's message was sent in a {@link ChannelType#PRIVATE_CHANNEL private channel}.
*
* @return Whether or not the event's message was sent in a private channel.
* @see Message#isPrivateMessage()
*/
default boolean isPrivateMessage() {
return getMessage().isPrivateMessage();
}

/**
* Checks if the event's message was sent in a {@link ChannelType#SERVER_TEXT_CHANNEL server channel}.
*
* @return Whether or not the event's message was sent in a server channel.
* @see Message#isServerMessage()
*/
default boolean isServerMessage() {
return getMessage().isServerMessage();
}

/**
* Checks if the event's message was sent in a {@link ChannelType#GROUP_CHANNEL group channel}.
*
* @return Whether or not the event's message was sent in a group channel.
* @see Message#isPrivateMessage()
*/
default boolean isGroupMessage() {
return getMessage().isGroupMessage();
}

/**
* Gets the author of the event's message.
*
* @return The author of the event's message.
* @see Message#getAuthor()
*/
default MessageAuthor getMessageAuthor() {
return getMessage().getAuthor();
}

/**
* Gets a list with all attachments of the event's message.
*
* @return A list with all attachments of the event's message.
* @see Message#getAttachments()
*/
default List<MessageAttachment> getMessageAttachments() {
return getMessage().getAttachments();
}

/**
* Gets the content of the event's message.
*
* @return The content of the event's message.
* @see Message#getContent()
*/
default String getMessageContent() {
return getMessage().getContent();
}

/**
* Gets the readable content of the event's message.
*
* @return The readable content of the event's message.
* @see Message#getReadableContent()
*/
default String getReadableMessageContent() {
return getMessage().getReadableContent();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.javacord.api.event.message;

import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageAttachment;
import org.javacord.api.entity.message.MessageAuthor;

import java.util.List;
import java.util.Optional;

/**
Expand All @@ -16,4 +19,44 @@ public interface OptionalMessageEvent extends MessageEvent {
*/
Optional<Message> getMessage();

/**
* Gets the author of the event's message.
*
* @return The author of the event's message.
* @see Message#getAuthor()
*/
default Optional<MessageAuthor> getMessageAuthor() {
return getMessage().map(Message::getAuthor);
}

/**
* Gets a list with all attachments of the event's message.
*
* @return A list with all attachments of the event's message.
* @see Message#getAttachments()
*/
default Optional<List<MessageAttachment>> getMessageAttachments() {
return getMessage().map(Message::getAttachments);
}

/**
* Gets the content of the event's message.
*
* @return The content of the event's message.
* @see Message#getContent()
*/
default Optional<String> getMessageContent() {
return getMessage().map(Message::getContent);
}

/**
* Gets the readable content of the event's message.
*
* @return The readable content of the event's message.
* @see Message#getReadableContent()
*/
default Optional<String> getReadableMessageContent() {
return getMessage().map(Message::getReadableContent);
}

}

0 comments on commit 193a485

Please sign in to comment.