-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
MessageBuilder.java
388 lines (360 loc) · 13.3 KB
/
MessageBuilder.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* 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;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.entities.impl.MessageImpl;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MessageBuilder
{
public static final String USER_KEY = "%U%";
public static final String ROLE_KEY = "%R%";
public static final String TEXTCHANNEL_KEY = "%TC%";
public static final String EVERYONE_KEY = "%E%";
public static final String HERE_KEY = "%H%";
protected final StringBuilder builder = new StringBuilder();
protected final List<User> mentioned = new LinkedList<>();
protected final List<TextChannel> mentionedTextChannels = new LinkedList<>();
protected final List<Role> mentionedRoles = new LinkedList<>();
protected boolean mentionEveryone = false;
protected boolean isTTS = false;
protected Pattern formatPattern;
public MessageBuilder()
{
formatPattern = Pattern.compile(String.format("%s|%s|%s|%s|%s",
USER_KEY, ROLE_KEY, TEXTCHANNEL_KEY, EVERYONE_KEY, HERE_KEY));
}
/**
* Makes the created Message a TTS message
*
* @param tts whether the created Message should be a tts message
* @return this instance
*/
public MessageBuilder setTTS(boolean tts)
{
this.isTTS = tts;
return this;
}
/**
* Appends a string to the Message
*
* @param text the text to append
* @return this instance
*/
public MessageBuilder appendString(String text)
{
builder.append(text);
return this;
}
/**
* Appends a formatted string to the Message
*
* @param text the text to append
* @param format the format(s) to apply to the text
* @return this instance
*/
public MessageBuilder appendString(String text, Formatting... format)
{
boolean blockPresent = false;
for (Formatting formatting : format)
{
if (formatting == Formatting.BLOCK)
{
blockPresent = true;
continue;
}
builder.append(formatting.getTag());
}
if (blockPresent)
builder.append(Formatting.BLOCK.getTag());
builder.append(text);
if (blockPresent)
builder.append(Formatting.BLOCK.getTag());
for (int i = format.length - 1; i >= 0; i--)
{
if (format[i] == Formatting.BLOCK) continue;
builder.append(format[i].getTag());
}
return this;
}
/**
* This method is an extended form of {@link String#format(String, Object...)}. It allows for all of
* the token replacement functionality that String.format(String, Object...) supports, but it also supports
* specialized token replacement specific to JDA objects.
* <p>
* Current tokens:
* <ul>
* <li><b>%U%</b> - Used to mention a {@link net.dv8tion.jda.core.entities.User User}.
* Same as {@link #appendMention(net.dv8tion.jda.core.entities.User)}</li>
* <li><b>%R%</b> - Used to mention a {@link net.dv8tion.jda.core.entities.Role Role}.
* Same as {@link #appendMention(net.dv8tion.jda.core.entities.Role)}</li>
* <li><b>%TC%</b> - Used to mention a {@link net.dv8tion.jda.core.entities.TextChannel TextChannel}.
* Same as {@link #appendMention(net.dv8tion.jda.core.entities.TextChannel)}</li>
* <li><b>%E%</b> - Used to mention @everyone. Same as {@link #appendEveryoneMention()}</li>
* <li><b>%H%</b> - Used to mention @here. Same as {@link #appendHereMention()}</li>
* </ul>
* <p>
* Example: <br>
* If you placed the following code in an method handling a
* {@link net.dv8tion.jda.core.events.message.MessageReceivedEvent MessageReceivedEvent}<br>
*
* <pre>{@code
* User user = event.getAuthor();
* MessageBuilder builder = new MessageBuilder();
* builder.appendFormat("%U% is really cool!", user);
* builder.build();
* }</pre>
*
* It would build a message that mentions the author and says that he is really cool!. If the user's
* name was "Bob", it would say:<br>
* <pre> "Bob is really cool!"</pre>
*
* @param format
* A format string.
* @param args
* An array objects that will be used to replace the tokens.
* They must be provided in the order that the tokens appear in the provided format string.
* @return
* this instance of the MessageBuilder. Useful for chaining.
*/
public MessageBuilder appendFormat(String format, Object... args)
{
if (format == null || format.isEmpty())
return this;
int index = 0;
int stringIndex = 0;
StringBuilder sb = new StringBuilder();
Matcher m = formatPattern.matcher(format);
List<Class> classes = Arrays.asList(User.class, TextChannel.class, Role.class);
while (m.find() && stringIndex < format.length())
{
Class target = null;
boolean everyone = false;
switch (m.group())
{
case USER_KEY:
target = User.class;
break;
case TEXTCHANNEL_KEY:
target = TextChannel.class;
break;
case ROLE_KEY:
target = Role.class;
break;
case EVERYONE_KEY:
everyone = true;
break;
case HERE_KEY:
everyone = false;
break;
default:
throw new IllegalArgumentException("MessageBuilder's format regex triggered on an unknown key. How?!");
}
sb.append(format.substring(stringIndex, m.start())).append("%s");
stringIndex = m.end();
if (target != null)
{
boolean found = false;
for (int i = index; i < args.length; i++)
{
Object arg = args[i];
//This is a JDA object. If it isn't, skip it.
if (classes.stream().anyMatch(c -> c.isInstance(arg)))
{
//This isn't the object type we were expecting.
if (!target.isInstance(arg))
throw new IllegalArgumentException(String.format("Expected: %s at args index: %d but received: %s instead",
target.getSimpleName(), index, arg.getClass().getSimpleName()));
if (arg instanceof User)
{
User u = (User) arg;
args[i] = u.getAsMention();
mentioned.add(u);
}
else if (arg instanceof TextChannel)
{
TextChannel tc = (TextChannel) arg;
args[i] = tc.getAsMention();
mentionedTextChannels.add(tc);
}
else if (arg instanceof Role)
{
Role r = (Role) arg;
args[i] = r.getAsMention();
mentionedRoles.add(r);
}
else
throw new IllegalArgumentException("When checking instances of arguments, something failed. Contact dev.");
index++;
found = true;
break;
}
index++;
}
if (!found)
throw new MissingFormatArgumentException(m.group());
}
else
{
if (everyone)
{
sb.append("@everyone");
mentionEveryone = true;
}
else
{
sb.append("@here");
mentionEveryone = true;
}
}
}
if (stringIndex < format.length())
sb.append(format.substring(stringIndex, format.length()));
String finalFormat = String.format(sb.toString(), args);
builder.append(finalFormat);
return this;
}
/**
* Appends a code-block to the Message
*
* @param text the code to append
* @param language the language of the code. If unknown use an empty string
* @return this instance
*/
public MessageBuilder appendCodeBlock(String text, String language)
{
builder.append("```").append(language).append('\n').append(text).append("\n```");
return this;
}
/**
* Appends a mention to the Message
*
* @param user the user to mention
* @return this instance
*/
public MessageBuilder appendMention(User user)
{
builder.append("<@").append(user.getId()).append('>');
mentioned.add(user);
return this;
}
/**
* Appends a @everyone mention to the Message
*
* @return this instance
*/
public MessageBuilder appendEveryoneMention()
{
builder.append("@everyone");
mentionEveryone = true;
return this;
}
/**
* Appends a @here mention to the Message
*
* @return this instance
*/
public MessageBuilder appendHereMention()
{
builder.append("@here");
mentionEveryone = true;
return this;
}
/**
* Appends a channel mention to the Message.
* For this to work, the given TextChannel has to be from the Guild the mention is posted to.
*
* @param channel the TextChannel to mention
* @return this instance
*/
public MessageBuilder appendMention(TextChannel channel)
{
builder.append("<#").append(channel.getId()).append('>');
mentionedTextChannels.add(channel);
return this;
}
/**
* Appends a Role mention to the Message.
* For this to work, the given Role has to be from the Guild the mention is posted to.
*
* @param role the Role to mention
* @return this instance
*/
public MessageBuilder appendMention(Role role)
{
builder.append("<@&").append(role.getId()).append('>');
mentionedRoles.add(role);
return this;
}
/**
* Returns the current length of the content that will be built into a {@link net.dv8tion.jda.core.entities.Message Message}
* when {@link #build()} is called.<br>
* If this value is <code>0</code> or greater than <code>2000</code> when {@link #build()} is called, an exception
* will be raised.
*
* @return
* The currently length of the content that will be built into a Message.
*/
public int getLength()
{
return builder.length();
}
/**
* Creates a {@link net.dv8tion.jda.core.entities.Message Message} object from this Builder
*
* @return the created {@link net.dv8tion.jda.core.entities.Message Message}
*
* @throws UnsupportedOperationException
* <ul>
* <li>If you attempt to build() an empty Message (no content added to the Message)</li>
* <li>If you attempt to build() a Message with more than 2000 characters of content.</li>
* </ul>
*/
public Message build()
{
String message = builder.toString();
if (message.isEmpty())
throw new UnsupportedOperationException("Cannot build a Message with no content. (You never added any content to the message)");
if (message.length() > 2000)
throw new UnsupportedOperationException("Cannot build a Message with more than 2000 characters. Please limit your input.");
return new MessageImpl("", null, false).setContent(message).setTTS(isTTS).setMentionedUsers(mentioned)
.setMentionedChannels(mentionedTextChannels).setMentionedRoles(mentionedRoles).setMentionsEveryone(mentionEveryone);
}
/**
* Holds the Available formatting used in {@link #appendString(String, net.dv8tion.jda.core.MessageBuilder.Formatting...)}
*/
public enum Formatting
{
ITALICS("*"),
BOLD("**"),
STRIKETHROUGH("~~"),
UNDERLINE("__"),
BLOCK("`");
private final String tag;
Formatting(String tag)
{
this.tag = tag;
}
private String getTag()
{
return tag;
}
}
}