This repository was archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageBuilder.js
More file actions
69 lines (63 loc) · 1.96 KB
/
Copy pathMessageBuilder.js
File metadata and controls
69 lines (63 loc) · 1.96 KB
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
const BaseComponent = require('./BaseComponent');
const Markdown = require('./Markdown');
const Paragraph = require('./Paragraph');
const ParagraphComponent = require('./ParagraphComponent');
/**
*/
const MessageBuilder = class MessageBuilder {
/**
* Used to build messages.
* @param {...BaseComponent|ParagraphComponent|string} components - The components to initialize with.
*/
constructor(...components) {
/**
* The contents of the message.
* @type {Array.<(BaseComponent|ParagraphComponent)>}
*/
this.content = [];
this.add(...components);
}
/**
* Add a component to the message.
* @param {BaseComponent|ParagraphComponent|string} component - The component to add.
* @returns {this}
*/
add(...inputs) {
inputs.forEach(data => {
if (data instanceof BaseComponent) {
this.content.push(data);
} else if (data instanceof ParagraphComponent) {
let l = this.content[this.content.length - 1];
if (l instanceof Paragraph) l.add(data);
else this.content.push(new Paragraph(data));
} else if (typeof data === 'string')
this.content.push(new Markdown(data));
else throw new Error('Please provide a valid component.');
});
return this;
}
/**
* Convert to a JSON string like Guilded likes.
* @returns {object}
* @private
*/
toJSON() {
return {
object: 'value',
document: {
object: 'document',
data: {},
nodes: this.content.map(c => c.toJSON())
}
};
}
/**
* Converts the message into a string-like format.
* @returns {string}
* @private
*/
toString(options = {}) {
return this.content.map(c => c.toString(options)).filter(v => v).join('\n');
}
};
module.exports = MessageBuilder;