/
MessageEditCallbackActionImpl.java
184 lines (164 loc) · 6.22 KB
/
MessageEditCallbackActionImpl.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
/*
* 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.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.requests.restaction.interactions.MessageEditCallbackAction;
import net.dv8tion.jda.api.utils.AttachedFile;
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.Checks;
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;
public class MessageEditCallbackActionImpl extends DeferrableCallbackActionImpl implements MessageEditCallbackAction
{
private List<MessageEmbed> embeds = null;
private List<ActionRow> components = null;
private String content = null;
public MessageEditCallbackActionImpl(InteractionHookImpl hook)
{
super(hook);
}
@Nonnull
@Override
public MessageEditCallbackActionImpl setCheck(BooleanSupplier checks)
{
return (MessageEditCallbackActionImpl) super.setCheck(checks);
}
@Nonnull
@Override
public MessageEditCallbackActionImpl timeout(long timeout, @Nonnull TimeUnit unit)
{
return (MessageEditCallbackActionImpl) super.timeout(timeout, unit);
}
@Nonnull
@Override
public MessageEditCallbackActionImpl deadline(long timestamp)
{
return (MessageEditCallbackActionImpl) super.deadline(timestamp);
}
@Nonnull
@Override
public MessageEditCallbackActionImpl closeResources()
{
return (MessageEditCallbackActionImpl) super.closeResources();
}
private boolean isEmpty()
{
return content == null && embeds == null && components == null && !isFileUpdate && files.isEmpty();
}
@Override
protected DataObject toData()
{
DataObject json = DataObject.empty();
if (isEmpty())
return json.put("type", ResponseType.DEFERRED_MESSAGE_UPDATE.getRaw());
json.put("type", ResponseType.MESSAGE_UPDATE.getRaw());
DataObject data = DataObject.empty();
if (content != null)
data.put("content", content);
if (embeds != null)
data.put("embeds", DataArray.fromCollection(embeds));
if (components != null)
data.put("components", DataArray.fromCollection(components));
json.put("data", data);
return json;
}
public MessageEditCallbackAction applyMessage(Message message)
{
this.content = message.getContentRaw();
this.embeds = new ArrayList<>(message.getEmbeds());
this.components = new ArrayList<>(message.getActionRows());
return this;
}
@Nonnull
@Override
public MessageEditCallbackAction setEmbeds(@Nonnull Collection<? extends MessageEmbed> embeds)
{
Checks.noneNull(embeds, "MessageEmbed");
Checks.check(embeds.size() <= Message.MAX_EMBED_COUNT, "Cannot have more than %d embeds per message!", Message.MAX_EMBED_COUNT);
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 (this.embeds == null)
this.embeds = new ArrayList<>();
this.embeds.clear();
this.embeds.addAll(embeds);
return this;
}
@Nonnull
@Override
public MessageEditCallbackAction setActionRows(@Nonnull ActionRow... rows)
{
Checks.noneNull(rows, "ActionRows");
Checks.checkComponents("Some components are incompatible with Messages",
rows,
component -> component.getType().isMessageCompatible());
Checks.check(rows.length <= 5, "Can only have 5 action rows per message!");
Checks.checkDuplicateIds(Arrays.stream(rows));
this.components = new ArrayList<>();
Collections.addAll(components, rows);
return this;
}
@Nonnull
@Override
public MessageEditCallbackAction 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 MessageEditCallbackAction retainFilesById(@Nonnull Collection<String> ids)
{
Checks.noneNull(ids, "IDs");
ids.forEach(Checks::isSnowflake);
// Keep uploads and remove all existing attachments
this.files.removeIf(file -> !(file instanceof FileUpload));
// Keep attachment for specified ids
ids.stream()
.map(AttachedFile::fromAttachment)
.forEach(this.files::add);
isFileUpdate = true;
return this;
}
@Nonnull
@Override
public MessageEditCallbackAction setContent(@Nullable String content)
{
if (content != null)
Checks.notLonger(content, Message.MAX_CONTENT_LENGTH, "Content");
this.content = content == null ? "" : content;
return this;
}
}