-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
107 lines (87 loc) · 3.14 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
const Discord = require("discord.js");
const bot = new Discord.Client({fetchAllMembers: true});
const fs = require("fs");
const moment = require("moment");
var settings = './settingsConfig/settings.json';
var file = require(settings)
var TOKEN = file.TOKEN;
const log = (msg) => {
console.log(`[${moment().format("YYYY-MM-DD HH:mm:ss")}] ${msg}`);
};
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
fs.readdir("./cmd/", (err, files) => {
if (err) console.error(err);
log(`Loading a total of ${files.length} commands.`);
files.forEach(f => {
let props = require(`./cmd/${f}`);
log(`Loading Command: ${props.help.name}`);
bot.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
bot.aliases.set(alias, props.help.name);
});
});
});
bot.on("guildMemberAdd", function(member) {
member.addRole(member.guild.roles.find("name", "Members")).then(() => {
})
});
bot.on("message", msg => {
var prefix = (file.prefix[msg.guild.id] == undefined) ? file.prefix["default"] : file.prefix[msg.guild.id];
if (msg.author.bot) return;
if (!msg.content.startsWith(prefix)) return;
if (msg.channel.type == "dm") return;
let command = msg.content.split(" ")[0].slice(prefix.length);
let params = msg.content.split(" ").slice(1);
let perms = bot.elevation(msg);
let cmd;
if (bot.commands.has(command)) {
cmd = bot.commands.get(command);
} else if (bot.aliases.has(command)) {
cmd = bot.commands.get(bot.aliases.get(command));
}
if (cmd) {
if (perms < cmd.conf.permLevel) return msg.channel.send("oops looks like you dont have the right permission level :(");
cmd.run(bot, msg, params, perms, prefix);
}
});
bot.on("ready", () => {
log(`Ready to serve ${bot.users.size} users, in ${bot.channels.size} channels of ${bot.guilds.size} servers.`);
});
bot.on("error", console.error);
bot.on("warn", console.warn);
bot.login(TOKEN);
bot.on('disconnect', function(erMsg, code) {
console.log('----- Bot disconnected from Discord with code', code, 'for reason:', erMsg, '-----');
bot.connect(TOKEN);
});
bot.reload = function (command) {
return new Promise((resolve, reject) => {
try {
delete require.cache[require.resolve(`./cmd/${command}`)];
let cmd = require(`./cmd/${command}`);
bot.commands.delete(command);
bot.aliases.forEach((cmd, alias) => {
if (cmd === command) bot.aliases.delete(alias);
});
bot.commands.set(command, cmd);
cmd.conf.aliases.forEach(alias => {
bot.aliases.set(alias, cmd.help.name);
});
resolve();
} catch (e) {
reject(e);
}
});
};
bot.elevation = function (msg) {
/* This function should resolve to an ELEVATION level which
is then sent to the command handler for verification*/
let permlvl = 0;
let mod_role = msg.guild.roles.find("name", "Members");
if (mod_role && msg.member.roles.has(mod_role.id)) permlvl = 2;
let admin_role = msg.guild.roles.find("name", "Higher-up Members");
if (admin_role && msg.member.roles.has(admin_role.id)) permlvl = 3;
if (msg.author.id === "103509994074312704") permlvl = 4;
return permlvl;
};