-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
MessagePoll.java
275 lines (248 loc) · 7.05 KB
/
MessagePoll.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* 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.entities.messages;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
import net.dv8tion.jda.api.utils.messages.MessagePollBuilder;
import org.jetbrains.annotations.Unmodifiable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.List;
/**
* Poll sent with messages.
*
* @see Message#getPoll()
* @see Message#endPoll()
*/
public interface MessagePoll
{
/** Maximum length of a {@link MessagePollBuilder#setTitle(String) poll question title} ({@value}) */
int MAX_QUESTION_TEXT_LENGTH = 300;
/** Maximum length of a {@link MessagePollBuilder#addAnswer(String)} poll answer title} ({@value}) */
int MAX_ANSWER_TEXT_LENGTH = 55;
/** Maximum amount of {@link MessagePollBuilder#addAnswer(String) poll answers} ({@value}) */
int MAX_ANSWERS = 10;
/** Maximum {@link MessagePollBuilder#setDuration(Duration) duration} of poll ({@value}) */
long MAX_DURATION_HOURS = 7 * 24;
/**
* The layout of the poll.
*
* @return The poll layout, or {@link LayoutType#UNKNOWN} if unknown
*/
@Nonnull
LayoutType getLayout();
/**
* The poll question, representing the title.
*
* @return {@link Question}
*/
@Nonnull
Question getQuestion();
/**
* The poll answers.
*
* <p>Each answer also has the current {@link Answer#getVotes() votes}.
* The votes might not be finalized and might be incorrect before the poll has expired,
* see {@link #isFinalizedVotes()}.
*
* @return Immutable {@link List} of {@link Answer}
*/
@Nonnull
@Unmodifiable
List<Answer> getAnswers();
/**
* The time when this poll will automatically expire.
*
* <p>The author of the poll can always expire the poll manually, using {@link Message#endPoll()}.
*
* @return {@link OffsetDateTime} representing the time when the poll expires automatically, or null if it never expires
*/
@Nullable
OffsetDateTime getTimeExpiresAt();
/**
* Whether this poll allows multiple answers to be selected.
*
* @return True, if this poll allows multi selection
*/
boolean isMultiAnswer();
/**
* Whether this poll is finalized and recounted.
*
* <p>The votes for answers might be inaccurate due to eventual consistency, until this is true.
* Finalization does not mean the votes cannot change anymore, use {@link #isExpired()} to check if a poll has ended.
*
* @return True, if the votes have been precisely counted
*/
boolean isFinalizedVotes();
/**
* Whether this poll has passed its {@link #getTimeExpiresAt() expiration time}.
*
* @return True, if this poll is expired.
*/
default boolean isExpired()
{
return getTimeExpiresAt().isBefore(OffsetDateTime.now());
}
/**
* The question for a poll.
*/
class Question
{
private final String text;
private final EmojiUnion emoji;
public Question(String text, Emoji emoji)
{
this.text = text;
this.emoji = (EmojiUnion) emoji;
}
/**
* The poll question title.
*
* <p>Shown above all answers.
*
* @return The question title
*/
@Nonnull
public String getText()
{
return text;
}
/**
* Possible emoji related to the poll question.
*
* @return Possibly-null emoji
*/
@Nullable
public EmojiUnion getEmoji()
{
return emoji;
}
}
/**
* One of the answers for a poll.
*
* <p>Provides the current {@link #getVotes()} and whether you have voted for it.
*/
class Answer
{
private final long id;
private final String text;
private final EmojiUnion emoji;
private final int votes;
private final boolean selfVoted;
public Answer(long id, String text, EmojiUnion emoji, int votes, boolean selfVoted)
{
this.id = id;
this.text = text;
this.emoji = emoji;
this.votes = votes;
this.selfVoted = selfVoted;
}
/**
* The id of this answer.
*
* @return The answer id.
*/
public long getId()
{
return id;
}
/**
* The text content of the answer.
*
* @return The answer label.
*/
@Nonnull
public String getText()
{
return text;
}
/**
* The emoji assigned to this answer.
*
* @return {@link EmojiUnion}
*/
@Nullable
public EmojiUnion getEmoji()
{
return emoji;
}
/**
* The number of votes this answer has received so far.
*
* <p>This might not be {@link #isFinalizedVotes() finalized}.
*
* @return The current number of votes
*/
public int getVotes()
{
return votes;
}
/**
* Whether the answer was voted for by the currently logged in account.
*
* @return True, if the bot has voted for this.
*/
public boolean isSelfVoted()
{
return selfVoted;
}
}
/**
* The poll layout.
*
* <p>Currently always {@link #DEFAULT}.
*/
enum LayoutType
{
DEFAULT(1),
UNKNOWN(-1);
private final int key;
LayoutType(int key)
{
this.key = key;
}
/**
* The raw API key used to identify this layout.
*
* @return The API key
*/
public int getKey()
{
return key;
}
/**
* Resolves the provided raw API key to the layout enum constant.
*
* @param key
* The API key
*
* @return The layout type or {@link #UNKNOWN}
*/
public static LayoutType fromKey(int key)
{
for (LayoutType type : values())
{
if (type.key == key)
return type;
}
return UNKNOWN;
}
}
}