-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
91 lines (79 loc) · 2.15 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
const fs = require('fs');
const Discord = require('discord.js');
const Client = require('./client/Client');
const {
prefix,
token,
} = require('./config.json');
const client = new Client();
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);
}
// Connecting to Discord
client.once('ready', () => {
console.log('Ready!');
});
client.once('reconnecting', () => {
console.log('Reconnecting!');
});
client.once('disconnect', () => {
console.log('Disconnect!');
});
const servers = [];
function getServer(message) {
for(const server of servers) {
if (server.guild.id === message.channel.guild.id) {
return server;
}
}
const configFileName = './server_configs/' + message.channel.guild.id + '_config.json';
const server = {
guild: message.channel.guild,
dispatcher: null,
timeoutLeave: undefined,
timeout: 30,
configFileName: configFileName,
getConfig: () => {
let configFile = '{}';
try {
configFile = fs.readFileSync(configFileName);
}
catch (e) {
configFile = '{"volume":100,"timeout":30}';
}
return JSON.parse(configFile);
},
saveConfig: (config) => {
fs.writeFileSync(configFileName, JSON.stringify(config));
},
prefix: prefix,
};
server.timeout = server.getConfig().timeout || 30;
server.prefix = server.getConfig().prefix || prefix;
servers.push(server);
return server;
}
client.on('message', async message => {
const server = getServer(message);
const args = message.content.slice(server.prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (message.author.bot) return;
if (!message.content.startsWith(server.prefix)) return;
try {
if(commandName == 'ban' || commandName == 'userinfo') {
command.execute(message, client);
}
else {
command.execute(message, server);
}
}
catch (error) {
console.error(error);
message.reply('There was an error trying to execute that command!');
}
});
client.login(token);