Interface Interaction
-
- All Superinterfaces:
ISnowflake
- All Known Subinterfaces:
ButtonInteraction,CommandInteraction,ComponentInteraction,SelectionMenuInteraction
- All Known Implementing Classes:
ButtonClickEvent,GenericComponentInteractionCreateEvent,GenericInteractionCreateEvent,SelectionMenuEvent,SlashCommandEvent
public interface Interaction extends ISnowflake
Abstract representation for any kind of Discord interaction.
This includes things such asSlash-CommandsorButtons.To properly handle an interaction you must acknowledge it. This means you need to use either
reply(String),deferReply(), or another similar method for other interactions.Once the interaction is acknowledged, you can use
getHook()to send additional messages or update the original reply. When usingdeferReply()the first message sent to theInteractionHookwill be identical to usingInteractionHook.editOriginal(String). You must decide whether your reply will be ephemeral or not before callingdeferReply(). So design your code flow with that in mind!You can only acknowledge an interaction once! Any additional calls to reply/deferReply will result in exceptions. You can use
isAcknowledged()to check whether the interaction has been acknowledged already.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description ReplyActiondeferReply()Acknowledge this interaction and defer the reply to a later time.default ReplyActiondeferReply(boolean ephemeral)Acknowledge this interaction and defer the reply to a later time.AbstractChannelgetChannel()The channel this interaction happened in.default ChannelTypegetChannelType()TheChannelTypefor the channel this interaction came from.GuildgetGuild()TheGuildthis interaction happened in.default GuildChannelgetGuildChannel()TheGuildChannelthis interaction happened in.InteractionHookgetHook()TheInteractionHookwhich can be used to send deferred replies or followup messages.JDAgetJDA()Returns theJDAinstance of this interactionMembergetMember()TheMemberwho caused this interaction.default MessageChannelgetMessageChannel()TheMessageChannelthis interaction happened in.default PrivateChannelgetPrivateChannel()ThePrivateChannelthis interaction happened in.default TextChannelgetTextChannel()TheTextChannelthis interaction happened in.java.lang.StringgetToken()The interaction token used for responding to an interaction.default InteractionTypegetType()TheInteractionTypefor this interaction.intgetTypeRaw()The raw interaction type.UsergetUser()TheUserwho caused this interaction.default VoiceChannelgetVoiceChannel()TheVoiceChannelthis interaction happened in.booleanisAcknowledged()Whether this interaction has already been acknowledged.default booleanisFromGuild()Whether this interaction came from aGuild.default ReplyActionreply(java.lang.String content)Reply to this interaction and acknowledge it.default ReplyActionreply(Message message)Reply to this interaction and acknowledge it.default ReplyActionreplyEmbeds(java.util.Collection<? extends MessageEmbed> embeds)Reply to this interaction and acknowledge it.default ReplyActionreplyEmbeds(MessageEmbed embed, MessageEmbed... embeds)Reply to this interaction and acknowledge it.default ReplyActionreplyFormat(java.lang.String format, java.lang.Object... args)Reply to this interaction and acknowledge it.-
Methods inherited from interface net.dv8tion.jda.api.entities.ISnowflake
getId, getIdLong, getTimeCreated
-
-
-
-
Method Detail
-
getTypeRaw
int getTypeRaw()
The raw interaction type.
It is recommended to usegetType()instead.- Returns:
- The raw interaction type
-
getType
@Nonnull default InteractionType getType()
TheInteractionTypefor this interaction.- Returns:
- The
InteractionTypeorInteractionType.UNKNOWN
-
getToken
@Nonnull java.lang.String getToken()
The interaction token used for responding to an interaction.- Returns:
- The interaction token
-
getGuild
@Nullable Guild getGuild()
TheGuildthis interaction happened in.
This is null in direct messages.- Returns:
- The
Guildor null
-
isFromGuild
default boolean isFromGuild()
- Returns:
- True, if this interaction happened in a guild
-
getChannelType
@Nonnull default ChannelType getChannelType()
TheChannelTypefor the channel this interaction came from.
IfgetChannel()is null, this returnsChannelType.UNKNOWN.- Returns:
- The
ChannelType
-
getMember
@Nullable Member getMember()
TheMemberwho caused this interaction.
This is null if the interaction is not from a guild.- Returns:
- The
Member
-
getChannel
@Nullable AbstractChannel getChannel()
The channel this interaction happened in.
This is currently never null, but might be nullable in the future.- Returns:
- The channel or null if this interaction is not from a channel context
-
getHook
@Nonnull InteractionHook getHook()
TheInteractionHookwhich can be used to send deferred replies or followup messages.- Returns:
- The interaction hook
- Throws:
java.lang.UnsupportedOperationException- If this interaction does not support deferred replies and followup messages
-
isAcknowledged
boolean isAcknowledged()
Whether this interaction has already been acknowledged.
BothdeferReply()andreply(String)acknowledge an interaction. Each interaction can only be acknowledged once.- Returns:
- True, if this interaction has already been acknowledged
-
deferReply
@Nonnull @CheckReturnValue ReplyAction deferReply()
Acknowledge this interaction and defer the reply to a later time.
This will send a<Bot> is thinking...message in chat that will be updated later through eitherInteractionHook.editOriginal(String)orWebhookClient.sendMessage(String).You can use
deferReply(true)to send a deferred ephemeral reply. If your initial deferred message is not ephemeral it cannot be made ephemeral later. Your first message to theInteractionHookwill inherit whether the message is ephemeral or not from this deferred reply.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.Use
reply(String)to reply directly.- Returns:
ReplyAction
-
deferReply
@Nonnull @CheckReturnValue default ReplyAction deferReply(boolean ephemeral)
Acknowledge this interaction and defer the reply to a later time.
This will send a<Bot> is thinking...message in chat that will be updated later through eitherInteractionHook.editOriginal(String)orWebhookClient.sendMessage(String).You can use
deferReply()ordeferReply(false)to send a non-ephemeral deferred reply. If your initial deferred message is ephemeral it cannot be made non-ephemeral later. Your first message to theInteractionHookwill inherit whether the message is ephemeral or not from this deferred reply.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.Use
reply(String)to reply directly.Ephemeral messages have some limitations and will be removed once the user restarts their client.
When a message is ephemeral, it will only be visible to the user that used the interaction.
Limitations:- Cannot be deleted by the bot
- Cannot contain any files/attachments
- Cannot be reacted to
- Cannot be retrieved
- Parameters:
ephemeral- True, if this message should only be visible to the interaction user- Returns:
ReplyAction
-
reply
@Nonnull @CheckReturnValue default ReplyAction reply(@Nonnull Message message)
Reply to this interaction and acknowledge it.
This will send a reply message for this interaction. You can usesetEphemeral(true)to only let the target user see the message. Replies are non-ephemeral by default.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.If your handling can take longer than 3 seconds, due to various rate limits or other conditions, you should use
deferReply()instead.- Parameters:
message- The message to send- Returns:
ReplyAction- Throws:
java.lang.IllegalArgumentException- If null is provided
-
reply
@Nonnull @CheckReturnValue default ReplyAction reply(@Nonnull java.lang.String content)
Reply to this interaction and acknowledge it.
This will send a reply message for this interaction. You can usesetEphemeral(true)to only let the target user see the message. Replies are non-ephemeral by default.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.If your handling can take longer than 3 seconds, due to various rate limits or other conditions, you should use
deferReply()instead.- Parameters:
content- The message content to send- Returns:
ReplyAction- Throws:
java.lang.IllegalArgumentException- If null is provided or the content is empty or longer thanMessage.MAX_CONTENT_LENGTH
-
replyEmbeds
@Nonnull @CheckReturnValue default ReplyAction replyEmbeds(@Nonnull java.util.Collection<? extends MessageEmbed> embeds)
Reply to this interaction and acknowledge it.
This will send a reply message for this interaction. You can usesetEphemeral(true)to only let the target user see the message. Replies are non-ephemeral by default.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.If your handling can take longer than 3 seconds, due to various rate limits or other conditions, you should use
deferReply()instead.- Parameters:
embeds- TheMessageEmbedsto send- Returns:
ReplyAction- Throws:
java.lang.IllegalArgumentException- If null is provided
-
replyEmbeds
@Nonnull @CheckReturnValue default ReplyAction replyEmbeds(@Nonnull MessageEmbed embed, @Nonnull MessageEmbed... embeds)
Reply to this interaction and acknowledge it.
This will send a reply message for this interaction. You can usesetEphemeral(true)to only let the target user see the message. Replies are non-ephemeral by default.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.If your handling can take longer than 3 seconds, due to various rate limits or other conditions, you should use
deferReply()instead.- Parameters:
embed- The message embed to sendembeds- Any additional embeds to send- Returns:
ReplyAction- Throws:
java.lang.IllegalArgumentException- If null is provided
-
replyFormat
@Nonnull @CheckReturnValue default ReplyAction replyFormat(@Nonnull java.lang.String format, @Nonnull java.lang.Object... args)
Reply to this interaction and acknowledge it.
This will send a reply message for this interaction. You can usesetEphemeral(true)to only let the target user see the message. Replies are non-ephemeral by default.You only have 3 seconds to acknowledge an interaction!
When the acknowledgement is sent after the interaction expired, you will receiveErrorResponse.UNKNOWN_INTERACTION.If your handling can take longer than 3 seconds, due to various rate limits or other conditions, you should use
deferReply()instead.- Parameters:
format- Format string for the message contentargs- Format arguments for the content- Returns:
ReplyAction- Throws:
java.lang.IllegalArgumentException- If the format string is null or the resulting content is longer thanMessage.MAX_CONTENT_LENGTH
-
getGuildChannel
@Nonnull default GuildChannel getGuildChannel()
TheGuildChannelthis interaction happened in.
IfgetChannelType()is not a guild type, this throwsIllegalStateException!- Returns:
- The
GuildChannel - Throws:
java.lang.IllegalStateException- IfgetChannel()is not a guild channel
-
getMessageChannel
@Nonnull default MessageChannel getMessageChannel()
TheMessageChannelthis interaction happened in.
IfgetChannelType()is not a message channel type, this throwsIllegalStateException!- Returns:
- The
MessageChannel - Throws:
java.lang.IllegalStateException- IfgetChannel()is not a message channel
-
getTextChannel
@Nonnull default TextChannel getTextChannel()
TheTextChannelthis interaction happened in.
IfgetChannelType()is notChannelType.TEXT, this throwsIllegalStateException!- Returns:
- The
TextChannel - Throws:
java.lang.IllegalStateException- IfgetChannel()is not a text channel
-
getVoiceChannel
@Nonnull default VoiceChannel getVoiceChannel()
TheVoiceChannelthis interaction happened in.
IfgetChannelType()is notChannelType.VOICE, this throwsIllegalStateException!- Returns:
- The
VoiceChannel - Throws:
java.lang.IllegalStateException- IfgetChannel()is not a voice channel
-
getPrivateChannel
@Nonnull default PrivateChannel getPrivateChannel()
ThePrivateChannelthis interaction happened in.
IfgetChannelType()is notChannelType.PRIVATE, this throwsIllegalStateException!- Returns:
- The
PrivateChannel - Throws:
java.lang.IllegalStateException- IfgetChannel()is not a private channel
-
-