-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (129 loc) · 5.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// CONFIGURACIONES
const { prefix, min_cooldown, locationsChannelId, locationsMessagesId } = require('./config.json');
// INICIAR SESIÓN DEL CLIENTE
const Discord = require('discord.js');
const client = new Discord.Client({partials: ['MESSAGE', 'CHANNEL', 'REACTION', 'GUILD_MEMBER'] });
client.commands = new Discord.Collection();
client.login(process.env.TOKEN);
// CARGAR COMANDOS
const fs = require('fs');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// INICIALIZAR COOLDOWNS
const cooldowns = new Discord.Collection();
// ACTIVAR FUNCIONES
client.on('ready', readyDiscord);
client.on('message', readMessage);
//client.on('messageReactionAdd', addReaction);
// LOGIN
function readyDiscord() {
console.log('✅ Logged in.');
client.user.setActivity("tus problemas bb💕", { type: 'LISTENING' });
//cacheGuildMembers();
//setupLocationMessages();
}
// LEER MENSAJES
function readMessage(message) {
// LEER COMANDOS SEGÚN EL PREFIJO
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
// COMPROBAR SI EL COMANDO EXISTE
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
// COMPROBAR SI EL COMANDO PUEDE USARSE EN DMs
if (command.guildOnly && message.channel.type === 'dm') {
return message.channel.send('⛔ No puedo ejecutar ese comando en DM');
}
// COMPROBAR SI EL USUARIO CUMPLE LOS PERMISOS
if (command.admin && !message.member.hasPermission('ADMINISTRATOR')) {
return message.channel.send('⛔ Este comando sólo puede ejecutarlo un administrador!');
}
// COMPROBAR NECESITA Y CONTIENE ARGUMENTOS
if (command.needs_args && args.length < command.min_args) {
return message.channel.send(`💭 Te faltan argumentos, prueba con:\`\`\`${prefix}${command.name} ${command.usage}\`\`\``);
}
// COMPROBAR COOLDOWNS
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || min_cooldown) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`⏳ Espera ${timeLeft.toFixed(1)} segundos más antes de volver a usar el comando \`${command.name}\``);
}
}
try {
// EJECUTAR COMANDO
command.execute(message, args);
// ESTABLECER COOLDOWN
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
} catch (error) {
// RESOLVER ERROR DE EJECUCIÓN
console.error(error);
message.channel.send('💥 Ha habido un error ejecutando ese comando!');
}
// ELIMINAR MENSAJE
//if (command.removeMessage) message.delete();
}
// LEER REACCIONES
async function addReaction(reaction, user) {
console.log("Reaccion detectada.");
// Al recibir una reacción comprobar si es parcial o no
if (reaction.partial) {
// Si el mensaje al que pertenece se ha eliminado, el fetch podría dar un error
// en la API, así que hace falta tenerlo en cuenta
try {
await reaction.fetch();
} catch (error) {
console.error('Algo ha fallado al hacer fetch de un mensaje: ', error);
// Acaba si 'reaction.message.author' podría ser undefined/null
return;
}
}
// Ahora el mensaje está disponible en la cache
// COMPROBAR ID
if (reaction.message.id == locationMessageId)
client.commands.get(locationReact).execute(reaction, user);
}
async function cacheGuildMembers() {
const guilds = client.guilds.cache.map(async guild => {
console.log(`Cargando nuevo servidor: ${guild.name} con ${guild.memberCount} usuarios`);
await guild.members.fetch();
console.log(`Done`);
});
}
// EJECUTAR MAPAS DE COORDENADAS:
function setupLocationMessages() {
// Cargar canal
client.channels.fetch(locationsChannelId).then(locationsChannel =>
locationsMessagesId.map(messageId => {
console.log(messageId);
if (!locationsChannel) return console.log('wtf');
locationsChannel.messages.fetch(messageId)
//locationsChannel.messages.fetch(args[0])
.then(msg => {
msg.react("➕");
msg.react('➖');
console.log(`✅ Mensaje establecido correctamente. El contenido era: "${msg.embeds[0].title}"`);
// REACTION COLLECTOR
const filter = (reaction, user) => reaction.message.id == msg.id;
const collector = msg.createReactionCollector(filter);
const command = msg.client.commands.get('locationreact');
collector.on('collect', command.execute);
})
.catch(error => {
message.channel.send(`💥 Ha habido un error obteniendo el mensaje: \`\`\`${error}\`\`\``);
});
})
);
//Ejecutar por cada mensaje guardado
}