-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (103 loc) · 4.32 KB
/
Copy pathindex.js
File metadata and controls
126 lines (103 loc) · 4.32 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
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
const { Plugin } = require('powercord/entities');
const { getModule, getModuleByDisplayName, channels } = require('powercord/webpack');
const { upload } = getModule(["upload", "cancel"], false);
const { createBotMessage } = getModule([ 'createBotMessage' ], false);
const { receiveMessage } = getModule([ 'receiveMessage' ], false);
const Settings = require("./Settings");
const mime_types = require("./assets/mimetypes")
let default_location = require("path").join(require("os").homedir(), "Pictures").toString();
const fs = require('fs');
const path = require('path');
module.exports = class UploadCommands extends Plugin {
startPlugin() {
powercord.api.settings.registerSettings(this.entityID, {
category: this.entityID,
label: "Upload Commands",
render: Settings,
});
powercord.api.commands.registerCommand({
command: 'image',
usage: '{f} [file]',
description: 'Upload images or gifs.',
executor: this.image.bind(this),
autocomplete: this.image_autocomplete.bind(this),
});
powercord.api.commands.registerCommand({
command: 'upload',
usage: '{f} [file]',
description: 'Upload other file types.',
executor: this.other_files.bind(this),
autocomplete: this.upload_autocomplete.bind(this),
});
}
pluginWillUnload() {
powercord.api.commands.unregisterCommand('image');
powercord.api.commands.unregisterCommand('upload');
powercord.api.settings.unregisterSettings(this.entityID);
}
async image(name) {
name = name.join(" ");
let fileLocation = path.join(this.settings.get("filePath"), name);
let type = mime_types(name);
if (!type.includes("image")) {
return { result: "Not an Image" }
}
console.log(type);
const buf = fs.readFileSync(fileLocation);
const f = new File([buf], name, {type: type});
if (this.settings.get("sendFile")) {
upload(channels.getChannelId(), f);
} else {
const { pushFiles } = await getModule(["pushFiles"]);
const UploadModal = await getModuleByDisplayName("UploadModal");
pushFiles({ files: [f], channelId: channels.getChannelId() });
require("powercord/modal").open(UploadModal);
return {}
}
}
async other_files(name) {
name = name.join(" ");
let fileLocation = path.join(this.settings.get("filePath"), name);
let type = mime_types(name);
const buf = fs.readFileSync(fileLocation);
const f = new File([buf], name, {type: type});
if (this.settings.get("sendFile")) {
upload(channels.getChannelId(), f);
} else {
const { pushFiles } = await getModule(["pushFiles"]);
const UploadModal = await getModuleByDisplayName("UploadModal");
pushFiles({ files: [f], channelId: channels.getChannelId() });
require("powercord/modal").open(UploadModal);
return {}
}
}
image_autocomplete(args) {
args = args.join(" ");
let allFiles = fs.readdirSync(this.settings.get('filePath', default_location));
let files = [...allFiles]
.filter((name) => {name = name.toLowerCase();return name.startsWith(args) && this.get_mime(name).startsWith("image")})
.map((name) => ({ command: name }))
return {
commands: files,
header: 'images list',
};
}
upload_autocomplete(args) {
args = args.join(" ");
let allFiles = fs.readdirSync(this.settings.get('filePath', default_location));
let files = [...allFiles]
.filter((name) => {name = name.toLowerCase();return name.startsWith(args) && !this.get_mime(name).startsWith("image")})
.map((name) => ({ command: name }))
return {
commands: files,
header: 'files list',
};}
sendBotMessage(content) {
const received = createBotMessage(channels.getChannelId(), '');
received.embeds.push(content);
return receiveMessage(received.channel_id, received);
};
get_mime(name) { // For some reason, I had to do this... not sure why
return mime_types(name).toString();
}
}