forked from Androz2091/AtlantaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlanta.js
121 lines (105 loc) · 4.65 KB
/
atlanta.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
// Load up the discord.js library
const { Client, Collection } = require("discord.js");
// We also load the rest of the things we need in this file:
const util = require("util"),
fs = require("fs"),
path = require("path"),
readdir = util.promisify(fs.readdir),
Idiot = require("idiotic-api"),
mongoose = require("mongoose");
// Creates new class
class Atlanta extends Client {
constructor (options) {
super(options);
this.config = require("./config.js"); // Load the config file
this.commands = new Collection(); // Creates new commands collection
this.aliases = new Collection(); // Creates new command aliases collection
this.logger = require("./utils/logger.js"); // Load the logger file
this.wait = util.promisify(setTimeout); // client.wait(1000) - Wait 1 second
this.functions = require("./utils/functions.js"); // Load the functions file
this.guildsData = require("./base/Guild.js"),
this.usersData = require("./base/User.js"),
this.queues = new Collection(); // This collection will be used for the music
}
// This function is used to load a command and add it to the collection
loadCommand (commandPath, commandName) {
try {
const props = new (require(`${commandPath}${path.sep}${commandName}`))(this);
this.logger.log(`Loading Command: ${props.help.name}. 👌`, "log");
props.conf.location = commandPath;
if (props.init){
props.init(this);
}
this.commands.set(props.help.name, props);
props.conf.aliases.forEach((alias) => {
this.aliases.set(alias, props.help.name);
});
return false;
} catch (e) {
return `Unable to load command ${commandName}: ${e}`;
}
}
// This function is used to unload a command (you need to load them again)
async unloadCommand (commandPath, commandName) {
let command;
if(this.commands.has(commandName)) {
command = this.commands.get(commandName);
} else if(this.aliases.has(commandName)){
command = this.commands.get(this.aliases.get(commandName));
}
if(!command){
return `The command \`${commandName}\` doesn't seem to exist, nor is it an alias. Try again!`;
}
if(command.shutdown){
await command.shutdown(this);
}
delete require.cache[require.resolve(`${commandPath}${path.sep}${commandName}.js`)];
return false;
}
}
// Creates new client
const client = new Atlanta();
const init = async () => {
// Search for all commands
let directories = await readdir("./commands/");
client.logger.log(`Loading a total of ${directories.length} categories.`, "log");
directories.forEach(async (dir) => {
let commands = await readdir("./commands/"+dir+"/");
commands.filter((cmd) => cmd.split(".").pop() === "js").forEach((cmd) => {
const response = client.loadCommand("./commands/"+dir, cmd);
if(response){
client.logger.log(response, "error");
}
});
});
// Then we load events, which will include our message and ready event.
const evtFiles = await readdir("./events/");
client.logger.log(`Loading a total of ${evtFiles.length} events.`, "log");
evtFiles.forEach((file) => {
const eventName = file.split(".")[0];
client.logger.log(`Loading Event: ${eventName}`);
const event = new (require(`./events/${file}`))(client);
client.on(eventName, (...args) => event.run(...args));
delete require.cache[require.resolve(`./events/${file}`)];
});
client.login(client.config.token); // Log in to the discord api
// connect to mongoose database
mongoose.connect(client.config.mongoDB, { useNewUrlParser: true }).then(() => {
client.logger.log("Connected to the Mongodb database.", "log");
}).catch((err) => {
client.logger.log("Unable to connect to the Mongodb database. Error:"+err, "error");
});
if(client.config.apiKeys.anidiots){
client.IdiotAPI = new Idiot.Client(client.config.apiKeys.anidiots, { dev: true });
}
};
init();
// if there are errors, log them
client.on("disconnect", () => client.logger.log("Bot is disconnecting...", "warn"))
.on("reconnecting", () => client.logger.log("Bot reconnecting...", "log"))
.on("error", (e) => client.logger.log(e, "error"))
.on("warn", (info) => client.logger.log(info, "warn"));
// if there is an unhandledRejection, log them
process.on("unhandledRejection", (err) => {
client.logger.log("Uncaught Promise Error: "+err, "error");
});