-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (120 loc) · 6.99 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
//
const fs = require('fs');
const Discord = require('discord.js');
const vars = require('./ressources/vars.js');
const Game = require('./engine/Game.js');
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
bot.gameEvents = new Discord.Collection();
bot.interfaceReactionEvents = new Discord.Collection();
//bot.login('NTM0NzU4Njc1MjY1NjE3OTIw.XjITPw.Q4sYlr8eWUZuPgtaZjwhxcLxAyE'); //bot release
bot.login('NTM5OTIyNjA0NzEwOTUyOTcx.XjITuA.xIWsGmwl9MvNtTt2rgVYlt7hlgA'); //bot TEST
const prefix = '$';
/*
Lecture des commandes dans le dossier commands
*/
fs.readdir('./commands/', (err, files) => { // This line of code reads all files from our "commands" folder
if(err) console.error(err); // If there is an error, send it to console.
let cmds = files.filter(f => f.split('.').pop() === 'js'); // This array will contain all files with 'js' extension
if(Object.keys(cmds).length <= 0) { // If array contains less than, or exactly 0 objects, send message that there is no commands to load
return console.log('No command files found...');
}else{
console.log(`Il y a ${Object.keys(cmds).length} commandes...`); // Optional, prints how many commands we are loading
}
cmds.forEach((f, i) => { // Execute this code for every file from our array, f is files, and i stands for number
const command = require(`./commands/${f}`); // Require file with command
console.log(`commande: ${command.help.name} chargé!`); // When command is loaded, log it into console
bot.commands.set(command.help.name, command); // Push command name and file with it's code to our Collection
});
});
fs.readdir('./gameEvents/', (err, files) => { // This line of code reads all files from our "gameEvents" folder
if(err) console.error(err); // If there is an error, send it to console.
let cmds = files.filter(f => f.split('.').pop() === 'js'); // This array will contain all files with 'js' extension
if(Object.keys(cmds).length <= 0) { // If array contains less than, or exactly 0 objects, send message that there is no gameEvents to load
return console.log('No gameEvents files found...');
}else{
console.log(`Il y a ${Object.keys(cmds).length} evenements de jeu...`); // Optional, prints how many gameEvents we are loading
}
cmds.forEach((f, i) => { // Execute this code for every file from our array, f is files, and i stands for number
const gameEvent = require(`./gameEvents/${f}`); // Require file with gameEvents
console.log(`evenement de jeu: ${gameEvent.help.name} chargé!`); // When gameEvents is loaded, log it into console
bot.gameEvents.set(gameEvent.help.name, gameEvent); // Push gameEvents name and file with it's code to our Collection
});
});
fs.readdir('./interfaceEvents/', (err, files) => { // This line of code reads all files from our "interfaceEvents" folder
if(err) console.error(err); // If there is an error, send it to console.
let cmds = files.filter(f => f.split('.').pop() === 'js'); // This array will contain all files with 'js' extension
if(Object.keys(cmds).length <= 0) { // If array contains less than, or exactly 0 objects, send message that there is no interfaceEvents to load
return console.log('No interfaceEvents files found...');
}else{
console.log(`Il y a ${Object.keys(cmds).length} evenements de l'interface...`); // Optional, prints how many interfaceEvents we are loading
}
cmds.forEach((f, i) => { // Execute this code for every file from our array, f is files, and i stands for number
const interfaceEvent = require(`./interfaceEvents/${f}`); // Require file with interfaceEvents
console.log(`evenement de jeu: ${interfaceEvent.help.name} chargé!`); // When interfaceEvents is loaded, log it into console
bot.interfaceEvents.set(interfaceEvent.help.name, interfaceEvent); // Push interfaceEvents name and file with it's code to our Collection
});
});
/* init core */
Game.initAvaibleGames();
Game.initGamesStorage();
/* init core end */
/* evenement du bot */
bot.on('ready', function () {
console.log("Je suis connecté !")
console.log("Il y a ",bot.guilds.cache.size," serveurs connectés: ");
vars.initDescriptions();
let guilds = bot.guilds.cache.array();
//console.log("voici les serveurs qui ont le bot:");
for(guild in guilds){
let id = guilds[guild].id
Game.addGameToStorage(null, id);
//console.log(id);
let filter = channel => channel.type == 'text' && channel.guild.id == id;
let channel = bot.channels.cache.filter(filter).first();
channel.send("Le bot est connecté à votre serveur");
}
});
//joined a server
bot.on("guildCreate", guild => {
console.log("Joined a new guild: " + guild.name);
Game.addGameToStorage(null, guild.id);
//Your other stuff like adding to guildArray
})
//removed from a server
bot.on("guildDelete", guild => {
console.log("Left a guild: " + guild.name);
Game.addGameToStorage(null, guild.id);
});
bot.on('message', async data => {
const messageArray = data.content.split(" "); // Message split into separate objects for example message containing 'Hello there!' would look like this: ["Hello", "there!"]
const command = messageArray[0]; // Command with prefix
const args = messageArray.slice(1); // Arguments given to command
let cmd = bot.commands.get(command.slice(prefix.length)); // We need to get command from our collection
if(command.startsWith(prefix)){ // If command object starts with prefix
if(cmd){
console.log(`--${data.author.username}#${data.author.discriminator} -> '${command}' sur le serveur ${data.guild.name}`); // We can log it to console
cmd.run(bot, data, args); // If cmd object isn't null, execute command code
}
}
});
bot.on('gameEvent', async (gameEvent, data, args)=> {
let cmd = bot.gameEvents.get(gameEvent); // We need to get gameEvent from our collection
if(cmd){
console.log(`--Evenement de jeu -> '${gameEvent}' sur le serveur ${data.guild.name}`); // We can log it to console
cmd.run(bot, data, args); // If cmd object isn't null, execute gameEvent code
}
});
bot.on('interfaceEvent', async (interfaceEvent, data, args) => {//args = {"handlerOn": [], "messageToListen": message}
if(Array.isArray(args.handlerOn) && args.messageToListen instanceof Discord.Message){
let cmd = bot.interfaceEvents.get(interfaceEvent); // We need to get interfaceEvent from our collection
if(cmd) cmd.run(bot, data, args); // If cmd object isn't null, execute gameEvent code
console.log(`--Handler de reactions -> '${interfacenEvent}' sur le serveur ${data.guild.name}`); // We can log it to console
}else{
if(!(args.messageToListen instanceof Discord.Message)){
console.log("arguments: ",args.messageToListen," non valides pour: ", interfaceEvent);
}else{
console.log("arguments: ",args.handlerOn," non valides pour: ", interfaceEvent);
}
}
});