/
ReplyCallbackActionImpl.java
251 lines (221 loc) · 7.94 KB
/
ReplyCallbackActionImpl.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
/*
* 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.internal.requests.restaction.interactions;
import net.dv8tion.jda.api.entities.IMentionable;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import net.dv8tion.jda.api.utils.AttachmentOption;
import net.dv8tion.jda.api.utils.FileUpload;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.interactions.InteractionHookImpl;
import net.dv8tion.jda.internal.utils.AllowedMentionsImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
import java.util.stream.Stream;
public class ReplyCallbackActionImpl extends DeferrableCallbackActionImpl implements ReplyCallbackAction
{
private final List<MessageEmbed> embeds = new ArrayList<>();
private final AllowedMentionsImpl allowedMentions = new AllowedMentionsImpl();
private final List<ActionRow> components = new ArrayList<>();
private String content = "";
private int flags;
private boolean tts;
public ReplyCallbackActionImpl(InteractionHookImpl hook)
{
super(hook);
}
@Nonnull
@Override
public ReplyCallbackActionImpl closeResources()
{
return (ReplyCallbackActionImpl) super.closeResources();
}
public ReplyCallbackActionImpl applyMessage(Message message)
{
this.content = message.getContentRaw();
this.tts = message.isTTS();
this.embeds.addAll(message.getEmbeds());
this.components.addAll(message.getActionRows());
this.allowedMentions.applyMessage(message);
return this;
}
protected DataObject toData()
{
DataObject json = DataObject.empty();
if (isEmpty())
{
json.put("type", ResponseType.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE.getRaw());
if (flags != 0)
json.put("data", DataObject.empty().put("flags", flags));
}
else
{
DataObject payload = DataObject.empty();
payload.put("allowed_mentions", allowedMentions);
payload.put("content", content);
payload.put("tts", tts);
payload.put("flags", flags);
if (!embeds.isEmpty())
payload.put("embeds", DataArray.fromCollection(embeds));
if (!components.isEmpty())
payload.put("components", DataArray.fromCollection(components));
json.put("data", payload);
json.put("type", ResponseType.CHANNEL_MESSAGE_WITH_SOURCE.getRaw()); // This type seemingly makes no difference right now, idk why it exists
}
return json;
}
private boolean isEmpty()
{
//Intentionally does not check components.isEmpty() here
// You cannot send a message with only components at this time.
return Helpers.isEmpty(content) && embeds.isEmpty() && files.isEmpty();
}
@Nonnull
@Override
public ReplyCallbackActionImpl setEphemeral(boolean ephemeral)
{
if (ephemeral)
this.flags |= 64;
else
this.flags &= ~64;
return this;
}
@Nonnull
@Override
public ReplyCallbackAction addFile(@Nonnull InputStream data, @Nonnull String name, @Nonnull AttachmentOption... options)
{
Checks.notNull(data, "Data");
Checks.notEmpty(name, "Name");
Checks.noneNull(options, "Options");
if (options.length > 0)
name = "SPOILER_" + name;
files.add(FileUpload.fromData(data, name));
isFileUpdate = true;
return this;
}
@Nonnull
@Override
public ReplyCallbackAction addEmbeds(@Nonnull Collection<? extends MessageEmbed> embeds)
{
Checks.noneNull(embeds, "MessageEmbed");
for (MessageEmbed embed : embeds)
{
Checks.check(embed.isSendable(),
"Provided Message contains an empty embed or an embed with a length greater than %d characters, which is the max for bot accounts!",
MessageEmbed.EMBED_MAX_LENGTH_BOT);
}
if (embeds.size() + this.embeds.size() > Message.MAX_EMBED_COUNT)
throw new IllegalStateException(String.format("Cannot have more than %d embeds per message!", Message.MAX_EMBED_COUNT));
this.embeds.addAll(embeds);
return this;
}
@Nonnull
@Override
public ReplyCallbackAction addActionRows(@Nonnull ActionRow... rows)
{
Checks.noneNull(rows, "ActionRows");
Checks.checkComponents("Some components are incompatible with Messages",
rows,
component -> component.getType().isMessageCompatible());
Checks.check(components.size() + rows.length <= 5, "Can only have 5 action rows per message!");
Checks.checkDuplicateIds(Stream.concat(this.components.stream(), Arrays.stream(rows)));
Collections.addAll(components, rows);
return this;
}
@Nonnull
@Override
public ReplyCallbackAction setCheck(BooleanSupplier checks)
{
return (ReplyCallbackAction) super.setCheck(checks);
}
@Nonnull
@Override
public ReplyCallbackAction timeout(long timeout, @Nonnull TimeUnit unit)
{
return (ReplyCallbackAction) super.timeout(timeout, unit);
}
@Nonnull
@Override
public ReplyCallbackAction deadline(long timestamp)
{
return (ReplyCallbackAction) super.deadline(timestamp);
}
@Nonnull
@Override
public ReplyCallbackActionImpl setTTS(boolean isTTS)
{
this.tts = isTTS;
return this;
}
@Nonnull
@Override
public ReplyCallbackActionImpl setContent(String content)
{
if (content != null)
Checks.notLonger(content, Message.MAX_CONTENT_LENGTH, "Content");
this.content = content == null ? "" : content;
return this;
}
@Nonnull
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public ReplyCallbackAction mentionRepliedUser(boolean mention)
{
allowedMentions.mentionRepliedUser(mention);
return this;
}
@Nonnull
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public ReplyCallbackAction allowedMentions(@Nullable Collection<Message.MentionType> allowedMentions)
{
this.allowedMentions.allowedMentions(allowedMentions);
return this;
}
@Nonnull
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public ReplyCallbackAction mention(@Nonnull IMentionable... mentions)
{
allowedMentions.mention(mentions);
return this;
}
@Nonnull
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public ReplyCallbackAction mentionUsers(@Nonnull String... userIds)
{
allowedMentions.mentionUsers(userIds);
return this;
}
@Nonnull
@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public ReplyCallbackAction mentionRoles(@Nonnull String... roleIds)
{
allowedMentions.mentionRoles(roleIds);
return this;
}
}