-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
70 lines (61 loc) · 2.27 KB
/
Utils.js
File metadata and controls
70 lines (61 loc) · 2.27 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
const errorPhrases = require('../assets/errorPhrases.json');
const moment = require('moment');
const collection = require('lodash/collection');
logAndMsg = (channel, msg) => {
console.log(msg);
channel.send(msg);
};
getRandomIndex = (arr) => Math.floor(Math.random() * arr.length);
errAndMsg = (channel, err) => {
console.error(err);
channel.send(`${errorPhrases[getRandomIndex(errorPhrases)]} ${err}`);
};
getUnixTimestamp = () => {
return moment().valueOf();
};
getHumanReadableDateTime = (datetime) => {
return moment(datetime).format("dddd, MMMM Do YYYY, h:mm:ssA");
};
formatArrayAsList = arr => {
// https://stackoverflow.com/a/29234240/5750790
let outStr = "";
arr = arr.map(el => '`' + el + '`');
if (arr.length === 1) {
outStr = arr[0];
} else if (arr.length === 2) {
outStr = arr.join(' and ');
} else if (arr.length > 2) {
outStr = arr.slice(0, -1).join(', ') + ', and ' + arr.slice(-1);
}
return outStr;
};
updateReactionsList = (snapshot, channel) => {
const mediaData = snapshot.val();
const sortedMediaNames = collection.groupBy(Object.keys(mediaData).filter(key => key !== 'TEMPLATE'), name => name[0].toLowerCase());
const embeds = Object.keys(sortedMediaNames).sort().map(key => ({
title: key.match(/[a-z]/i) ? `:regional_indicator_${key}:` : key,
description: sortedMediaNames[key].reduce((acc, val) => acc + `, [${val}](${mediaData[val].media})`, '').substring(2),
}));
channel.fetchMessages().then(messages => {
const embedNotUpdated = embed => {
const msg = messages.find(msg => msg.embeds.length > 0 && msg.embeds[0].title === embed.title);
if (msg === null || msg.embeds[0].description !== embed.description) return true;
else return false;
};
if (embeds.some(embedNotUpdated)) {
Promise.all(messages.map(msg => msg.delete())).then(() => {
// send embeds in order
embeds.reduce((chain, embed) => chain.then(() => channel.send({ embed })), Promise.resolve());
});
}
});
};
module.exports = {
logAndMsg,
getRandomIndex,
errAndMsg,
getUnixTimestamp,
getHumanReadableDateTime,
formatArrayAsList,
updateReactionsList,
};