forked from discordboats-club/website-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.js
165 lines (153 loc) · 5.37 KB
/
Util.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
const marked = require('marked');
const Joi = require('joi');
const moment = require('moment');
const chunk = require('chunk');
const fetch = require('node-fetch');
const sanitizeHtml = require('sanitize-html');
const htmlOptions = {
allowedTags: ['style', 'h1', 'h2', ...sanitizeHtml.defaults.allowedTags]
};
module.exports = class Utils {
/**
* @param {Object} bot
* @param user
* @returns {Object}
*/
static async attachPropBot(bot, user = {}) {
const client = require('./ConstantStore').bot;
const { r } = require('./ConstantStore');
const botUser = client.users.get(bot.id) || (await client.users.fetch(bot.id));
bot.online = botUser.presence.status !== 'offline';
bot.name = botUser.username;
bot._discordAvatarURL = botUser.displayAvatarURL({ format: "png", size: 512 });
const description = bot.certified ? sanitizeHtml(bot.longDescription, htmlOptions) : bot.longDescription;
bot._markedDescription = marked(description, { sanitize: bot.certified ? false : true });
bot._ownerViewing = user.id === bot.ownerID;
bot._comments = await r
.table('comments')
.filter({ botID: bot.id })
.run();
try {
bot._ownerTag = client.users.get(bot.ownerID).tag;
} catch (e) {
bot._ownerTag = 'Unknown#0000';
}
if (user.id) {
const like = (await r
.table('likes')
.filter({ userID: user.id, botID: bot.id })
.run())[0];
bot._userLikes = !!like;
}
bot.likeCount = await r
.table('likes')
.filter({ botID: bot.id })
.count()
.run();
return bot;
}
/**
* Hides sensetive and internal data from bots.
* @param {Object} bot
*/
static hidePropsBot(bot) {
delete bot._discordAvatarURL;
delete bot._markedDescription;
delete bot._ownerViewing;
delete bot.apiToken;
return bot;
}
/**
* Hides sensetive and internal data from bots.
* @param user
* @param hideBots
*/
static hidePropsUser(user, hideBots = true) {
delete user.discordAT;
delete user.discordRT;
delete user._fmtCreatedAt;
if (hideBots) user._bots = user._bots.map(Utils.hidePropsBot);
delete user._verifiedBots;
return user;
}
/**
* @param {Object} user
* @returns {Object}
*/
static async attachPropUser(user) {
const client = require('./ConstantStore').bot;
const { r } = require('./ConstantStore');
const discordUser = client.users.get(user.id) || (await client.users.fetch(user.id));
user.online = discordUser.presence.status !== 'offline';
user.username = discordUser.username;
user.discriminator = discordUser.discriminator;
user._discordAvatarURL = discordUser.displayAvatarURL({ format: 'png', size: 512 });
user._bots = await Promise.all((await r.table('bots').filter({ ownerID: user.id })).map(b => Utils.attachPropBot(b)));
user._verifiedBots = user._bots.filter(bot => bot.verified);
user._chunked = chunk(user._verifiedBots, 4);
if (user.mod) user.badges.push('Moderator');
if (user.admin) user.badges.push('Administrator');
if (user._bots.find(b => b.certified)) user.badges.push('Certified Developer');
return user;
}
/**
* method for api endpoints
*/
static filterUnexpectedData(orig, startingData, schema) {
const data = Object.assign({}, startingData);
Object.keys(schema.describe().children).forEach(key => {
data[key] = orig[key];
});
return data;
}
/**
* method for api endpoints
*/
static handleJoi(schema, req, res) {
const wdjt = Joi.validate(req.body, schema); // What Does Joi Think (wdjt)
if (wdjt.error) {
if (!wdjt.error.isJoi) {
console.error('Error while running Joi.', wdjt.error);
res.status(500).json({ error: 'Internal Server Error' });
return true;
}
res.status(400).json({ error: wdjt.error.name, details: wdjt.error.details.map(item => item.message) });
return true;
}
return false;
}
static async resolveUser(msg, args, client) {
try {
const mention = msg.mentions.users.first();
if (mention) return mention;
if (!args[0]) return false;
const id = /^(?:<@!?)?(\d{17,19})>?$/.exec(args[0]);
if (id) {
const user = await client.users.fetch(id[1]);
if (user) return user;
}
const userTag = client.users.find(u => u.tag === args[0]);
if (userTag) return userTag;
return false;
} catch (e) {
return false;
}
}
static async likeWebhook(url, auth, event, botId, userId) {
const body = JSON.stringify({
event,
botId,
userId
});
try {
await fetch(url, {
method: 'POST',
headers: {
Authorization: auth,
'Content-Type': 'application/json'
},
body
});
} catch (e) {}
}
};