UNPKG

discord-vatron

Version:

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

181 lines (153 loc) 8.4 kB
const Coleccion = require("./Coleccion.js"); const Miembro = require('./Miembro.js'); const Role = require('./Role.js'); const { tamanosImagen, errores, colores, tiposCanales } = require('../../props.js'); const EstadoVoz = require("./EstadoVoz.js"); const request = require('request-promise-native'); const CanalTexto = require('./CanalTexto.js'), CanalVoz = require('./CanalVoz.js'), CanalCategoria = require('./CanalCategoria.js'), CanalMD = require('./CanalMD.js'); module.exports = class Servidor { constructor(datos, bot) { this._datos = datos; this.bot = bot; this.nombre = datos.name; this.id = datos.id; this.nivelVerificacion = datos.verification_level || 0; this.tiempoAFK = datos.afk_timeout || null; if(datos.members) { this.miembros = new Coleccion(); for(var m of datos.members) { const miembro = new Miembro(m, bot, this); this.miembros.set(miembro.id, miembro); } } if(datos.voice_states) { this.estadosVoz = new Coleccion(); for(var e of datos.voice_states) { const estado = new EstadoVoz(e, bot, this); this.estadosVoz.set(estado.miembro.usuario.id, estado); } } } editar(opciones) { const promesa = new Promise((resolve, reject) => { const opc = { name: opciones.nombre || this.nombre, region: opciones.region || this.region, verification_level: opciones.nivelVerificacion || this.nivelVerificacion, afk_channel_id: opciones.canalAFK && typeof(opciones.canalAFK) == 'object' ? opciones.canalAFK ? opciones.canalAFK.id ? opciones.canalAFK.id : (opciones.canalAFK ? opciones.canalAFK : null) : (this.canalAFK ? this.canalAFK.id : null) : null, afk_timeout: opciones.tiempoAFK || this.tiempoAFK || 0, icon: opciones.icono || this.icono(), splash: opciones.salpicadura || this.salpicadura, system_channel_id: opciones.canalSistema && typeof(opciones.canalSistema) == 'object' ? opciones.canalSistema ? opciones.canalSistema.id ? opciones.canalSistema.id : (opciones.canalSistema ? opciones.canalSistema : null) : (this.canalSistema ? this.canalSistema.id : null) : null, rules_channel_id: opciones.canalReglas && typeof(opciones.canalReglas) == 'object' ? opciones.canalReglas ? opciones.canalReglas.id ? opciones.canalReglas.id : (opciones.canalReglas ? opciones.canalReglas : null) : (this.canalReglas ? this.canalReglas.id : null) : null }; request.patch({ url: `https://discord.com/api/v9/guilds/${this.id}`, headers: { Authorization: 'Bot ' + this.bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify(opc) }) .then(s => { resolve(new Servidor(JSON.parse(s), this.bot)); }) .catch(err => { reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } crearRole(opciones) { const promesa = new Promise((resolve, reject) => { const opc = { name: opciones.nombre || null, color: colores[opciones.color] ? parseInt(colores[opciones.color], 16) : (typeof(opciones.color) == 'string' ? parseInt(opciones.color.replace(/\#/g, ''), 16) : opciones.color) || null, hoist: typeof(opciones.separado) == 'boolean' ? opciones.separado : false, mentionable: typeof(opciones.mencionable) == 'boolean' ? opciones.mencionable : false }; request.post({ url: `https://discord.com/api/v9/guilds/${this.id}/roles`, headers: { Authorization: 'Bot ' + this.bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify(opc) }) .then(r => { this.bot.roles._setRole(JSON.parse(r), this.bot, this); resolve(new Role(JSON.parse(r), this.bot)); }) .catch(err => { reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } crearCanal(opciones) { const promesa = new Promise((resolve, reject) => { let opc = { name: opciones.nombre || 'sin-nombre', type: tiposCanales.indexOf(opciones.tipo) >= 0 ? tiposCanales.indexOf(opciones.tipo) : opciones.tipo >= 0 ? opciones.tipo : 0, topic: opciones.tema || '', user_limit: (opciones.tipo == 2 || opciones.tipo == 'voz') ? opciones.limiteUsuarios || 0 : null, rate_limit_per_user: (opciones.tipo == 0 || opciones.tipo == 'texto') ? opciones.tiempoPausa || 0 : null, position: opciones.posicion || 0, parent_id: opciones.categoria ? opciones.categoria.id || opciones.categoria : null, nsfw: (opciones.tipo == 0 || opciones.tipo == 'texto') ? opciones.nsfw || false : null }; request.post({ url: `https://discord.com/api/v9/guilds/${this.id}/channels`, headers: { Authorization: 'Bot ' + this.bot.token, 'Content-Type': 'application/json' }, body: JSON.stringify(opc) }) .then(c => { const tipos = [ CanalTexto, CanalVoz, CanalCategoria, CanalMD ]; if(!tipos[JSON.parse(c).type]) return; let canal = new (tipos[JSON.parse(c).type])(JSON.parse(c), this.bot); this.bot.canales._setCanal(JSON.parse(c), this.bot, this); resolve(canal); }) .catch(err => { reject('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); }); return promesa; } icono(t = 2048) { if(!this._datos.icon) 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.icon.startsWith('a_') ? 'gif' : 'webp'; return `https://cdn.discordapp.com/icons/${this.id}/${this._datos.icon}.${formato}${t?`?size=${t}`:''}`; } async desbanear(idMiembro) { if(idMiembro.id) idMiembro = idMiembro.id; request.delete({ url: `https://discord.com/api/v9/guilds/${this.id}/bans/${idMiembro}`, headers: { Authorization: 'Bot ' + this._bot.token } }) .catch(err => { //const json = JSON.parse(err.message.slice(err.message.indexOf('"')+1, -1).replace(/\\/g, '')); throw new Error('[DISCORD-VATRON] ' + errores[err.statusCode] || err.message); }); return true; } get region() { return this._datos.region || null; } get canales() { return this.bot.canales.filtrar(c => c.servidor.id == this._datos.id); } get roles() { return this.bot.roles.filtrar(r => r.servidor.id == this._datos.id); } get emojis() { return this.bot.emojis.filtrar(e => e.servidor.id == this._datos.id); } get salpicadura() { if(!this._datos.splash) return null; return `https://cdn.discordapp.com/splashes/${this.id}/${this._datos.splash}.webp`; } get dueno() { return this.miembros.has(this._datos.owner_id) ? this.miembros.get(this._datos.owner_id) : null; } get yo() { return this.miembros.has(this.bot.usuario.id) ? this.miembros.get(this.bot.usuario.id) : null; } get canalAFK() { return this.canales.has(this._datos.afk_channel_id) ? this.canales.get(this._datos.afk_channel_id) : null; } get canalSistema() { return this.canales.has(this._datos.system_channel_id) ? this.canales.get(this._datos.system_channel_id) : null; } get canalReglas() { return this.canales.has(this._datos.rules_channel_id) ? this.canales.get(this._datos.rules_channel_id) : null; } }