forked from Metalloriff/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactionImages.plugin.js
318 lines (261 loc) · 10.1 KB
/
ReactionImages.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
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
/**
* @name ReactionImages
* @invite yNqzuJa
* @authorLink https://github.com/Metalloriff
* @donate https://www.paypal.me/israelboone
* @website https://metalloriff.github.io/toms-discord-stuff/
* @source https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/ReactionImages.plugin.js
*/
module.exports = (() => {
const config = {
info: {
name: "ReactionImages",
authors: [{
name: "Metalloriff",
discord_id: "264163473179672576",
github_username: "metalloriff",
twitter_username: "Metalloriff"
}],
version: "2.0.1",
description: "Allows you to define custom reaction image folders and send images with 'foldername/imagesearch'. Example: 'reactions/goodmeme'",
github: "https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/ReactionImages.plugin.js",
github_raw: "https://raw.githubusercontent.com/Metalloriff/BetterDiscordPlugins/master/ReactionImages.plugin.js"
},
changelog: [{
type: "fixed",
title: "Shitcord patch",
items: [
"Shitcord broke, I fixed. Thanks Strencher for letting me know."
]
}],
defaultConfig: [{
type: "category",
id: "general",
name: "general settings",
collapsible: true,
shown: true,
settings: [{
type: "textbox",
id: "paths",
name: "Folder Sources",
note: "Note: Separate folder paths with commas.",
placeholder: "Example: C:/Folder1, C:/Folder2/Some Other Folder, C:/Folder3",
value: ""
}, {
type: "slider",
id: "acSize",
name: "Autocomplete Image Size",
value: 200, min: 50, max: 500
}, {
type: "slider",
id: "acCap",
name: "Autocomplete Result Cap",
note: "The maximum number of images that will be displayed at the same time. Reduce this if you experience freezes when searching.",
value: 20, min: 5, max: 100
}]
}]
};
return !global.ZeresPluginLibrary ? class {
constructor() { this._config = config; }
getName = () => config.info.name;
getAuthor = () => config.info.description;
getVersion = () => config.info.version;
load() {
BdApi.showConfirmationModal("Library Missing", `The library plugin needed for ${config.info.name} is missing. Please click Download Now to install it.`, {
confirmText: "Download Now",
cancelText: "Cancel",
onConfirm: () => {
require("request").get("https://rauenzi.github.io/BDPluginLibrary/release/0PluginLibrary.plugin.js", async (err, res, body) => {
if (err) return require("electron").shell.openExternal("https://betterdiscord.net/ghdl?url=https://raw.githubusercontent.com/rauenzi/BDPluginLibrary/master/release/0PluginLibrary.plugin.js");
await new Promise(r => require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0PluginLibrary.plugin.js"), body, r));
});
}
});
}
start() { }
stop() { }
} : (([Plugin, Api]) => {
const plugin = (Plugin, Api) => {
const { WebpackModules, DiscordModules, PluginUtilities, Toasts, Patcher, ReactTools } = Api;
const { UserStore, React } = DiscordModules;
const { getCurrentUser } = UserStore;
const Autocomplete = WebpackModules.getByDisplayName("Autocomplete");
const { AUTOCOMPLETE_OPTIONS } = WebpackModules.getByProps("AUTOCOMPLETE_OPTIONS");
const { upload } = WebpackModules.getByProps("cancel", "upload");
const fs = require("fs");
const path = require("path");
const classes = {
...WebpackModules.getByProps("horizontalAutocomplete"),
...WebpackModules.getByProps("horizontal", "directionRowReverse", "noWrap"),
...WebpackModules.getByProps("justifyStart", "alignStretch", "noWrap")
};
class AutocompleteItem extends React.Component {
render() {
const { fp, data, onClick, selected, index, height } = this.props;
return React.createElement(
"div",
{},
React.createElement(
"div",
{
style: {
color: "white",
textAlign: "center"
}
},
path.basename(fp).split(".")[0],
),
React.createElement(
Autocomplete.GIFIntegration,
{
className: classes.horizontalAutocomplete,
src: `data:image/${path.extname(fp)};base64, ${data}`,
height, onClick, selected, index
}
)
);
}
}
return class ReactionImages extends Plugin {
folders = {};
constructor() {
super();
}
getDataName = () => this.getName() + "." + getCurrentUser().id;
loadSettings = s => PluginUtilities.loadSettings(this.getDataName(), this.defaultSettings || s);
saveSettings = s => PluginUtilities.saveSettings(this.getDataName(), this.settings || s);
async showChangelog(footer) {
try { footer = (await WebpackModules.getByProps("getUser", "acceptAgreements").getUser("264163473179672576")).tag + " | https://discord.gg/yNqzuJa"; }
finally { super.showChangelog(footer); }
}
getSettingsPanel() {
const panel = this.buildSettingsPanel();
panel.addListener(() => {
this.term();
this.init();
});
return panel.getElement();
}
async init() {
this.fetchAllFolders();
}
async fetchAllFolders() {
Toasts.show("ReactionImages: Loading folder data...");
this.folders = {};
const folderPaths = this.settings.general.paths.split(",").map(p => p.trim());
for (let folder of folderPaths) {
const folderName = path.basename(folder).toLowerCase().replace(/ /g, "");
fs.readdir(folder, async (err, fileNames) => {
if (err) {
Toasts.show("ReactionImages: Failed to load folder named '" + folderName + "'!", { type: "error" });
return console.error(err);
}
this.folders[folderName] = [];
for (let fileName of fileNames) {
const ext = fileName.split(".")[fileName.split(".").length - 1];
if (!fileName.includes(".") || !["jpg", "jpeg", "png", "gif", "bmp"].includes(ext))
continue;
const fp = path.join(folder, fileName);
const data = await new Promise(r => fs.readFile(fp, "base64", (_, d) => r(d)));
this.folders[folderName].push({
data,
fp,
size: data.length
});
}
Toasts.show("ReactionImages: Successfully loaded folder named '" + folderName + "'.", { type: "success" });
});
}
}
getFolders() {
return this.settings.general.paths.split(",").map(fp => ([ fp.trim(), path.basename(fp.trim()) ]));
}
term() {
}
holdingShift = false;
updateHoldingShift = e => this.holdingShift = e.shiftKey;
async onStart() {
this.init();
document.addEventListener("keydown", this.updateHoldingShift);
document.addEventListener("keyup", this.updateHoldingShift);
AUTOCOMPLETE_OPTIONS.REACTION_IMAGES = {
autoSelect: true,
getPlainText: () => "",
getRawText: () => "",
getSentinel: () => "",
matches: (channel, {}, query, {}, config, queryRaw) => {
for (const [ fp, dirName ] of this.getFolders()) {
if (query.split("/")[0] == dirName.toLowerCase()) {
return true;
}
}
return false;
},
queryResults: (channel, query, config, queryRaw) => {
const [ folderName, reactionQuery ] = query.split("/");
const results = [];
const folder = this.folders[folderName];
if (folder && folder.length) {
for (const file of folder) {
const fn = path.basename(file.fp);
if (fn.toLowerCase().replace(/ /g, "").startsWith(reactionQuery)) {
results.push(file);
}
}
}
return { results };
},
renderResults: (channel, query, selectedIndex, select, choose, config, _, { results }) => {
const strong = c => React.createElement("strong", {}, c);
return [
React.createElement(
Autocomplete.Title,
{ title: [ "Searching ", strong(`"${query.split("/")[1]}"`), " in ", strong(query.split("/")[0]) ] }
),
React.createElement(
"div",
{ className: [ classes.flex, classes.horizontal, classes.justifyStart, classes.alignStretch, classes.noWrap, classes.horizontalAutocompletes ].join(" ") },
results.map(({ data, fp, size }, index) => React.createElement(
AutocompleteItem,
{
fp, data,
onClick: () => {
const { deserialize } = WebpackModules.getByProps("serialize", "deserialize");
const { channelTextArea } = WebpackModules.getByProps("channelTextArea", "channelTextAreaDisabled");
const [ cta ] = document.getElementsByClassName(channelTextArea);
const owner = ReactTools.getOwnerInstance(cta);
const { state: { textValue } } = owner;
upload(channel.id, new File([ fs.readFileSync(fp) ], path.basename(fp)), {
content: textValue.split(" ").splice(0, textValue.split(" ").length - 1).join(" ")
});
if (!this.holdingShift) {
owner.setState({ textValue: "", richValue: deserialize("") });
if (!this.settings.hasShownShiftNotice) {
Toasts.show("Note: You can hold shift to prevent the menu from closing when sending a reaction!", { type: "success" });
this.settings.hasShownShiftNotice = true;
this.saveSettings();
}
}
},
selected: selectedIndex == index,
height: this.settings.general.acSize,
index,
}
)).slice(0, this.settings.general.acCap)
)
];
}
};
}
onStop() {
this.term();
document.removeEventListener("keydown", this.updateHoldingShift);
document.removeEventListener("keyup", this.updateHoldingShift);
delete AUTOCOMPLETE_OPTIONS.REACTION_IMAGES;
Patcher.unpatchAll();
}
}
};
return plugin(Plugin, Api);
})(global.ZeresPluginLibrary.buildPlugin(config));
})();