-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
MessageChannel.java
339 lines (317 loc) · 15.4 KB
/
MessageChannel.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright 2015-2016 Austin Keener & Michael Ritter
*
* 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.core.entities;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.MessageHistory;
import net.dv8tion.jda.core.entities.impl.MessageImpl;
import net.dv8tion.jda.core.requests.Request;
import net.dv8tion.jda.core.requests.Response;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.Route;
import org.apache.http.util.Args;
import org.json.JSONObject;
//import net.dv8tion.jda.core.exceptions.VerificationLevelException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
/**
* Represents a Discord channel that can have messages and files sent to it.
*/
public interface MessageChannel extends ISnowflake
{
/**
* This method is a shortcut method to return the following information in the following situation:
* If the MessageChannel is instance of..
* <ul>
* <li><b>TextChannel</b> - Returns {@link TextChannel#getName()}</li>
* <li><b>PrivateChannel</b> Returns {@link PrivateChannel#getUser()} {@link net.dv8tion.jda.core.entities.User#getName() .getName()}</li>
* <li><b>Group</b> - Returns {@link net.dv8tion.jda.client.entities.Group#getName()}</li>
* </ul>
*
* @return
* Possibly-null "name" of the MessageChannel. Different implementations determine what the name is.
*/
String getName();
//TODO: doc
ChannelType getType();
/**
* Returns the {@link net.dv8tion.jda.core.JDA JDA} instance of this MessageChannel
*
* @return
* the corresponding JDA instance
*/
JDA getJDA();
/**
* Sends a plain text {@link net.dv8tion.jda.core.entities.Message Message} to this channel.
* This will fail if the account of the api does not have the {@link net.dv8tion.jda.core.Permission#MESSAGE_WRITE Write-Permission}
* for this channel set
* After the Message has been sent, the created {@link net.dv8tion.jda.core.entities.Message Message} object is returned
* This Object will be null, if the sending failed.
* When the Rate-limit is reached (10 Messages in 10 secs), a {@link net.dv8tion.jda.core.exceptions.RateLimitedException RateLimitedException} is thrown
*
* @param text
* the text to send
* @return
* the Message created by this function
* @throws net.dv8tion.jda.core.exceptions.RateLimitedException
* when rate-imit is reached
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} and the logged in account does
* not have {@link net.dv8tion.jda.core.Permission#MESSAGE_WRITE Permission.MESSAGE_WRITE}.
* @throws net.dv8tion.jda.core.exceptions.BlockedException
* If this is a {@link net.dv8tion.jda.core.entities.PrivateChannel PrivateChannel} and PMs are blocked
* @throws VerificationLevelException
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
* and you do not meet the required verification-level of the guild.
*/
RestAction<Message> sendMessage(String text);
/**
* Sends a given {@link net.dv8tion.jda.core.entities.Message Message} to this Channel
* This method only extracts the mentions, text and tts status out of the given Message-Object
* Therefore this can also be used to resend already received Messages
* To allow above behaviour, this method returns a new {@link net.dv8tion.jda.core.entities.Message Message} instance. The passed one is not modified!
* If the sending of the Message failed (probably Permissions), this method returns null.
* When the Rate-limit is reached (10 Messages in 10 secs), a {@link net.dv8tion.jda.core.exceptions.RateLimitedException RateLimitedException} is thrown
*
* @param msg
* the {@link net.dv8tion.jda.core.entities.Message Message} to send
* @return
* The created {@link net.dv8tion.jda.core.entities.Message Message} object or null if it failed
* @throws net.dv8tion.jda.core.exceptions.RateLimitedException
* when rate-limit is reached
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} and the logged in account does
* not have {@link net.dv8tion.jda.core.Permission#MESSAGE_WRITE Permission.MESSAGE_WRITE}.
* @throws net.dv8tion.jda.core.exceptions.BlockedException
* If this is a {@link net.dv8tion.jda.core.entities.PrivateChannel PrivateChannel} and PMs are blocked
* @throws VerificationLevelException
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
* and you do not meet the required verification-level of the guild.
*/
RestAction<Message> sendMessage(Message msg);
/**
* Uploads a file to the Discord servers and sends it to this {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
* Sends the provided {@link net.dv8tion.jda.core.entities.Message Message} with the uploaded file.<br>
* If you do not wish to send a Message with the uploaded file, you can provide <code>null</code> for
* the <code>message</code> parameter.
*
* @param file
* The file to upload to the {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
* @param message
* The message to be sent along with the uploaded file. This value can be <code>null</code>.
* @return
* The {@link net.dv8tion.jda.core.entities.Message Message} created from this upload.
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* <ul>
* <li>
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} and the logged in account does
* not have {@link net.dv8tion.jda.core.Permission#MESSAGE_WRITE Permission.MESSAGE_WRITE}.
* </li>
* <li>
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel} and the logged in account does
* not have {@link net.dv8tion.jda.core.Permission#MESSAGE_ATTACH_FILES Permission.MESSAGE_ATTACH_FILES}.
* </li>
* </ul>
* @throws VerificationLevelException
* If this is a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}
* and you do not meet the required verification-level of the guild.
*/
RestAction<Message> sendFile(File file, Message message) throws IOException;
RestAction<Message> sendFile(File file, String fileName, Message message) throws IOException;
RestAction<Message> sendFile(InputStream data, String fileName, Message message);
RestAction<Message> sendFile(byte[] data, String fileName, Message message);
/**
* Attempts to get a {@link net.dv8tion.jda.core.entities.Message Message} from the Discord servers that has
* the same id as the id provided.<br>
*
* @param messageId
* The id of the sought after Message
* @return
* The Message defined by the provided id. `Null` if the message doesn't exist.
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* Thrown if:
* <ul>
* <li>Attempt to get a message from a channel which this account doesn't have access to.
* ({@link net.dv8tion.jda.core.Permission#MESSAGE_READ Permission.MESSAGE_READ})</li>
* <li>Attempt to get a message from a channel that this account cannot read the history of.
* ({@link net.dv8tion.jda.core.Permission#MESSAGE_HISTORY Permission.MESSAGE_HISTORY})</li>
* </ul>
*/
RestAction<Message> getMessageById(String messageId);
/**
* Attempts to delete a {@link net.dv8tion.jda.core.entities.Message Message} from the Discord servers
* that has the same id as the id provided.<br>
*
* @param messageId
* The id of the Message which should be deleted
* @return
* True if the message was successfully deleted. False if the message didn't exist.
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* Thrown if:
* <ul>
* <li>Attempt to get a message from a channel which this account doesn't have access to.
* ({@link net.dv8tion.jda.core.Permission#MESSAGE_READ Permission.MESSAGE_READ})</li>
* <li>Attempt to delete another user's message in a channel that this account doesn't have permission to manage.
* ({@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE})</li>
* <li>Attempt to delete another user's message in a PrivateChannel.</li>
* </ul>
*/
RestAction<Void> deleteMessageById(String messageId);
/**
* Creates a new {@link net.dv8tion.jda.core.MessageHistory MessageHistory} object for each call of this method.<br>
* This is <b>NOT</b> and internal message cache, but rather it queries the Discord servers for old messages.
*
* @return
* The MessageHistory for this channel.
*/
MessageHistory getHistory();
//TODO: doc
RestAction<MessageHistory> getHistoryAround(Message message, int limit);
RestAction<MessageHistory> getHistoryAround(String messageId, int limit);
/**
* Sends the typing status to discord. This is what is used to make the message "X is typing..." appear.<br>
* The typing status only lasts for 10 seconds or until a message is sent.<br>
* So if you wish to show continuous typing you will need to call this method once every 10 seconds.
* <p>
* The official discord client sends this every 5 seconds even though the typing status lasts 10.
*
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction}.
*/
RestAction<Void> sendTyping();
//TODO: doc
default RestAction<Void> addReactionById(String messageId, String unicode)
{
Args.notNull(messageId, "MessageId");
Args.containsNoBlanks(unicode, "Provided Unicode");
String encoded;
try
{
encoded = URLEncoder.encode(unicode, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException(e); //thanks JDK 1.4
}
Route.CompiledRoute route = Route.Messages.ADD_REACTION.compile(getId(), messageId, encoded);
return new RestAction<Void>(getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
//TODO: doc
default RestAction<Void> addReactionById(String messageId, Emote emote)
{
Args.notNull(messageId, "MessageId");
Args.notNull(emote, "Emote");
Route.CompiledRoute route = Route.Messages.ADD_REACTION.compile(getId(), messageId, String.format("%s:%s", emote.getName(), emote.getId()));
return new RestAction<Void>(getJDA(), route, null)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
/**
* Used to pin a message.<br>
* If the provided messageId is invalid or not in this channel, this does nothing.
*
* @param messageId
* The message to pin.
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction}<{@link Void}>
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If this is a TextChannel and this account does not have both
* {@link net.dv8tion.jda.core.Permission#MESSAGE_READ Permission.MESSAGE_READ} and
* {@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE}
*/
RestAction<Void> pinMessageById(String messageId);
/**
* Used to unpin a message.<br>
* If the provided messageId is invalid or not in this channel, this does nothing.
*
* @param messageId
* The message to pin.
* @return
* True - if the message was successfully unpinned. If false, the message id probably didn't exist or wasn't a message from this channel.
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If this is a TextChannel and this account does not have both
* {@link net.dv8tion.jda.core.Permission#MESSAGE_READ Permission.MESSAGE_READ} and
* {@link net.dv8tion.jda.core.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE}
*/
RestAction<Void> unpinMessageById(String messageId);
/**
* Gets a List of {@link net.dv8tion.jda.core.entities.Message Messages} that have been pinned in this channel.<br>
* If no messages have been pinned, this returns an empty List.
*
* @return
* {@link net.dv8tion.jda.core.requests.RestAction RestAction<List<Message>>}
* @throws net.dv8tion.jda.core.exceptions.PermissionException
* If this is a TextChannel and this account does not have
* {@link net.dv8tion.jda.core.Permission#MESSAGE_READ Permission.MESSAGE_READ}
*/
RestAction<List<Message>> getPinnedMessages();
default RestAction<Message> editMessageById(String id, String newContent)
{
return editMessageById(id, new MessageBuilder().appendString(newContent).build());
}
default RestAction<Message> editMessageById(String id, Message newContent)
{
Args.notNull(id, "id");
Args.notNull(newContent, "message");
JSONObject json = ((MessageImpl) newContent).toJSONObject();
Route.CompiledRoute route = Route.Messages.EDIT_MESSAGE.compile(getId(), id);
return new RestAction<Message>(getJDA(), route, json)
{
@Override
protected void handleResponse(Response response, Request request)
{
if (response.isOk())
{
try
{
Message m = EntityBuilder.get(api).createMessage(response.getObject());
request.onSuccess(m);
}
catch (IllegalArgumentException e)
{
request.onFailure(e);
}
}
else
{
request.onFailure(response);
}
}
};
}
}