-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji-add.js
44 lines (39 loc) · 1.38 KB
/
emoji-add.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
const dotenv = require('dotenv');
const fs = require('fs');
const request = require('request-promise-native');
dotenv.config();
const dirName = process.env.DIRNAME;
const apiToken = process.env.UPLOAD_TOKEN;
(async () => {
if (apiToken.substr(0,5) !== 'xoxs-') {
throw Error('Use token starting with "xoxs-"');
}
const images = fs.readdirSync(dirName);
for (let imgFile of images) {
try {
const img = fs.createReadStream(`${dirName}/${imgFile}`);
await request.post({
url: 'https://api.slack.com/api/emoji.getInfo',
formData: {
token: apiToken,
name: imgFile.split('.')[0],
},
}).then(res => JSON.parse(res)).then(res => {
if (res.ok) throw `${imgFile.split('.')[0]} already exists`;
});
const res = await request.post({
url: 'https://api.slack.com/api/emoji.add',
formData: {
token: apiToken,
mode: 'data',
name: imgFile.split('.')[0],
image: img,
},
});
const json = JSON.parse(res);
if (!json.ok) throw `${imgFile.split('.')[0]}: ${json.error}`;
} catch (e) {
console.error(`Error: ${e}`);
}
}
})();