java.lang.Appendable
public class MessageAction extends RestAction<Message> implements java.lang.Appendable
RestAction
that allows setting message information before sending!
This is available as return type of all sendMessage/sendFile methods in MessageChannel
or by using MessageBuilder.sendTo(MessageChannel)
When updating a Message, unset fields will be ignored by default. To override existing fields with no value (remove content)
you can use override(true)
. Setting this to true
will cause all fields to be considered
and will override the Message entirely causing unset values to be removed from that message.
This can be used to remove existing embeds from a message:
message.editMessage("This message had an embed").override(true).queue()
When this RestAction has been executed all provided files will be closed.
If you decide not to execute this action, you should call clearFiles()
to free resources.
Note that the garbage collector also frees opened file streams when it finalizes the stream object.
@Override
public void onMessageReceived(MessageReceivedEvent event)
{
MessageChannel channel = event.getChannel();
channel.sendMessage("This has an embed with an image!")
.addFile(new File("dog.png"))
.embed(new EmbedBuilder()
.setImage("attachment://dog.png")
.build())
.queue(); // this actually sends the information to discord
}
RestAction.EmptyRestAction<T>
DEFAULT_FAILURE, DEFAULT_SUCCESS, LOG
Constructor | Description |
---|---|
MessageAction(JDA api,
net.dv8tion.jda.core.requests.Route.CompiledRoute route,
MessageChannel channel) |
|
MessageAction(JDA api,
net.dv8tion.jda.core.requests.Route.CompiledRoute route,
MessageChannel channel,
java.lang.StringBuilder contentBuilder) |
Modifier and Type | Method | Description |
---|---|---|
MessageAction |
addFile(byte[] data,
java.lang.String name) |
Adds the provided byte[] as file data.
|
MessageAction |
addFile(java.io.File file) |
Adds the provided
File as file data. |
MessageAction |
addFile(java.io.File file,
java.lang.String name) |
Adds the provided
File as file data. |
MessageAction |
addFile(java.io.InputStream data,
java.lang.String name) |
Adds the provided
InputStream as file data. |
MessageAction |
append(char c) |
|
MessageAction |
append(java.lang.CharSequence csq) |
|
MessageAction |
append(java.lang.CharSequence csq,
int start,
int end) |
|
MessageAction |
appendFormat(java.lang.String format,
java.lang.Object... args) |
Applies the result of
String.format(String, Object...)
as content. |
MessageAction |
apply(Message message) |
Applies the sendable information of the provided
Message
to this MessageAction settings. |
MessageAction |
clearFiles() |
|
MessageAction |
clearFiles(java.util.function.BiConsumer<java.lang.String,java.io.InputStream> finalizer) |
Clears all previously added files
|
MessageAction |
clearFiles(java.util.function.Consumer<java.io.InputStream> finalizer) |
Clears all previously added files
The clearFiles(BiConsumer) version provides the resource name for more selective operations. |
MessageAction |
content(java.lang.String content) |
Overrides existing content with the provided input
The content of a Message may not exceed 2000! |
MessageAction |
embed(MessageEmbed embed) |
Sets the
MessageEmbed
that should be used for this Message. |
boolean |
isEdit() |
Whether this MessageAction will be used to update an existing message.
|
boolean |
isEmpty() |
Whether this MessageAction has no values set.
|
MessageAction |
nonce(java.lang.String nonce) |
Sets the validation nonce for the outgoing Message
|
MessageAction |
override(boolean bool) |
Whether all fields should be considered when editing a message
|
MessageAction |
reset() |
Resets this MessageAction to empty state
isEmpty() will result in true after this has been performed! |
MessageAction |
tts(boolean isTTS) |
Enable/Disable Text-To-Speech for the resulting message.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
complete, complete, completeAfter, getJDA, isPassContext, queue, queue, queue, queueAfter, queueAfter, queueAfter, queueAfter, queueAfter, queueAfter, setCheck, setPassContext, submit, submit, submitAfter, submitAfter
public MessageAction(JDA api, net.dv8tion.jda.core.requests.Route.CompiledRoute route, MessageChannel channel)
public MessageAction(JDA api, net.dv8tion.jda.core.requests.Route.CompiledRoute route, MessageChannel channel, java.lang.StringBuilder contentBuilder)
public boolean isEmpty()
isEmpty() == true
will result in an IllegalStateException
!
This does not check for files!
public boolean isEdit()
@CheckReturnValue public MessageAction apply(Message message)
Message
to this MessageAction settings.
This does not copy files!
message
- The nullable Message to apply settings fromjava.lang.IllegalArgumentException
- If the message contains an MessageEmbed
that exceeds the sendable character limit,
see MessageEmbed.isSendable(AccountType)
@CheckReturnValue public MessageAction tts(boolean isTTS)
isEdit() == true
!isTTS
- True, if this should cause a Text-To-Speech effect when sent to the channel@CheckReturnValue public MessageAction reset()
isEmpty()
will result in true
after this has been performed!
Convenience over using
content(null).nonce(null).embed(null).tts(false).override(false).clearFiles()
@CheckReturnValue public MessageAction nonce(java.lang.String nonce)
For more information see MessageBuilder.setNonce(String)
and Message.getNonce()
nonce
- The nonce that shall be usedMessage.getNonce()
,
MessageBuilder.setNonce(String)
,
Cryptographic Nonce - Wikipedia@CheckReturnValue public MessageAction content(java.lang.String content)
content
- Sets the specified content and overrides previous content
or null
to reset contentjava.lang.IllegalArgumentException
- If the provided content exceeds the 2000 character limit@CheckReturnValue public MessageAction embed(MessageEmbed embed)
MessageEmbed
that should be used for this Message.
Refer to EmbedBuilder
for more information.embed
- The MessageEmbed
that should
be attached to this message, null
to use no embed.java.lang.IllegalArgumentException
- If the provided MessageEmbed is not sendable according to
MessageEmbed.isSendable(AccountType)
!
If the provided MessageEmbed is an unknown implementation this operation will fail as we are unable to deserialize it.@CheckReturnValue public MessageAction append(java.lang.CharSequence csq)
append
in interface java.lang.Appendable
java.lang.IllegalArgumentException
- If the appended CharSequence is too big and will cause the content to
exceed the 2000 character limit@CheckReturnValue public MessageAction append(java.lang.CharSequence csq, int start, int end)
append
in interface java.lang.Appendable
java.lang.IllegalArgumentException
- If the appended CharSequence is too big and will cause the content to
exceed the 2000 character limit@CheckReturnValue public MessageAction append(char c)
append
in interface java.lang.Appendable
java.lang.IllegalArgumentException
- If the appended CharSequence is too big and will cause the content to
exceed the 2000 character limit@CheckReturnValue public MessageAction appendFormat(java.lang.String format, java.lang.Object... args)
String.format(String, Object...)
as content.
For more information of formatting review the Formatter
documentation!
format
- The format Stringargs
- The arguments that should be used for conversionjava.lang.IllegalArgumentException
- If the appended formatting is too big and will cause the content to
exceed the 2000 character limitjava.util.IllegalFormatException
- If a format string contains an illegal syntax,
a format specifier that is incompatible with the given arguments,
insufficient arguments given the format string, or other illegal conditions.
For specification of all possible formatting errors,
see the Details
section of the formatter class specification.@CheckReturnValue public MessageAction addFile(java.io.InputStream data, java.lang.String name)
InputStream
as file data.
To reset all files use clearFiles()
data
- The InputStream that will be interpreted as file dataname
- The file name that should be used to interpret the type of the given data
using the file-name extension. This name is similar to what will be visible
through Message.Attachment.getFileName()
java.lang.IllegalStateException
- If the file limit of 10 has been reached prior to calling this method,
or if this MessageAction will perform an edit operation on an existing Message (see isEdit()
)java.lang.IllegalArgumentException
- If the provided data is null
or the provided name is blank or null
InsufficientPermissionException
- If this is targeting a TextChannel and the currently logged in account does not have
Permission.MESSAGE_ATTACH_FILES
@CheckReturnValue public MessageAction addFile(byte[] data, java.lang.String name)
To reset all files use clearFiles()
data
- The byte[] that will be interpreted as file dataname
- The file name that should be used to interpret the type of the given data
using the file-name extension. This name is similar to what will be visible
through Message.Attachment.getFileName()
java.lang.IllegalStateException
- If the file limit of 10 has been reached prior to calling this method,
or if this MessageAction will perform an edit operation on an existing Message (see isEdit()
)java.lang.IllegalArgumentException
- If the provided data is null
or the provided name is blank or null
or if the provided data exceeds the maximum file size of the currently logged in accountInsufficientPermissionException
- If this is targeting a TextChannel and the currently logged in account does not have
Permission.MESSAGE_ATTACH_FILES
SelfUser.getAllowedFileSize()
@CheckReturnValue public MessageAction addFile(java.io.File file)
File
as file data.
addFile(file, file.getName())
with the same side-effects.file
- The File that will be interpreted as file datajava.lang.IllegalStateException
- If the file limit of 10 has been reached prior to calling this method,
or if this MessageAction will perform an edit operation on an existing Message (see isEdit()
)java.lang.IllegalArgumentException
- If the provided file is null
or if the provided File is bigger than the maximum file size of the currently logged in accountInsufficientPermissionException
- If this is targeting a TextChannel and the currently logged in account does not have
Permission.MESSAGE_ATTACH_FILES
SelfUser.getAllowedFileSize()
@CheckReturnValue public MessageAction addFile(java.io.File file, java.lang.String name)
File
as file data.
To reset all files use clearFiles()
This method opens a FileInputStream
which will be closed by executing this action or using clearFiles()
!
file
- The File that will be interpreted as file dataname
- The file name that should be used to interpret the type of the given data
using the file-name extension. This name is similar to what will be visible
through Message.Attachment.getFileName()
java.lang.IllegalStateException
- If the file limit of 10 has been reached prior to calling this method,
or if this MessageAction will perform an edit operation on an existing Message (see isEdit()
)java.lang.IllegalArgumentException
- If the provided file is null
or the provided name is blank or null
or if the provided file is bigger than the maximum file size of the currently logged in account,
or if the provided file does not exist/ is not readableInsufficientPermissionException
- If this is targeting a TextChannel and the currently logged in account does not have
Permission.MESSAGE_ATTACH_FILES
SelfUser.getAllowedFileSize()
@CheckReturnValue public MessageAction clearFiles()
FileInputStreams
generated by addFile(File, String)
.
addFile(InputStream, String)
) use clearFiles(Consumer)
.clearFiles(Consumer)
,
clearFiles(BiConsumer)
@CheckReturnValue public MessageAction clearFiles(java.util.function.BiConsumer<java.lang.String,java.io.InputStream> finalizer)
finalizer
- BiConsumer useful to close remaining resources,
the consumer will receive the name as a string parameter and the resource as InputStream
.Closeable
@CheckReturnValue public MessageAction clearFiles(java.util.function.Consumer<java.io.InputStream> finalizer)
clearFiles(BiConsumer)
version provides the resource name for more selective operations.finalizer
- Consumer useful to close remaining resources,
the consumer will receive only the resource in the form of an InputStream
Closeable
@CheckReturnValue public MessageAction override(boolean bool)
bool
- True, to override all fields even if they are not set