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-Commands
orButtons
.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 theInteractionHook
will 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 ReplyAction
deferReply()
Acknowledge this interaction and defer the reply to a later time.default ReplyAction
deferReply(boolean ephemeral)
Acknowledge this interaction and defer the reply to a later time.AbstractChannel
getChannel()
The channel this interaction happened in.default ChannelType
getChannelType()
TheChannelType
for the channel this interaction came from.Guild
getGuild()
TheGuild
this interaction happened in.default GuildChannel
getGuildChannel()
TheGuildChannel
this interaction happened in.InteractionHook
getHook()
TheInteractionHook
which can be used to send deferred replies or followup messages.JDA
getJDA()
Returns theJDA
instance of this interactionMember
getMember()
TheMember
who caused this interaction.default MessageChannel
getMessageChannel()
TheMessageChannel
this interaction happened in.default PrivateChannel
getPrivateChannel()
ThePrivateChannel
this interaction happened in.default TextChannel
getTextChannel()
TheTextChannel
this interaction happened in.java.lang.String
getToken()
The interaction token used for responding to an interaction.default InteractionType
getType()
TheInteractionType
for this interaction.int
getTypeRaw()
The raw interaction type.User
getUser()
TheUser
who caused this interaction.default VoiceChannel
getVoiceChannel()
TheVoiceChannel
this interaction happened in.boolean
isAcknowledged()
Whether this interaction has already been acknowledged.default boolean
isFromGuild()
Whether this interaction came from aGuild
.default ReplyAction
reply(java.lang.String content)
Reply to this interaction and acknowledge it.default ReplyAction
reply(Message message)
Reply to this interaction and acknowledge it.default ReplyAction
replyEmbeds(java.util.Collection<? extends MessageEmbed> embeds)
Reply to this interaction and acknowledge it.default ReplyAction
replyEmbeds(MessageEmbed embed, MessageEmbed... embeds)
Reply to this interaction and acknowledge it.default ReplyAction
replyFormat(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()
TheInteractionType
for this interaction.- Returns:
- The
InteractionType
orInteractionType.UNKNOWN
-
getToken
@Nonnull java.lang.String getToken()
The interaction token used for responding to an interaction.- Returns:
- The interaction token
-
getGuild
@Nullable Guild getGuild()
TheGuild
this interaction happened in.
This is null in direct messages.- Returns:
- The
Guild
or null
-
isFromGuild
default boolean isFromGuild()
- Returns:
- True, if this interaction happened in a guild
-
getChannelType
@Nonnull default ChannelType getChannelType()
TheChannelType
for the channel this interaction came from.
IfgetChannel()
is null, this returnsChannelType.UNKNOWN
.- Returns:
- The
ChannelType
-
getMember
@Nullable Member getMember()
TheMember
who 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()
TheInteractionHook
which 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 theInteractionHook
will 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 theInteractionHook
will 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
- TheMessageEmbeds
to 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()
TheGuildChannel
this 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()
TheMessageChannel
this 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()
TheTextChannel
this 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()
TheVoiceChannel
this 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()
ThePrivateChannel
this interaction happened in.
IfgetChannelType()
is notChannelType.PRIVATE
, this throwsIllegalStateException
!- Returns:
- The
PrivateChannel
- Throws:
java.lang.IllegalStateException
- IfgetChannel()
is not a private channel
-
-