-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard.js
158 lines (121 loc) · 6.95 KB
/
shard.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Packages & Helpful Tools
const { Client, GatewayIntentBits, Collection, Events } = require("discord.js");
const { readdir } = require('node:fs');
const Logger = require('terminal.xr');
const { Axios } = require("axios");
const wait = require('delay');
const { botId } = require('./config');
require('dotenv/config');
//Setup
const client = new Client({
intents: [
GatewayIntentBits.Guilds
]
});
const logger = new Logger();
const discordAPI = new Axios({
baseURL: 'https://discord.com/api/v10',
headers: {
'Content-Type': 'application/json',
Authorization: `Bot ${process.env.BOT_TOKEN}`
}
});
//Details
var shard = client.shard.ids[0];
//Interaction Loader
client.interactions = {
commands: new Collection(),
guildCommands: {}
};
readdir('./interactions/slash', (error, files = []) => { //Slash Commands
if (error) logger.error(`[Shard ${shard}] [InterationLoader/SlashCommands] ${error.stack ?? error}`);
if (files.length > 0) logger.info(`[Shard ${shard}] [InterationLoader/SlashCommands] Loading ${files.length} slash commands...`);
else logger.warn(`[Shard ${shard}] [InterationLoader/SlashCommands] Your bot has not any slash commands`);
files.forEach(file => {
try {
const command = require(`./interactions/slash/${file}`);
if (command.guild) {
if (client.interactions.guildCommands[command.guild]) client.interactions.guildCommands[command.guild].set(command.data.name, command);
else client.interactions.guildCommands[command.guild] = new Collection().set(command.data.name, command);
} else client.interactions.commands.set(command.data.name, command);
logger.success(`[Shard ${shard}] [InterationLoader/SlashCommands] ${command.data.name} slash command loaded`);
} catch (error) {
logger.error(`[Shard ${shard}] [InterationLoader/SlashCommands/${file}] ${error.stack ?? error}`);
};
});
});
readdir('./interactions/user', (error, files = []) => { //User Commands
if (error) logger.error(`[Shard ${shard}] [InterationLoader/UserCommands] ${error.stack ?? error}`);
if (files.length > 0) logger.info(`[Shard ${shard}] [InterationLoader/UserCommands] Loading ${files.length} user commands...`);
else logger.warn(`[Shard ${shard}] [InterationLoader/UserCommands] Your bot has not any user commands`);
files.forEach(file => {
try {
const command = require(`./interactions/user/${file}`);
if (command.guild) {
if (client.interactions.guildCommands[command.guild]) client.interactions.guildCommands[command.guild].set(command.data.name, command);
else client.interactions.guildCommands[command.guild] = new Collection().set(command.data.name, command);
} else client.interactions.commands.set(command.data.name, command);
logger.success(`[Shard ${shard}] [InterationLoader/UserCommands] ${command.data.name} user command loaded`);
} catch (error) {
logger.error(`[Shard ${shard}] [InterationLoader/UserCommands/${file}] ${error.stack ?? error}`);
};
});
});
readdir('./interactions/message', (error, files = []) => { //Message Commands
if (error) logger.error(`[Shard ${shard}] [InterationLoader/MessageCommands] ${error.stack ?? error}`);
if (files.length > 0) logger.info(`[Shard ${shard}] [InterationLoader/MessageCommands] Loading ${files.length} message commands...`);
else logger.warn(`[Shard ${shard}] [InterationLoader/MessageCommands] Your bot has not any message commands`);
files.forEach(file => {
try {
const command = require(`./interactions/message/${file}`);
if (command.guild) {
if (client.interactions.guildCommands[command.guild]) client.interactions.guildCommands[command.guild].set(command.data.name, command);
else client.interactions.guildCommands[command.guild] = new Collection().set(command.data.name, command);
} else client.interactions.commands.set(command.data.name, command);
logger.success(`[Shard ${shard}] [InterationLoader/MessageCommands] ${command.data.name} message command loaded`);
} catch (error) {
logger.error(`[Shard ${shard}] [InterationLoader/MessageCommands/${file}] ${error.stack ?? error}`);
};
});
});
//Command Installer
if (shard === 0) (async () => {
logger.status('[CommandInstaller] Waiting for loading commands...');
await wait(5000); //Wait 5 seconds for loading commands
var commands = client.interactions.commands;
var guildCommands = client.interactions.guildCommands;
logger.info(`[CommandInstaller] Installing ${commands.size} commands...`);
discordAPI.put(`applications/${botId}/commands`, JSON.stringify(commands.map(command => command.data.toJSON())))
.then(() => logger.success('[CommandInstaller] All commands are registered'))
//.then(res => logger.debug(`[CommandInstaller] ${JSON.stringify(res.data)}`)) //For Debugging
.catch(error => logger.error(`[CommandInstaller] ${error.stack ?? error}`));
for (var guild in client.interactions.guildCommands) {
logger.info(`[CommandInstaller/Guild_${guild}] Registering ${commands.size} commands...`)
discordAPI.put(`applications/${botId}/guilds/${guild}/commands`, JSON.stringify(guildCommands[guild].map(command => command.data.toJSON())))
.then(() => logger.success(`[CommandInstaller/Guild_${guild}] All commands are registered`))
//.then(res => logger.debug(`[CommandInstaller/Guild_${guild}] ${JSON.stringify(res.data)}`)) //For Debugging
.catch(error => logger.error(`[CommandInstaller/Guild_${guild}] ${error.stack ?? error}`));
await wait(3000); //Wait 3 seconds each guild for rate limits
};
})().then(() => client.login(process.env.BOT_TOKEN));
//Events
client
.on(Events.InteractionCreate, async interaction => {
if (interaction.isCommand()) {
let name = interaction.commandName.split(' ')[0];
let guild = interaction.commandGuildId;
let command;
if (guild) command = client.interactions.guildCommands[guild].get(name);
else command = client.interactions.commands.get(name);
if (command) command.execute(interaction, client);
else logger.error(`[Shard ${shard}] [Events/InteractionCreate/Command] ${name} command could not found`);
} else if (interaction.isAutocomplete()) {
let name = interaction.commandName.split(' ')[0];
let guild = interaction.commandGuildId;
let command;
if (guild) command = client.interactions.guildCommands[guild].get(name);
else command = client.interactions.commands.get(name);
if (command) command.autocomplete(interaction, client);
else logger.error(`[Shard ${shard}] [Events/InteractionCreate/Autocomplete] ${name} command could not found`);
};
});