UNPKG

discord-vatron

Version:

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

164 lines (139 loc) 6.68 kB
const request = require('request-promise-native'); const Coleccion = require('./Coleccion.js'); const Role = require('./Role.js'); const { errores, tamanosImagen, permisos } = require('../../props.js'); module.exports = class Miembro { constructor(datos, bot, sv) { this._datos = datos; this._bot = bot; this.id = datos.user.id; this.marcaTiempoUnido = datos.joined_at; this.servidor = sv; this.avatar = datos.avatar || ''; } anadirRole(idR) { const promesa = new Promise((resolve, reject) => { if(!idR) throw new TypeError('Necesitas específicar la ID de un role.'); if(idR.id) idR = idR.id; request.put({ url: `https://discord.com/api/v9/guilds/${this.servidor.id}/members/${this.id}/roles/${idR}`, headers: { Authorization: 'Bot ' + this._bot.token } }) .then((r) => { resolve(new Role(JSON.parse(r), this._bot)); }) .catch(err => { /*const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\/g, '')); console.log(json.code)*/ reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } eliminarRole(idR) { const promesa = new Promise((resolve, reject) => { if(!idR) throw new TypeError('Necesitas específicar la ID de un role.'); if(idR.id) idR = idR.id; request.delete({ url: `https://discord.com/api/v9/guilds/${this.servidor.id}/members/${this.id}/roles/${idR}`, headers: { Authorization: 'Bot ' + this._bot.token } }) .then((r) => { resolve(new Role(JSON.parse(r), this._bot)); }) .catch(err => { //const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\/g, '')); reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } urlAvatar(opciones = { tamano: 2048, defecto: false }) { if(!this._datos.avatar && !opciones.defecto) return null; else if(!this._datos.avatar && opciones.defecto) return this.usuario.urlAvatar(opciones.tamano || undefined); if(!tamanosImagen.includes(opciones.tamano)) 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/guilds/${this.servidor.id}/users/${this.id}/avatars/${this._datos.avatar}.${formato}${opciones.tamano?`?size=${opciones.tamano}`:''}`; } banear(opciones = {}) { const promesa = new Promise((resolve, reject) => { let miembro = this; request.put({ url: `https://discord.com/api/v9/guilds/${this.servidor.id}/bans/${this.id}`, headers: { Authorization: 'Bot ' + this._bot.token, 'X-Audit-Log-Reason': opciones.razon || '' }, body: JSON.stringify({ delete_message_days: opciones.dias ? opciones.dias > 7 ? 7 : opciones.dias < 0 ? 0 : opciones.dias : 0, reason: opciones.razon || '' }) }) .then(() => { resolve(miembro); }) .catch(err => { //const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\/g, '')); reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } expulsar(razon) { const promesa = new Promise((resolve, reject) => { let miembro = this; request.delete({ url: `https://discord.com/api/v9/guilds/${this.servidor.id}/members/${this.id}`, headers: { Authorization: 'Bot ' + this._bot.token, 'X-Audit-Log-Reason': razon || '' }, body: JSON.stringify({ reason: razon || '' }) }) .then(() => { resolve(miembro); }) .catch(err => { //const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\/g, '')); reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } apodo(defecto) { return (this._datos.nick || (defecto ? this.usuario.nombre : '')); } estApodo(ap) { const promesa = new Promise((resolve, reject) => { request.patch({ url: `https://discord.com/api/v9/guilds/${this.servidor.id}/members/${this.id}`, headers: { Authorization: 'Bot ' + this._bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify({ nick: ap || this.apodo(true) }) }) .then(() => { resolve(this.servidor.miembros.has(this.id) ? this.servidor.miembros.get(this.id) : null); }) .catch((err) => { reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } /*get silenciado() { return this._datos.mute || false; } get ensordecido() { return this._datos.deaf || false; }*/ get usuario() { return this._bot.usuarios.has(this.id) ? this._bot.usuarios.get(this.id) : null; } get roles() { return this.servidor.roles.filtrar(r => this._datos.roles.includes(r.id)); } get roleAlto() { return this.roles.size ? this.roles.ordenar((a, b) => b.posicion - a.posicion).array()[0] : null; } get permisos() { const col = new Coleccion(); if(this.id != this.servidor.dueno.id) { for(var r of this.roles.array()) { for(var p of r.permisos.array) { col.set(p, p); } } } else if(this.id == this.servidor.dueno.id) { Object.keys(permisos).map((p) => col.set(p, p)); } if(col.has('ADMINISTRADOR')) { Object.keys(permisos).map((p) => col.set(p, p)); } return col; } get canalVoz() { let c = this.servidor ? this.servidor.estadosVoz.has(this.id) ? this.servidor.estadosVoz.get(this.id)._datos.channel_id : null : null; return this._bot.canales.has(c) ? this._bot.canales.get(c) : null; } toString() { return `<@!${this.id}>`; } }