-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmessageBuilder.ts
86 lines (71 loc) · 1.89 KB
/
messageBuilder.ts
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
import { MessageCreateOptions, MessageMentionTypes } from 'discord.js';
/**
* Roughly based on Discord.js's EmbedBuilder, but doesn't build an embed
* so that bot messages for users with embeds turned off work nicely.
*
* By default, disables mentions.
*/
export class MessageBuilder {
private author?: string | null = null;
private title?: string | null = null;
private url?: string | null = null;
private description?: string | null = null;
private fields: { name: string; value: string }[] = [];
private footer?: string | null = null;
private allowMentions: MessageMentionTypes[] = [];
setAuthor(name: string | null | undefined): this {
this.author = name;
return this;
}
setTitle(title: string | null | undefined): this {
this.title = title;
return this;
}
setURL(url: string | null | undefined): this {
this.url = url;
return this;
}
setDescription(description: string | null | undefined): this {
this.description = description;
return this;
}
addFields(...fields: { name: string; value: string }[]): this {
this.fields.push(...fields);
return this;
}
setFooter(footer: string | null | undefined): this {
this.footer = footer;
return this;
}
setAllowMentions(...mentions: MessageMentionTypes[]): this {
this.allowMentions = mentions;
return this;
}
build(): MessageCreateOptions {
const message: string[] = [];
if (this.author) {
message.push(this.author);
}
if (this.title) {
if (this.url) {
message.push(`## [${this.title}](<${this.url}>)`);
} else {
message.push(`## ${this.title}`);
}
}
if (this.description) {
message.push(this.description);
}
for (const field of this.fields) {
message.push(`### ${field.name}`);
message.push(field.value);
}
if (this.footer) {
message.push('', this.footer);
}
return {
content: message.join('\n'),
allowedMentions: { parse: this.allowMentions },
};
}
}