forked from Fraserbc/BetterDiscord-Embeds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendEmbeds.plugin.js
244 lines (211 loc) · 5.77 KB
/
SendEmbeds.plugin.js
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
/**
* @name SendEmbeds
* @author Fraserbc
* @version 2.1.0
* @description Allows you to send embeds
* @website https://github.com/Fraserbc
* @source https://github.com/Fraserbc/BetterDiscord-Embeds/blob/master/SendEmbeds.plugin.js
*/
const config = {
info: {
name: 'SendEmbeds',
authors: [
{
name: 'Fraser Price',
github_username: 'Fraserbc'
}
],
version: '2.1.0',
description: 'Allows you to send embeds',
github: 'https://github.com/Fraserbc/BetterDiscord-Embeds',
github_raw: 'https://raw.githubusercontent.com/Fraserbc/BetterDiscord-Embeds/master/SendEmbeds.plugin.js'
}
};
module.exports = class {
constructor() {
this._config = config;
}
getName() {
return config.info.name;
}
getAuthor() {
return config.info.authors.map(author => author.name).join(', ');
}
getDescription() {
return config.info.description;
}
getVersion() {
return config.info.version;
}
load() {}
unload() {}
start() { this.attachHandler(); }
onSwitch() { this.attachHandler(); }
stop() {
let el = document.querySelectorAll('form[class^="form-');
if (el.length == 0) return;
// Remove handlers and injected script
el[0].removeEventListener('keydown', this.handler);
}
attachHandler() {
this.handler = this.handleKeypress.bind(this);
let el = document.querySelectorAll('form[class^="form-');
if (el.length == 0) return;
// Bind the handler
el[0].addEventListener('keydown', this.handler, false);
}
// Function that sends the embed
sendEmbed(embed) {
// Get the ID of the channel we want to send the embed
let channelID = BdApi.findModuleByProps('getLastSelectedChannelId').getChannelId();
// Create the message
let MessageQueue = BdApi.findModuleByProps('enqueue');
let MessageParser = BdApi.findModuleByProps('createBotMessage');
let msg = MessageParser.createBotMessage(channelID, '');
// Send the message
MessageQueue.enqueue({
type: 0,
message: {
channelId: channelID,
content: '',
tts: false,
nonce: msg.id,
embed: embed
}
}, r => {
return;
});
}
// Handling the user input
handleKeypress(e) {
var code = e.keyCode || e.which;
if (code !== 13) {
return;
}
if (e.shiftKey) {
return;
}
// Split a string on only the first delimeter
function splitSingle(str, delimeter) {
let part1 = str.substr(0, str.indexOf(delimeter));
let part2 = str.substr(str.indexOf(delimeter) + 1);
return [part1, part2]
};
// Get the deepest child of a parent
function getDeepest(elem) {
if(elem.firstChild == null) {
return elem;
} else {
return getDeepest(elem.firstChild);
}
};
// Parse the text
let elements = Array.from(document.querySelectorAll('div[class^="textArea-')[0].children[0].children);
let text = '';
elements.forEach(function(l0) {
Array.from(l0.children).forEach(function(l1) {
Array.from(l1.children).forEach(function(elem) {
elem = getDeepest(elem);
if(elem.alt) {
text += elem.alt;
} else {
text += elem.textContent;
}
});
});
text += '\n';
});
if (!text.startsWith('/e')) {
return;
};
// Cancel the event so we can handle it ourselves
e.preventDefault();
e.stopPropagation();
// Strip and split the text
text = text.replace('/e ', '');
text = text.replace('\uFEFF', '');
text = text.replace(/\n\n/g, '\n');
text = text.split('\n');
// Create the embed
let fields = ['title', 'description', 'url', 'color', 'timestamp', 'footer_image', 'footer', 'thumbnail', 'image', 'author', 'author_url', 'author_icon'];
let embed = {};
let last_attrb = ''
for (var x = 0; x < text.length; x++) {
let line = text[x]
let split = splitSingle(line, ':');
// Check if it is an attribute or continuation of previous
if(fields.includes(split[0])) {
// Check if there is a leading ' '
if(split[1].startsWith(' ')) {
embed[split[0]] = split[1].slice(1);
} else {
embed[split[0]] = split[1];
}
// Store the last attribute to be set so we can have multi-line
last_attrb = split[0];
} else {
embed[last_attrb] += '\n' + line;
}
}
// Find the unused fields
let unused = [];
let keys = Object.keys(embed);
for (var x = 0; x < keys.length; x++) {
if (embed[keys[x]] == '') {
unused.push(keys[x]);
}
}
// Remove the unused fields
for (var x = 0; x < unused.length; x++) {
delete embed[unused[x]];
}
// Proccess color
embed.color = embed.color ? parseInt(embed.color.replace('#', ''), 16) : 0;
// Convert the embed to Discord's format
let discordEmbed = {
type: 'rich',
footer: { text: '' },
author: { name: '' }
}
keys = Object.keys(embed);
for (var x = 0; x < keys.length; x++) {
switch(keys[x]) {
case 'timestamp':
if (embed.timestamp.toLowerCase() == 'true') {
let timestamp = (new Date).toISOString();
discordEmbed.timestamp = timestamp;
}
break;
case 'footer_image':
discordEmbed.footer.icon_url = embed.footer_image;
break;
case 'footer':
discordEmbed.footer.text = embed.footer;
break;
case 'thumbnail':
discordEmbed.thumbnail = {};
discordEmbed.thumbnail.url = embed.thumbnail;
break;
case 'image':
discordEmbed.image = {};
discordEmbed.image.url = embed.image;
break;
case 'author':
discordEmbed.author.name = embed.author;
break;
case 'author_url':
discordEmbed.author.url = embed.author_url;
break;
case 'author_icon':
discordEmbed.author.icon_url = embed.author_icon;
break;
default:
discordEmbed[keys[x]] = embed[keys[x]];
break;
}
}
// Send the embed
this.sendEmbed(discordEmbed);
this.lastKey = 0;
}
};