-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (170 loc) · 7.44 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const fs = require('node:fs');
const path = require('node:path');
// Require the necessary discord.js classes
const { REST, Routes, GuildMember, ChannelType, PermissionsBitField, PermissionFlagsBits, SortOrderType } = require('discord.js');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { appID, guildID, token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [0b111111111111111111111] });
//------------------------- Registering Commands -------------------------
client.commands = new Collection();
const commands = [];
const commandsDev = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
const foldersPathDev = path.join(__dirname, 'commands_dev');
const commandFoldersDev = fs.readdirSync(foldersPathDev);
for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
let cmdLog = `Registred: ${file} as ${command.data.name}`;
for (let alias of command.aliases) {
client.commands.set(alias, command);
cmdLog += `, ${alias}`;
}
console.log(cmdLog);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
//DEBUG
//console.log(`${command.data} / ${command.execute}`);
//console.log(`${commandsPath} / ${filePath}`);
//console.log(`${command} if: ${'data' in command} ${'execute' in command}`)
}
}
}
// These commands will deploy only on selected server
for (const folder of commandFoldersDev) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPathDev, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commandsDev.push(command.data.toJSON());
client.commands.set(command.data.name, command);
let cmdLog = `Registred for development: ${file} as ${command.data.name}`;
for (let alias of command.aliases) {
client.commands.set(alias, command);
cmdLog += `, ${alias}`;
}
console.log(cmdLog);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
//DEBUG
//console.log(`${command.data} / ${command.execute}`);
//console.log(`${commandsPath} / ${filePath}`);
//console.log(`${command} if: ${'data' in command} ${'execute' in command}`)
}
}
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);
// and deploy your commands!
(async () => {
try {
// Dev Deployment
console.log(`Started refreshing ${commandsDev.length} application (/) commands in development.`);
// The put method is used to fully refresh all commands in the guild with the current set
const dataDev = await rest.put(
Routes.applicationGuildCommands(appID, guildID),
{ body: commandsDev },
);
console.log(`Successfully reloaded ${dataDev.length} application (/) commands in development.`);
// All Server Deployment
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(appID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
//----------------------- END Registering Commands -----------------------
//----------------------- Registering Events -----------------------
const eventsPath = path.join(__dirname, 'event_handlers');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
console.log(`Registering ${event.name} event.`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
//----------------------- END Registering Events -----------------------
// Slash Command Handling
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) console.log(interaction.command);
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction, interaction.commandName);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
client.on(Events.GuildCreate, async test => {
});
// When the client is ready, run this code (only once).
// The distinction between `client: Client<boolean>` and `readyClient: Client<true>` is important for TypeScript developers.
// It makes some properties non-nullable.
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
// Message and commands handling
client.on(Events.MessageCreate, async message => {
// Command conditions
if (message.author.bot) return;
let path = `servers/${message.guild.id}`;
let file = fs.readFileSync(`${path}/config.json`);
let config = JSON.parse(file);
let x = config.prefix;
if (!message.content.startsWith(x)) return;
//console.log(message.member.permissions);
let args = message.content.slice(x.length).toLowerCase().split(' ');
const command = message.client.commands.get(args[0]);
if (!command) {
console.error(`No command matching ${args[0]} was found.`);
return;
}
/*if (!message.member.permissions.has(command.default_memeber_permissions)) {
message.reply(`You dont have enough permissions!`)
}*/
// Command Execution
try {
await command.execute(message, args);
} catch (error) {
console.error(error);
if (message.replied || message.deferred) {
await message.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await message.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
// Log in to Discord with your client's token
client.login(token);