UNPKG

discord-vatron

Version:

Módulo para facilitar la interacción con la API de Discord

93 lines (80 loc) 3.56 kB
const request = require('request-promise-native'); const { banderas, tamanosImagen, errores } = require('../../props.js'); const CanalMD = require('./CanalMD.js'); module.exports = class Usuario { constructor(datos, bot) { this._datos = datos; this._bot = bot; this.nombre = datos.username; this.id = datos.id; this.tag = datos.discriminator; this.bot = datos.bot || false; this.sistema = datos.system || false; this.avatar = datos.avatar || ''; } get nombreCompleto() { return `${this.nombre}#${this.tag}`; } enviar(opciones) { const promesa = new Promise(async(resolve, reject) => { if(!this._canal) await this.crearMD(); let comp; if(opciones.botones && Array.isArray(opciones.botones)) { comp = [{ type: 1, components: opciones.botones }]; } else if(opciones.filas && Array.isArray(opciones.filas)) { comp = opciones.filas; } request.post(({ url: `https://discord.com/api/v9/channels/${this._canal.id}/messages`, headers: { Authorization: 'Bot ' + this._bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify({ content: opciones.contenido ? opciones.contenido.toString() : '', embeds: opciones.embeds && Array.isArray(opciones.embeds) ? opciones.embeds : undefined, tts: opciones.tts || false, components: comp }) })) .then((m) => { resolve(JSON.parse(m)); }) .catch((err) => { console.log(err.message) reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } crearMD() { const promesa = new Promise((resolve, reject) => { request.post({ url: `https://discord.com/api/v9/users/@me/channels`, headers: { Authorization: 'Bot ' + this._bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify({ recipient_id: this.id }) }) .then((c) => { this._bot.canalesMD.set(this.id, JSON.parse(c)); resolve(new CanalMD(JSON.parse(c), this._bot)); }) .catch((err) => { reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } urlAvatar(t = 2048) { if(!this._datos.avatar) return null; if(!tamanosImagen.includes(t)) throw new TypeError('No puede tener ese tamaño la imagen. Puedes poner: 16, 32, 64, 128, 256, 512, 1024, 2048, 4096'); const formato = this._datos.avatar.startsWith('a_') ? 'gif' : 'webp'; return `https://cdn.discordapp.com/avatars/${this.id}/${this._datos.avatar}.${formato}${t?`?size=${t}`:''}`; } get presencia() { return this._bot.presencias.has(this.id) ? this._bot.presencias.get(this.id) : null; } get banderas() { const array = Object.entries(banderas).filter(([n, v]) => (this._datos.public_flags & v) == v).map(([n]) => n); return { num: this._datos.public_flags, array }; } get _canal() { return this._bot.canalesMD.has(this.id) ? this._bot.canalesMD.get(this.id) : null; } toString() { return `<@${this.id}>`; } }