detritus-client
Version:
A Typescript NodeJS library to interact with Discord's API, both Rest and Gateway.
161 lines (160 loc) • 4.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Attachment = exports.MimeClassTypes = exports.EmbeddableRegexes = void 0;
const baseset_1 = require("../collections/baseset");
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const basestructure_1 = require("./basestructure");
exports.EmbeddableRegexes = Object.freeze({
audio: /mp3|ogg|wav|flac/i,
image: /png|jpe?g|webp|gif/i,
video: /mp4|webm|mov/i,
});
exports.MimeClassTypes = [
{
type: 'name',
classType: 'acrobat',
regex: /\.pdf$/i,
},
{
type: 'name',
classType: 'ae',
regex: /\.ae/i,
},
{
type: 'name',
classType: 'ai',
regex: /\.ai$/i,
},
{
type: 'name',
classType: 'archive',
regex: /\.(?:rar|zip|7z|tar|tar\.gz)$/i,
},
{
type: 'name',
classType: 'audio',
regex: /\.(?:mp3|ogg|wav|flac)$/i,
},
{
type: 'name',
classType: 'code',
regex: /\.(?:c\+\+|cpp|cc|c|h|hpp|mm|m|json|js|rb|rake|py|asm|fs|pyc|dtd|cgi|bat|rss|java|graphml|idb|lua|o|gml|prl|sls|conf|cmake|make|sln|vbe|cxx|wbf|vbs|r|wml|php|bash|applescript|fcgi|yaml|ex|exs|sh|ml|actionscript)$/i,
},
{
type: 'name',
classType: 'document',
regex: /\.(?:txt|rtf|doc|docx|md|pages|ppt|pptx|pptm|key|log)$/i,
},
{
type: 'mime',
classType: 'image',
regex: /^image\//,
},
{
type: 'mime',
classType: 'photoshop',
regex: /^image\/vnd.adobe.photoshop/,
},
{
type: 'name',
classType: 'sketch',
regex: /\.sketch$/i,
},
{
type: 'name',
classType: 'spreadsheet',
regex: /\.(?:xls|xlsx|numbers|csv)$/i,
},
{
type: 'mime',
classType: 'video',
regex: /^video\//,
},
{
type: 'name',
classType: 'webcode',
regex: /\.(?:html|xhtml|htm|js|xml|xls|xsd|css|styl)$/i,
},
];
const keysAttachment = new baseset_1.BaseSet([
constants_1.DiscordKeys.FILENAME,
constants_1.DiscordKeys.HEIGHT,
constants_1.DiscordKeys.ID,
constants_1.DiscordKeys.PROXY_URL,
constants_1.DiscordKeys.SIZE,
constants_1.DiscordKeys.URL,
constants_1.DiscordKeys.WIDTH,
]);
/**
* Attachment Structure, used for [Message] objects
* @category Structure
*/
class Attachment extends basestructure_1.BaseStructure {
constructor(message, data) {
super(message.client, undefined, message._clone);
this._uncloneable = true;
this._keys = keysAttachment;
this.filename = '';
this.height = 0;
this.id = '';
this.size = 0;
this.width = 0;
this.message = message;
this.merge(data);
Object.defineProperty(this, 'message', { enumerable: false, writable: false });
}
get classType() {
const mimetype = this.mimetype;
const found = exports.MimeClassTypes.find((search) => {
switch (search.type) {
case 'mime':
{
return search.regex.exec(mimetype);
}
;
case 'name':
{
return search.regex.exec(this.filename);
}
;
}
});
return (found) ? found.classType : 'unknown';
}
get createdAt() {
return new Date(this.createdAtUnix);
}
get createdAtUnix() {
return utils_1.Snowflake.timestamp(this.id);
}
get extension() {
const filename = (this.filename).split('.');
if (filename.length) {
return filename.pop();
}
return '';
}
get hasSpoiler() {
return this.filename.startsWith(constants_1.SPOILER_ATTACHMENT_PREFIX);
}
get isAudio() {
return !!exports.EmbeddableRegexes.audio.exec(this.extension);
}
get isImage() {
return !!exports.EmbeddableRegexes.image.exec(this.extension);
}
get isVideo() {
return !!exports.EmbeddableRegexes.video.exec(this.extension);
}
get isEmbeddable() {
return this.isAudio || this.isImage || this.isVideo;
}
get mimetype() {
return '';
}
toString() {
return this.filename;
}
}
exports.Attachment = Attachment;