Skip to content

Commit b779308

Browse files
committed
rewrite handlers
1 parent 84fad99 commit b779308

File tree

18 files changed

+151
-64
lines changed

18 files changed

+151
-64
lines changed

src/events/messageCreate.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
import config from '../data/config.js';
2+
13
export default {
24
event: 'messageCreate',
35
async execute(client, message) {
46
try {
57
if (message.author.bot) return;
68

9+
if (message.content.startsWith(config.bot.prefix)) {
10+
const args = message.content.slice(config.bot.prefix.length).trim().split(/ +/);
11+
const command = client.messageCommands.get(args[0]) || client.messageCommands.find(cmd => cmd.aliases && cmd.aliases.includes(args[0]));
12+
if (!command) return;
13+
try {
14+
command.execute(client, message, args);
15+
} catch (error) {
16+
message.reply('There was an error trying to execute that command!');
17+
console.log(error);
18+
}
19+
}
20+
721
} catch (error) {
822
console.log(error);
923
}

src/handlers/buttons.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import { readdirSync } from 'fs';
22
import chalk from 'chalk';
33

44
async function loadButtons(client) {
5-
const buttonFiles = readdirSync('./src/interactions/buttons').filter(file => file.endsWith('.js'));
6-
for (let i = 0; i < buttonFiles.length; i++) {
7-
const button = await import(`../interactions/buttons/${buttonFiles[i]}`);
8-
await client.buttons.set(button.default.id, button.default);
9-
console.log(chalk.greenBright(`[BUTTON] Loaded ${(chalk.yellow(buttonFiles[i]))} with button ${(chalk.yellow(button.default.id))}`));
10-
}
11-
}
12-
13-
async function reloadButtons(client) {
145
client.buttons.clear();
156
const buttonFiles = readdirSync('./src/interactions/buttons').filter(file => file.endsWith('.js'));
167
for (let i = 0; i < buttonFiles.length; i++) {
178
const button = await import(`../interactions/buttons/${buttonFiles[i]}?${Date.now()}`);
189
await client.buttons.set(button.default.id, button.default);
19-
console.log(chalk.greenBright(`[BUTTON] Reloaded ${(chalk.yellow(buttonFiles[i]))} with button ${(chalk.yellow(button.default.id))}`));
10+
console.log(chalk.greenBright(`[BUTTON] Loaded ${(chalk.yellow(buttonFiles[i]))} with button ${(chalk.yellow(button.default.id))}`));
11+
}
12+
const buttonFolders = readdirSync('./src/interactions/buttons', { withFileTypes: true }).filter(file => file.isDirectory());
13+
for (let i = 0; i < buttonFolders.length; i++) {
14+
const buttonFiles = readdirSync(`./src/interactions/buttons/${buttonFolders[i].name}`).filter(file => file.endsWith('.js'));
15+
for (let j = 0; j < buttonFiles.length; j++) {
16+
const button = await import(`../interactions/buttons/${buttonFolders[i].name}/${buttonFiles[j]}`);
17+
await client.buttons.set(button.default.id, button.default);
18+
console.log(chalk.greenBright(`[BUTTON] Loaded ${(chalk.yellow(buttonFiles[j]))} with button ${(chalk.yellow(button.default.id))}`));
19+
}
2020
}
21-
return buttonFiles.length;
2221
}
2322

24-
export default { loadButtons, reloadButtons };
23+
export default { loadButtons };

src/handlers/contextMenus.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import { readdirSync } from 'fs';
22
import chalk from 'chalk';
33

44
async function loadContextMenus(client) {
5-
const contextMenus = readdirSync('./src/interactions/contextMenus').filter(file => file.endsWith('.js'));
6-
for (let i = 0; i < contextMenus.length; i++) {
7-
const menus = await import(`../interactions/contextMenus/${contextMenus[i]}`);
8-
client.contextMenus.set(menus.default.data.toJSON().name, menus.default);
9-
console.log(chalk.greenBright(`[CONTEXTMENU] Loaded ${chalk.yellow(contextMenus[i])} with command ${chalk.yellow(menus.default.data.toJSON().name)}`));
10-
}
11-
}
12-
13-
async function reloadContextMenus(client) {
145
client.contextMenus.clear();
156
const contextMenus = readdirSync('./src/interactions/contextMenus').filter(file => file.endsWith('.js'));
167
for (let i = 0; i < contextMenus.length; i++) {
178
const menus = await import(`../interactions/contextMenus/${contextMenus[i]}?${Date.now()}`);
189
client.contextMenus.set(menus.default.data.toJSON().name, menus.default);
19-
console.log(chalk.greenBright(`[CONTEXTMENU] Reloaded ${chalk.yellow(contextMenus[i])} with command ${chalk.yellow(menus.default.data.toJSON().name)}`));
10+
console.log(chalk.greenBright(`[CONTEXTMENU] Loaded ${chalk.yellow(contextMenus[i])} with command ${chalk.yellow(menus.default.data.toJSON().name)}`));
11+
}
12+
const contextMenuFolders = readdirSync('./src/interactions/contextMenus', { withFileTypes: true }).filter(file => file.isDirectory());
13+
for (let i = 0; i < contextMenuFolders.length; i++) {
14+
const contextMenus = readdirSync(`./src/interactions/contextMenus/${contextMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
15+
for (let j = 0; j < contextMenus.length; j++) {
16+
const menus = await import(`../interactions/contextMenus/${contextMenuFolders[i].name}/${contextMenus[j]}`);
17+
client.contextMenus.set(menus.default.data.toJSON().name, menus.default);
18+
console.log(chalk.greenBright(`[CONTEXTMENU] Loaded ${chalk.yellow(contextMenus[j])} with command ${chalk.yellow(menus.default.data.toJSON().name)}`));
19+
}
2020
}
21-
return contextMenus.length;
2221
}
2322

24-
export default { loadContextMenus, reloadContextMenus };
23+
export default { loadContextMenus };

src/handlers/messageCommands.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import chalk from 'chalk';
2+
import { readdirSync } from 'fs';
3+
4+
async function loadMessageCommands(client) {
5+
client.messageCommands.clear();
6+
client.messageCommandsAliases.clear();
7+
const commandFiles = readdirSync('./src/interactions/messageCommands').filter(file => file.endsWith('.js'));
8+
for (let i = 0; i < commandFiles.length; i++) {
9+
const command = await import(`../interactions/messageCommands/${commandFiles[i]}?${Date.now()}`);
10+
if (command.default.aliases) {
11+
command.default.aliases.forEach(alias => {
12+
client.messageCommandsAliases.set(alias, command.default);
13+
});
14+
}
15+
client.messageCommands.set(command.default.name, command.default);
16+
console.log(chalk.greenBright(`[COMMAND] Loaded ${(chalk.yellow(commandFiles[i]))} with command ${(chalk.yellow(command.default.name))}`));
17+
}
18+
const commandFolders = readdirSync('./src/interactions/messageCommands', { withFileTypes: true }).filter(file => file.isDirectory());
19+
for (let i = 0; i < commandFolders.length; i++) {
20+
const commandFiles = readdirSync(`./src/interactions/messageCommands/${commandFolders[i].name}`).filter(file => file.endsWith('.js'));
21+
for (let j = 0; j < commandFiles.length; j++) {
22+
const command = await import(`../interactions/messageCommands/${commandFolders[i].name}/${commandFiles[j]}?${Date.now()}`);
23+
if (command.default.aliases) {
24+
command.default.aliases.forEach(alias => {
25+
client.messageCommandsAliases.set(alias, command.default);
26+
});
27+
}
28+
client.messageCommands.set(command.default.name, command.default);
29+
console.log(chalk.greenBright(`[COMMAND] Loaded ${(chalk.yellow(commandFiles[j]))} with command ${(chalk.yellow(command.default.name))}`));
30+
}
31+
}
32+
}
33+
34+
export default { loadMessageCommands };

src/handlers/modals.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import { readdirSync } from 'fs';
22
import chalk from 'chalk';
33

44
async function loadModals(client) {
5+
client.modals.clear();
56
const modalFiles = readdirSync('./src/interactions/modals').filter(file => file.endsWith('.js'));
67
for (let i = 0; i < modalFiles.length; i++) {
7-
const modal = await import(`../interactions/modals/${modalFiles[i]}`);
8+
const modal = await import(`../interactions/modals/${modalFiles[i]}?${Date.now()}`);
89
await client.modals.set(modal.default.id, modal.default);
910
console.log(chalk.greenBright(`[MODAL] Loaded ${(chalk.yellow(modalFiles[i]))} with modal ${(chalk.yellow(modal.default.id))}`));
1011
}
11-
}
12-
13-
async function reloadModals(client) {
14-
client.modals.clear();
15-
const modalFiles = readdirSync('./src/interactions/modals').filter(file => file.endsWith('.js'));
16-
for (let i = 0; i < client.modals.size; i++) {
17-
const modal = await import(`../interactions/modals/${modalFiles[i]}?${Date.now()}`);
18-
await client.modals.set(modal.default.id, modal.default);
19-
console.log(chalk.greenBright(`[MODAL] Reloaded ${(chalk.yellow(modalFiles[i]))} with modal ${(chalk.yellow(modal.default.id))}`));
12+
const modalFolders = readdirSync('./src/interactions/modals', { withFileTypes: true }).filter(file => file.isDirectory());
13+
for (let i = 0; i < modalFolders.length; i++) {
14+
const modalFiles = readdirSync(`./src/interactions/modals/${modalFolders[i].name}`).filter(file => file.endsWith('.js'));
15+
for (let j = 0; j < modalFiles.length; j++) {
16+
const modal = await import(`../interactions/modals/${modalFolders[i].name}/${modalFiles[j]}?${Date.now()}`);
17+
await client.modals.set(modal.default.id, modal.default);
18+
console.log(chalk.greenBright(`[MODAL] Loaded ${(chalk.yellow(modalFiles[j]))} with modal ${(chalk.yellow(modal.default.id))}`));
19+
}
2020
}
21-
return modalFiles.length;
2221
}
2322

24-
export default { loadModals, reloadModals };
23+
export default { loadModals };

src/handlers/selectMenus.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import { readdirSync } from 'fs';
22
import chalk from 'chalk';
33

44
async function loadSelectMenus(client) {
5-
const selectMenuFiles = readdirSync('./src/interactions/selectMenus').filter(file => file.endsWith('.js'));
6-
for (let i = 0; i < selectMenuFiles.length; i++) {
7-
const selectMenu = await import(`../interactions/selectMenus/${selectMenuFiles[i]}`);
8-
await client.selectMenus.set(selectMenu.default.id, selectMenu.default);
9-
console.log(chalk.greenBright(`[SELECTMENU] Loaded ${(chalk.yellow(selectMenuFiles[i]))} with selectMenu ${(chalk.yellow(selectMenu.default.id))}`));
10-
}
11-
}
12-
13-
async function reloadSelectMenus(client) {
145
client.selectMenus.clear();
156
const selectMenuFiles = readdirSync('./src/interactions/selectMenus').filter(file => file.endsWith('.js'));
167
for (let i = 0; i < selectMenuFiles.length; i++) {
178
const selectMenu = await import(`../interactions/selectMenus/${selectMenuFiles[i]}?${Date.now()}`);
189
await client.selectMenus.set(selectMenu.default.id, selectMenu.default);
19-
console.log(chalk.greenBright(`[SELECTMENU] Reloaded ${(chalk.yellow(selectMenuFiles[i]))} with selectMenu ${(chalk.yellow(selectMenu.default.id))}`));
10+
console.log(chalk.greenBright(`[SELECTMENU] Loaded ${(chalk.yellow(selectMenuFiles[i]))} with selectMenu ${(chalk.yellow(selectMenu.default.id))}`));
11+
}
12+
const selectMenuFolders = readdirSync('./src/interactions/selectMenus', { withFileTypes: true }).filter(file => file.isDirectory());
13+
for (let i = 0; i < selectMenuFolders.length; i++) {
14+
const selectMenuFiles = readdirSync(`./src/interactions/selectMenus/${selectMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
15+
for (let j = 0; j < selectMenuFiles.length; j++) {
16+
const selectMenu = await import(`../interactions/selectMenus/${selectMenuFolders[i].name}/${selectMenuFiles[j]}?${Date.now()}`);
17+
await client.selectMenus.set(selectMenu.default.id, selectMenu.default);
18+
console.log(chalk.greenBright(`[SELECTMENU] Loaded ${(chalk.yellow(selectMenuFiles[j]))} with selectMenu ${(chalk.yellow(selectMenu.default.id))}`));
19+
}
2020
}
21-
return selectMenuFiles.length;
2221
}
2322

24-
export default { loadSelectMenus, reloadSelectMenus };
23+
export default { loadSelectMenus };

src/handlers/slashCommands.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import { readdirSync } from 'fs';
22
import chalk from 'chalk';
33

44
async function loadSlashCommands(client) {
5-
const slashCommands = readdirSync('./src/interactions/commands').filter(file => file.endsWith('.js'));
6-
for (let i = 0; i < slashCommands.length; i++) {
7-
const command = await import(`../interactions/commands/${slashCommands[i]}`);
8-
client.slashCommands.set(command.default.data.toJSON().name, command.default);
9-
console.log(chalk.greenBright(`[SLASHCOMMAND] Loaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(command.default.data.toJSON().name)}`));
10-
}
11-
}
12-
13-
async function reloadSlashCommands(client) {
145
client.slashCommands.clear();
15-
const slashCommands = readdirSync('./src/interactions/commands').filter(file => file.endsWith('.js'));
6+
const slashCommands = readdirSync('./src/interactions/slashCommands').filter(file => file.endsWith('.js'));
167
for (let i = 0; i < slashCommands.length; i++) {
17-
const command = await import(`../interactions/commands/${slashCommands[i]}?${Date.now()}`);
18-
client.slashCommands.set(command.default.data.toJSON().name, command.default);
19-
console.log(chalk.greenBright(`[SLASHCOMMAND] Reloaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(command.default.data.toJSON().name)}`));
8+
const commands = await import(`../interactions/slashCommands/${slashCommands[i]}?${Date.now()}`);
9+
client.slashCommands.set(commands.default.data.toJSON().name, commands.default);
10+
console.log(chalk.greenBright(`[SLASHCOMMAND] Loaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(commands.default.data.toJSON().name)}`));
11+
}
12+
const slashCommandFolders = readdirSync('./src/interactions/slashCommands', { withFileTypes: true }).filter(file => file.isDirectory());
13+
for (let i = 0; i < slashCommandFolders.length; i++) {
14+
const slashCommands = readdirSync(`./src/interactions/slashCommands/${slashCommandFolders[i].name}`).filter(file => file.endsWith('.js'));
15+
for (let j = 0; j < slashCommands.length; j++) {
16+
const commands = await import(`../interactions/slashCommands/${slashCommandFolders[i].name}/${slashCommands[j]}`);
17+
client.slashCommands.set(commands.default.data.toJSON().name, commands.default);
18+
console.log(chalk.greenBright(`[SLASHCOMMAND] Loaded ${chalk.yellow(slashCommands[j])} with command ${chalk.yellow(commands.default.data.toJSON().name)}`));
19+
}
2020
}
21-
return slashCommands.length;
2221
}
2322

24-
export default { loadSlashCommands, reloadSlashCommands };
23+
export default { loadSlashCommands };

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Discord from 'discord.js';
22
import config from './data/config.js';
33

44
import eventHandler from './handlers/events.js';
5+
import messageCommandHandler from './handlers/messageCommands.js';
56
import slashCommandHandler from './handlers/slashCommands.js';
67
import modalHandler from './handlers/modals.js';
78
import buttonHandler from './handlers/buttons.js';
@@ -27,13 +28,16 @@ const client = new Discord.Client({
2728

2829
await client.login(config.bot.token);
2930

31+
client.messageCommands = new Discord.Collection();
32+
client.messageCommandsAliases = new Discord.Collection();
3033
client.slashCommands = new Discord.Collection();
3134
client.modals = new Discord.Collection();
3235
client.buttons = new Discord.Collection();
3336
client.selectMenus = new Discord.Collection();
3437
client.contextMenus = new Discord.Collection();
3538

3639
await eventHandler.loadEvents(client);
40+
await messageCommandHandler.loadMessageCommands(client);
3741
await slashCommandHandler.loadSlashCommands(client);
3842
await modalHandler.loadModals(client);
3943
await buttonHandler.loadButtons(client);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
id: 'test',
3+
async execute(client, interaction) {
4+
interaction.reply('Hello World! Button');
5+
}
6+
};

src/interactions/buttons/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default {
2-
id: 'test',
2+
id: 'testg',
33
async execute(client, interaction) {
44
interaction.reply('Hello World! Button');
55
}

0 commit comments

Comments
 (0)