-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
124 lines (95 loc) · 3.33 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
const { Client, Partials, Collection, REST, Routes, ApplicationCommandType } = require("discord.js");
const { ClientToken, ClientID } = require("./configuration/config.json");
const { logger } = require("./configuration/common.js");
const { readdirSync } = require("fs");
const client = new Client({
intents: [
"Guilds",
"GuildMembers",
"GuildMessages",
"MessageContent",
"GuildPresences",
"GuildVoiceStates",
"DirectMessages",
],
partials: [
Partials.Channel,
Partials.Reaction,
Partials.User,
Partials.GuildMember,
Partials.Message,
]
});
client.commands = new Collection();
client.slashCommands = new Collection();
//stores models list rather than fetching everytime.
client.models = new Collection();
module.exports = client;
(async () => {
await loadCommands();
await loadEvents();
await loadSlashCommands();
})();
process.on("unhandledRejection", (reason, p) => {
console.log(reason, p)
});
process.on("uncaughtException", (err, origin) => {
console.log(err, origin)
});
client.login(ClientToken).catch((err) => {
logger("WARN", "Client", `${err}`);
});
async function loadEvents() {
console.log(`\n✎ ᴇᴠᴇɴᴛ-ʟᴏᴀᴅᴇʀ-ʀᴜɴɪɴɢ...`);
readdirSync("./events/").forEach(async (directory) => {
const events = readdirSync(`./events/${directory}`).filter((file) => file.endsWith(".js"));
events.forEach(file => {
const event = require(`./events/${directory}/${file}`);
if (event.once) {
client.once(event.name, async (...args) => {
event.callback(...args)
})
} else {
client.on(event.name, async (...args) => {
event.callback(...args)
})
}
logger("INFO", event.name, "Successfully Loaded.");
})
})
}
async function loadCommands() {
console.log(`\n✎ ᴍᴇssᴀɢᴇ-ᴄᴏᴍᴍᴀɴᴅ-ʟᴏᴀᴅᴇʀ-ʀᴜɴɪɴɢ...`);
readdirSync("./commands/").forEach(async (directory) => {
const commands = readdirSync(`./commands/${directory}`).filter((file) => file.endsWith(".js"));
commands.forEach((file) => {
const command = require(`./commands/${directory}/${file}`);
if (!command.data.name) return logger("WARN", file, `Missing command name.`);
client.commands.set(command.data.name, command);
logger("INFO", command.data.name, "Successfully Loaded.");
})
})
}
async function loadSlashCommands() {
console.log(`\n✎ sʟᴀsʜ-ᴄᴏᴍᴍᴀɴᴅ-ʟᴏᴀᴅᴇʀ-ʀᴜɴɪɴɢ...`);
if (!ClientID) {
return logger("WARN", "Client", `Client ID is missing in config file.`);
}
const slashArray = [];
readdirSync("./slashcommands/").forEach(async (directory) => {
const slashCommands = readdirSync(`./slashcommands/${directory}`).filter((file) => file.endsWith(".js"));
slashCommands.forEach(async (file) => {
const slash = require(`./slashcommands/${directory}/${file}`);
if (!slash.data.name) return logger("WARN", file, `Missing command name.`);
client.slashCommands.set(slash.data.name, slash);
slashArray.push(slash.data);
logger("INFO", slash.data.name, "Successfully Loaded.");
})
})
const rest = new REST({ version: '10' }).setToken(ClientToken);
try {
await rest.put(Routes.applicationCommands(ClientID), { body: slashArray })
} catch (error) {
console.error(error);
}
}