-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
82 lines (60 loc) · 2.16 KB
/
bot.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
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
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}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
client.commands.set(command.name, command);
}
const conf = require('./config.json');
let server = {
config: conf,
grafana_url: conf['grafana_render_url'],
knet_key: conf['knet_key'],
knet_url: conf['knet_url'],
token: conf['token'],
loutres: {},
loadLoutres: function(){
if(fs.existsSync('loutres.json'))
this.loutres = JSON.parse(fs.readFileSync('loutres.json'));
},
saveLoutres: function(){
fs.writeFileSync('loutres.json', JSON.stringify(this.loutres));
}
};
server.loadLoutres();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setGame('Jardin de loutres');
});
client.on('message', async msg => {
if(msg.author.bot) return;
if(msg.content.indexOf(server.config.prefix) !== 0) return;
const args = msg.content.slice(server.config.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;
if (command.guildOnly && msg.channel.type !== 'text') {
return msg.reply('I can\'t execute that command inside DMs!');
}
try {
command.execute(msg, args, server);
} catch (error) {
console.error(error);
msg.reply('J\'ai perdu. (Ou j\'ai planté mais je marche encore)');
}
});
// Prevent fails
process.on("unhandledRejection", err => {
console.error("Uncaught Promise Error: \n" + err.stack);
});
client.on('disconnect', async () => {
client.destroy();
client.login(config.clientToken);
});
client.on("error", (err) => console.log(err));
client.login(server.token);