attachment storing

This commit is contained in:
Erik 2020-06-20 23:27:23 +03:00
parent f9f43b233f
commit 46b374f2bb

View File

@ -7,7 +7,18 @@ const CONSTANTS = {
YELLOW: 15120384, // message edit
LIGHT_BLUE: 11337726, // message pin
BLUE: 479397
}
},
IMAGES: {
PREMIUM_LIMIT: 2,
UPLOAD_LIMIT: {
'0': 8,
'1': 8,
'2': 50,
'3': 100
},
MB_DIVIDER: 1024*1024
},
WEEK: 7 * 24 * 60 * 60
};
class GuildLogger extends Observer {
@ -36,6 +47,85 @@ class GuildLogger extends Observer {
async storeAttachment(message) {
const { guild, member, author, channel } = message;
if(!guild || author.bot) return;
if(!message.attachments.size) return;
console.log('Store call')
const settings = await guild.settings(),
setting = settings.messageLog,
roles = member._roles,
{ ignoredRoles } = setting;
//if(guild._settings.premium < CONSTANTS.IMAGES.PREMIUM_LIMIT) return;
if(!setting.attachments || setting.ignoredChannels.includes(channel.id)) return;
if(setting.ignoredRoles.length && roles.length)
for(const role of ignoredRoles)
if(roles.includes(role)) return;
console.log('Can store')
const attachments = message.attachments.values();
for(const attachment of attachments) {
const TH = this.client.transactionHandler;
const { size, name, id } = attachment;
const fsize = size/CONSTANTS.IMAGES.MB_DIVIDER // File size in MB
if(fsize > CONSTANTS.IMAGES.UPLOAD_LIMIT[guild.premiumTier]) continue;
const buffer = await Util.downloadAsBuffer(attachment.url).catch(err => { this.client.logger.error(err); return null; });
if(!buffer) return;
const data = {
buffer,
id
};
try {
//TODO: test this
//const start = Date.now();
const result = await TH.send({
provider: 'mongodb',
request: {
type: 'insertOne',
collection: 'attachment_logs',
data
}
});
//console.log(`Took ${Date.now()-start}ms to insert image.`);
//console.log(result);
const metadata = {
database_ID: result.insertedId,
id,
guild: guild.id,
message: message.id,
author: author.id,
name,
size,
timestamp: Math.floor(Date.now()/1000),
removeAt: Math.floor(Date.now()/1000 + guild._settings.premium * CONSTANTS.WEEK * 0.25)
}
//console.log(metadata);
await TH.send({
provider: 'mongodb',
request: {
type: 'insertOne',
collection: 'attachment_logs_index',
data: metadata
}
});
//console.log(`Took ${Date.now()-start}ms to insert entire entry.`);
} catch (err) {
this.client.logger.error('Something went wrong with storing image to db:\n' + err.stack);
}
}
}
//TODO: Figure this thing out, this should be called from messageDelete and rawMessageDelete if any attachments are present
@ -66,7 +156,7 @@ class GuildLogger extends Observer {
const perms = logChannel.permissionsFor(guild.me);
if(!perms.has('SEND_MESSAGES') || !perms.has('VIEW_CHANNEL')) return;
if (ignoredRoles && member._roles.length)
if (ignoredRoles.length && member._roles.length)
for (const role of ignoredRoles)
if (member._roles.includes(role)) return;
@ -234,14 +324,15 @@ class GuildLogger extends Observer {
}
_replaceTags(text, member) {
const { user } = member;
const { user, guild } = member;
return text
.replace(/\{mention\}/g, `<@${member.id}>`)
.replace(/\{tag\}/g, `${Util.escapeMarkdown(user.tag)}`)
.replace(/\{user\}/g, `${user.username}`)
.replace(/\{guildsize\}/g, `${guild.memberCount}`)
.replace(/\{accage\}/g, `${this.client.resolver.timeAgo(Date.now()/1000 - user.createdTimestamp/1000)}`) //.replace(/a/, '1')
.replace(/\{id\}/g, `${user.id}`)
.replace(/\{tag\}/g, Util.escapeMarkdown(user.tag))
.replace(/\{user\}/g, Util.escapeMarkdown(user.username))
.replace(/\{guildsize\}/g, guild.memberCount)
.replace(/\{guildname\}/g, guild.name)
.replace(/\{accage\}/g, this.client.resolver.timeAgo(Date.now()/1000 - user.createdTimestamp/1000)) //.replace(/a/, '1')
.replace(/\{id\}/g, user.id)
.trim();
}