UNPKG

discord-vatron

Version:

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

158 lines (133 loc) 6.2 kB
const request = require('request-promise-native'); const Embed = require('./Embed.js'); const { errores } = require('../../props.js'); const Reaccion = require('./Reaccion.js'); const Coleccion = require('./Coleccion.js'); const Menciones = require('./Menciones.js'); const Colector = require('./Colector.js'); const Archivo = require('./Archivo.js'); module.exports = class Mensaje { constructor(datos, bot, sv, usuario) { this._datos = datos; this.bot = bot; this.contenido = datos.content || ''; this.id = datos.id; this.canal = bot.canales.has(datos.channel_id) ? bot.canales.get(datos.channel_id) : null; this.marcaTiempoEnviado = datos.timestamp; this.marcaTiempoEditado = datos.edited_timestamp || null; this.tts = datos.tts || false; this.everyoneMencionado = datos.mention_everyone || false; this.fijado = datos.pinned || false; this._datos.author = this._datos.author || usuario || {}; this._datos.guild_id = this._datos.guild_id || (sv ? sv.id : ''); } eliminar() { try { request.delete({ url: `https://discord.com/api/v9/channels/${this.canal.id}/messages/${this.id}`, headers: { Authorization: 'Bot ' + this.bot.token } }) .catch(err => { const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\/g, '')); console.error('[DISCORD-VATRON] ' + errores[json.code] || err.message); }); } catch(err) { console.error('[DISCORD-VATRON] ' + err.message); } } editar(opciones) { if(!this.editable) return console.error('[DISCORD-VATRON] No se puede editar este mensaje porque no es del bot'); if(!opciones) return console.error('[DISCORD-VATRON] No hay opciones.'); if(!opciones.contenido && !opciones.embeds) return console.error('[DISCORD-VATRON] No hay contenido ni embeds.'); 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.patch({ url: `https://discord.com/api/v9/channels/${this.canal.id}/messages/${this.id}`, headers: { Authorization: 'Bot ' + this.bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify({ content: opciones.contenido ? opciones.contenido.toString() : '', embeds: opciones.embeds || undefined, components: comp }) }) .catch(err => { console.log(err.message); console.error('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); } responder(opciones) { if(!opciones) return; opciones.responder = this; return this.canal.enviar(opciones); } reaccionar(e) { const promesa = new Promise((resolve, reject) => { let emoji = e; if(this.bot.emojis.has(e)) emoji = this.bot.emojis.get(e); else if(this.bot.emojis.has(e.id)) emoji = this.bot.emojis.get(e.id); if(emoji.nombre) { emoji = `${emoji.nombre}:${emoji.id}`; } else emoji = encodeURIComponent(e); request.put({ url: `https://discord.com/api/v9/channels/${this.canal.id}/messages/${this.id}/reactions/${emoji}/@me`, headers: { Authorization: 'Bot ' + this.bot.token } }) .then(() => { resolve(this); }) .catch((err) => { reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } crearColectorBotones(filtro, opciones) { const colector = new Colector(filtro, opciones); this.bot.on('interaccionCreada', (i) => { if(i.mensaje.id != this.id) return; colector._enviar(i); }); return colector; } /*crearColectorReacciones(filtro, opciones) { const colector = new Colector(filtro, opciones); this.bot.on('mensajeReaccionAnadida', (r) => { colector._enviar(r); }); return colector; }*/ get miembro() { return this.servidor ? this.servidor.miembros.has(this.autor.id) ? this.servidor.miembros.get(this.autor.id) : null : null; } get servidor() { return this.bot.servidores.has(this._datos.guild_id) ? this.bot.servidores.get(this._datos.guild_id) : null; } get embeds() { return this._datos.embeds.length ? this._datos.embeds.map(e => new Embed(e)) : []; } get editable() { return this.autor.id == this.bot.usuario.id; } get url() { return `https://discord.com/channels/${this.servidor.id}/${this.canal.id}/${this.id}`; } get esWebhook() { return this._datos.webhook_id ? true : false; } get autor() { if(this.esWebhook) return { bot: true }; if(!this._datos.author) return {}; return this.bot.usuarios.has(this._datos.author.id) ? this.bot.usuarios.get(this._datos.author.id) : {}; } get reacciones() { const col = new Coleccion(); if(this._datos.reactions) { for(var r of this._datos.reactions) { col.set(r.emoji.id || r.emoji.name, new Reaccion(r, this.bot, this.miembro, this.servidor, this)); } } return col; } get menciones() { if(!this.contenido) return new Menciones(); const regexCanales = /\<\#[0-9]{18}\>/g; const canales = this.contenido.match(regexCanales) ? this.contenido.match(regexCanales).map(x => x.replace(/(\<)|(\#)|(\>)/g, '')) : []; return new Menciones({ miembros: this._datos.mentions, roles: this._datos.mention_roles, canales }, this.bot, this.servidor); } get archivos() { return this._datos.attachments ? this._datos.attachments.map(a => new Archivo(a)) : []; } }