/
Component.java
166 lines (152 loc) · 5.1 KB
/
Component.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.interactions.components;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.interactions.components.selections.SelectMenu;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.modals.Modal;
import net.dv8tion.jda.api.utils.data.SerializableData;
import javax.annotation.Nonnull;
/**
* Component of a Message or Modal.
* <br>These are used to extend messages with interactive elements such as buttons or select menus.
* Components are also the primary building blocks for {@link Modal Modals}.
*
* <br><p>Not every component can be used in {@link net.dv8tion.jda.api.entities.Message Messages} or {@link Modal Modals}.
* Use {@link Type#isMessageCompatible()} and {@link Type#isModalCompatible()} to check whether a component can be used.
*
*
* @see ActionRow
*
* @see Button
* @see SelectMenu
* @see TextInput
*/
public interface Component extends SerializableData
{
/**
* The type of component.
*
* @return {@link Type}
*/
@Nonnull
Type getType();
/**
* Whether this Component is compatible with {@link net.dv8tion.jda.api.entities.Message Messages}.
* <br>If the component in question is a {@link LayoutComponent}, this also checks every component inside it.
*
* @return True, if this Component is compatible with messages.
*/
boolean isMessageCompatible();
/**
* Whether this Component is compatible with {@link Modal Modals}.
* <br>If the component in question is a {@link LayoutComponent}, this also checks every component inside it.
*
* @return True, if this Component is compatible with modals.
*/
boolean isModalCompatible();
/**
* The component types
*/
enum Type
{
UNKNOWN(-1, 0, false, false),
/** A row of components */
ACTION_ROW(1, 0, true, true),
/** A button */
BUTTON(2, 5, true, false),
/** A select menu of strings */
STRING_SELECT(3, 1, true, false),
/** A text input field */
TEXT_INPUT(4, 1, false, true),
/** A select menu of users */
USER_SELECT(5, 1, true, false),
/** A select menu of roles */
ROLE_SELECT(6, 1, true, false),
/** A select menu of users and roles */
MENTIONABLE_SELECT(7, 1, true, false),
/** A select menu of channels */
CHANNEL_SELECT(8, 1, true, false),
;
private final int key;
private final int maxPerRow;
private final boolean messageCompatible;
private final boolean modalCompatible;
Type(int key, int maxPerRow, boolean messageCompatible, boolean modalCompatible)
{
this.key = key;
this.maxPerRow = maxPerRow;
this.messageCompatible = messageCompatible;
this.modalCompatible = modalCompatible;
}
/**
* How many of these components can be added to one {@link ActionRow}.
*
* @return The maximum amount an action row can contain
*/
public int getMaxPerRow()
{
return maxPerRow;
}
/**
* Raw int representing this ComponentType
*
* <p>This returns -1 if it's of type {@link #UNKNOWN}.
*
* @return Raw int representing this ComponentType
*/
public int getKey()
{
return key;
}
/**
* Whether this component can be used in {@link net.dv8tion.jda.api.entities.Message Messages}.
*
* @return Whether this component can be used in Messages.
*/
public boolean isMessageCompatible()
{
return messageCompatible;
}
/**
* Whether this component can be used in {@link Modal Modals}.
*
* @return Whether this component can be used in Modals.
*/
public boolean isModalCompatible()
{
return modalCompatible;
}
/**
* Maps the provided type id to the respective enum instance.
*
* @param type
* The raw type id
*
* @return The Type or {@link #UNKNOWN}
*/
@Nonnull
public static Type fromKey(int type)
{
for (Type t : values())
{
if (t.key == type)
return t;
}
return UNKNOWN;
}
}
}