discord-vatron
Version:
Módulo para facilitar la interacción con la API de Discord
101 lines (87 loc) • 4.29 kB
JavaScript
const request = require('request-promise-native');
const Coleccion = require('./Coleccion.js');
const Canal = require('./Canal.js');
const Mensaje = require('./Mensaje.js');
const { errores } = require('../../props.js');
const tipos = require('../../props.js').tiposCanales;
const Colector = require('./Colector.js');
module.exports = class CanalTexto extends Canal {
constructor(datos, bot) {
super(datos, bot);
this.tema = datos.topic || '';
this.nsfw = datos.nsfw || false;
this.tiempoPausa = datos.rate_limit_per_user || 0;
this.mensajes = new Coleccion();
}
async enviar(opciones = {}) {
if(!opciones.contenido && !opciones.embeds) throw new Error('[DISCORD-VATRON] No hay contenido ni embeds en lo que quieres enviar.');
const promesa = new Promise((resolve, reject) => {
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.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,
message_reference: opciones.responder && (opciones.responder instanceof Mensaje) ? {
message_id: opciones.responder.id,
channel_id: opciones.responder.canal.id,
guild_id: opciones.responder.servidor.id,
fail_if_not_exists: false
} : null
}) })
.then((m) => {
resolve(new Mensaje(JSON.parse(m), this.bot));
})
.catch(err => {
reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message);
});
});
return promesa;
}
get ultimoMensaje() {
return this.mensajes.ultimo() || null;
}
async obtenerMensaje(id) {
const m = await request.get({ url: `https://discord.com/api/v9/channels/${this.id}/messages/${id}`, headers: { Authorization: 'Bot ' + this.bot.token } })
.catch(err => {
//const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\([aA-zZ]){1,}/g, ''));
console.error('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message);
});
if(!m) return null;
const mensaje = new Mensaje(JSON.parse(m), this.bot);
this.mensajes.set(mensaje.id, mensaje);
return mensaje;
}
crearColectorMensajes(filtro, opciones) {
const colector = new Colector(filtro, opciones);
this.bot.on('mensaje', (msg) => {
if(msg.canal.id != this.id) return;
colector._enviar(msg);
});
return colector;
}
editar(opciones) {
const promesa = new Promise((resolve, reject) => {
const opc = {
name: opciones.nombre || this.nombre,
position: opciones.posicion || this.posicion,
topic: opciones.tema || this.tema,
nsfw: opciones.nsfw || this.nsfw,
parent_id: opciones.categoria ? opciones.categoria.id || opciones.categoria : (this.categoria ? this.categoria.id : null)
};
request.patch({ url: `https://discord.com/api/v9/channels/${this.id}`, headers: { Authorization: 'Bot ' + this.bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify(opc) })
.then(c => {
resolve(new CanalTexto(JSON.parse(c), this.bot));
})
.catch(err => {
reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message);
});
});
return promesa;
}
}