forked from powange/discord-sot-alliance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
316 lines (260 loc) · 11.8 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const fs = require('fs');
const Discord = require('discord.js');
const AllianceManager = require('./src/allianceManager');
const UpdateAlliance = require('./src/updateAlliance');
const isIp = require('is-ip');
const client = new Discord.Client({partials: ['MESSAGE', 'REACTION']});
client.commands = new Discord.Collection();
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);
}
const cooldowns = new Discord.Collection();
// Here we load the config.json file that contains our token and our prefix values.
const {prefix, token} = require('./config.json');
// token contains the bot's token
// prefix contains the message prefix.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("guildCreate", guild => {
// This event triggers when the bot joins a guild.
console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
client.user.setActivity(`Serving ${client.guilds.size} servers`);
});
client.on("guildDelete", guild => {
// this event triggers when the bot is removed from a guild.
console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
client.user.setActivity(`Serving ${client.guilds.size} servers`);
});
client.on('message', async message => {
// It's good practice to ignore other bots. This also makes your bot ignore itself
// and not get into a spam loop (we call that "botception").
if (message.author.bot) return;
try {
const alliancesManager = AllianceManager.getInstance(message.guild);
let alliancesMatch = alliancesManager.alliances.filter(a => a.textChannelID === message.channel.id);
if (alliancesMatch.size) {
const alliance = alliancesMatch.first();
alliancesManager.removeAFK(message.member, alliance);
const reg = /((\d{1,3}\.){3}\d{1,3}:\d+)/g;
const found = message.content.trim().match(reg)
if (found !== null) {
let messageIPPort = found[0];
const messageSplit = messageIPPort.split(':');
if (isIp.v4(messageSplit[0])) {
alliance.setIp(message.member, messageIPPort).then(participant => {
alliancesManager.saveAlliance(alliance);
alliance.updateMessageEmbed();
}).catch(err => console.log(err));
message.delete();
return;
}
}
if (message.content.trim() === `afk`) {
alliance.setAFK(message.member).then(async participant => {
alliance.updateMessageEmbed();
message.delete();
}).catch(err => {
console.log(err);
reaction.users.remove(user);
});
return;
}
if (message.content.trim() === `software`) {
sendSotFleetCreator(message.member).then(async participant => {
message.delete();
}).catch(err => {
console.log(err);
});
return;
}
if (alliance.proprietaireID === message.author.id) {
let members = message.mentions.members.filter(u => u.user.bot === false);
if (members.size) {
if (message.content.startsWith(`afk `)) {
const user = members.first();
alliance.setAFK(user).then(async participant => {
alliance.updateMessageEmbed();
console.log(`Le participant ${user} a été mis en mode AFK`);
await replyMessageTemp(message, `Le participant ${user} a été mis en mode AFK`);
}).catch(err => {
console.log(err);
reaction.users.remove(user);
});
return;
} else if (message.content.startsWith(`creator `)) {
const user = await alliancesManager.setProprietaireID(members.first(), alliance);
alliance.updateMessageEmbed();
console.log(`Le nouveau créateur de cette alliance est ${user}`);
await replyMessageTemp(message, `Le nouveau créateur de cette alliance est ${user}`);
return;
}
} else if (message.content === `countdownSpeed`) {
let countdownIsSpeed = alliance.switchCountdownSpeed();
await replyMessageTemp(message, `Le décompte est désormais en **mode ` + (countdownIsSpeed ? 'rapide' : 'lent') + '**.');
return;
} else if (message.content === `muteMembers`) {
let muteMembersStatut = alliance.switchMuteMembers();
if (muteMembersStatut) {
await replyMessageTemp(message, `Les membres du Vocal seront mute pendant le décompte.`);
} else {
await replyMessageTemp(message, `Les membres du Vocal ne seront pas mute pendant le décompte.`);
}
return;
}
}
await replyMessageTemp(message, 'Commande non authorisé');
return;
}
} catch (e) {
console.error(e);
await replyMessageTemp(message, 'there was an error trying to execute that command!');
}
// Also good practice to ignore any message that does not start with our prefix,
// which is set in the configuration file.
if (message.content.indexOf(prefix) !== 0) return;
// Here we separate our "command" name, and our "arguments" for the command.
// e.g. if we have the message "+say Is this the real life?" , we'll get the following:
// command = say
// args = ["Is", "this", "the", "real", "life?"]
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
// This event will run on every single message received, from any channel or DM.
if (command.guildOnly && message.channel.type !== 'text') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
message.channel.send(reply);
return;
}
if (!cooldowns.has(message.guild.id)) {
cooldowns.set(message.guild.id, new Discord.Collection());
}
const guildCooldowns = cooldowns.get(message.guild.id);
if (!guildCooldowns.has(command.name)) {
guildCooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = guildCooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
await replyMessageTemp(message, `please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
return;
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
await replyMessageTemp(message, 'there was an error trying to execute that command!');
}
});
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.log('Something went wrong when fetching the message: ', error);
return;
}
}
if (user.bot === false) {
const alliancesManager = AllianceManager.getInstance(reaction.message.guild);
let alliancesMatch = alliancesManager.alliances.filter(a => a.messageID === reaction.message.id);
if (alliancesMatch.size) {
let alliance = alliancesMatch.first();
alliancesManager.addReaction(reaction, user, alliance);
}
}
});
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.log('Something went wrong when fetching the message: ', error);
return;
}
}
if (user.bot === false) {
const alliancesManager = AllianceManager.getInstance(reaction.message.guild);
let alliancesMatch = alliancesManager.alliances.filter(a => a.messageID === reaction.message.id);
if (alliancesMatch.size) {
let alliance = alliancesMatch.first();
alliancesManager.removeReaction(reaction, user, alliance);
}
} else if (user.bot === true && user.id === client.user.id) {
const emojis = ['🤚', '⚓', '🗑️', '⏳', '🔄', '❌'];
if (emojis.includes(reaction.emoji.name)) {
const alliancesManager = AllianceManager.getInstance(reaction.message.guild);
let alliancesMatch = alliancesManager.alliances.filter(a => a.messageID === reaction.message.id);
if (alliancesMatch.size) {
let alliance = alliancesMatch.first();
if (alliance.messageID !== null) {
alliance.guild.channels.cache.get(alliance.textChannelID)
.messages.fetch(alliance.messageID).then(message => {
message.react(reaction.emoji.name);
}).catch(console.error);
}
}
}
}
});
client.on('voiceStateUpdate', async (oldState, newState) => {
if (newState.member.user.bot === false) {
const alliancesManager = AllianceManager.getInstance(newState.guild);
let alliancesMatch = alliancesManager.alliances.filter(a => {
return a.voiceChannelID === newState.channelID || a.voiceChannelID === oldState.channelID;
});
if (alliancesMatch.size) {
let alliance = alliancesMatch.first();
alliance.updateMessageEmbed()
}
}
});
client.login(token);
/**
* @param message {Message}
* @param text {string}
*/
async function replyMessageTemp(message, text) {
const msg = await message.reply(text);
await msg.delete({timeout: 5000}).catch(e => console.log(e));
message.delete();
}
async function sendSotServerFinder(user) {
return user.send({
files: [{
attachment: './Ressources/SoTServerFinder.exe',
name: 'SoTServerFinder.exe'
}]
});
}
async function sendSotFleetCreator(user) {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Créateur de Flotte SOT')
.setURL('https://fleetcreator.com/fr/')
.setDescription('Téléchargez Fleet Creator, lancez le, mettez vous en mode "En ligne", et liez votre compte Discord.')
return user.send(exampleEmbed);
}
function updateAllIP() {
const updateAlliance = UpdateAlliance.getInstance();
updateAlliance.updateAllIP();
}
setInterval(updateAllIP, 3000);
updateAllIP();