Skip to content

Commit 74d2dab

Browse files
committed
Improved Code
1 parent d0b80c6 commit 74d2dab

37 files changed

+178
-569
lines changed

README.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Discord.js-Advanced-Command-Handler
2-
32
Simple and easy to use Slash Command Handler for Discord.js v14.
43

54
## Prerequisites
@@ -13,20 +12,8 @@ Simple and easy to use Slash Command Handler for Discord.js v14.
1312
5. Enjoy!
1413

1514
## Categories
16-
Category folders are optional, but you cant have a subfolder in a category.
17-
```
18-
├───interactions
19-
│ ├───slashCommands
20-
│ │ ├───example.js
21-
│ │ └───category
22-
│ │ └───example.js
23-
│ ├───selectMenus
24-
...
25-
```
26-
27-
## Select Menus
28-
In order to use select menus you can either put them all in ``/interactions/selectMenus`` or divided by their category in their according folder.
29-
Example: A user select menu would be in ``/interactions/userSelectMenus/example.js``
15+
You can create as many subfolders as you want, and each of these folders can have an unlimited number of nested subfolders.
16+
You only need to specify the interaction like the already existing ones in the ``/interactions`` folder.
3017

3118
## Contributing
3219
Just make a pull request and I will review it.

src/events/interactionCreate.js

Lines changed: 21 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,116 +3,29 @@ export default {
33
async execute(client, interaction) {
44
try {
55

6-
if (interaction.isUserContextMenuCommand() === true) {
7-
const contextMenu = client.contextMenus.get(interaction.commandName);
8-
if (!contextMenu) return;
9-
try {
10-
contextMenu.execute(client, interaction);
11-
} catch (error) {
12-
interaction.reply({ content: 'There was an error while executing this context menu!', ephemeral: true });
13-
console.log(error);
14-
}
6+
const interactionType = (() => {
7+
if (interaction.isContextMenuCommand()) return 'contextMenu';
8+
if (interaction.isMessageContextMenuCommand()) return 'messageContextMenu';
9+
if (interaction.isUserContextMenuCommand()) return 'userContextMenu';
10+
if (interaction.isCommand()) return 'slashCommand';
11+
if (interaction.isModalSubmit()) return 'modal';
12+
if (interaction.isButton()) return 'button';
13+
if (interaction.isStringSelectMenu()) return 'stringSelectMenu';
14+
if (interaction.isChannelSelectMenu()) return 'channelSelectMenu';
15+
if (interaction.isMentionableSelectMenu()) return 'mentionableSelectMenu';
16+
if (interaction.isRoleSelectMenu()) return 'roleSelectMenu';
17+
if (interaction.isUserSelectMenu()) return 'userSelectMenu';
18+
})();
19+
20+
let action = client.interaction.get(`${interactionType}-${interaction.customId || interaction.commandName}`);
21+
if (!action) return;
22+
try {
23+
action.execute(client, interaction);
24+
} catch (error) {
25+
interaction.reply({ content: 'There was an error while executing this context menu!', ephemeral: true });
26+
console.log(error);
1527
}
1628

17-
if (interaction.isCommand()) {
18-
const command = client.slashCommands.get(interaction.commandName);
19-
if (!command) return;
20-
try {
21-
command.execute(client, interaction);
22-
} catch (error) {
23-
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
24-
console.log(error);
25-
}
26-
}
27-
28-
if (interaction.isModalSubmit()) {
29-
const modal = client.modals.get(interaction.customId);
30-
if (!modal) return;
31-
try {
32-
modal.execute(client, interaction);
33-
} catch (error) {
34-
interaction.reply({ content: 'There was an error while executing this modal!', ephemeral: true });
35-
console.log(error);
36-
}
37-
}
38-
39-
if (interaction.isButton()) {
40-
const button = client.buttons.get(interaction.customId);
41-
if (!button) return;
42-
try {
43-
button.execute(client, interaction);
44-
} catch (error) {
45-
interaction.reply({ content: 'There was an error while executing this button!', ephemeral: true });
46-
console.log(error);
47-
}
48-
}
49-
50-
if (interaction.isAnySelectMenu()) {
51-
const selectMenu = client.selectMenus.get(interaction.customId);
52-
if (!selectMenu) return;
53-
try {
54-
selectMenu.execute(client, interaction);
55-
} catch (error) {
56-
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
57-
console.log(error);
58-
}
59-
}
60-
61-
if (interaction.isStringSelectMenu()) {
62-
const stringSelectMenu = client.stringSelectMenus.get(interaction.customId);
63-
if (!stringSelectMenu) return;
64-
try {
65-
stringSelectMenu.execute(client, interaction);
66-
} catch (error) {
67-
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
68-
console.log(error);
69-
}
70-
}
71-
72-
if (interaction.isChannelSelectMenu()) {
73-
const channelSelectMenu = client.channelSelectMenus.get(interaction.customId);
74-
if (!channelSelectMenu) return;
75-
try {
76-
channelSelectMenu.execute(client, interaction);
77-
} catch (error) {
78-
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
79-
console.log(error);
80-
}
81-
}
82-
83-
if (interaction.isMentionableSelectMenu()) {
84-
const mentionableSelectMenu = client.mentionableSelectMenus.get(interaction.customId);
85-
if (!mentionableSelectMenu) return;
86-
try {
87-
mentionableSelectMenu.execute(client, interaction);
88-
} catch (error) {
89-
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
90-
console.log(error);
91-
}
92-
}
93-
94-
if (interaction.isRoleSelectMenu()) {
95-
const roleSelectMenu = client.roleSelectMenus.get(interaction.customId);
96-
if (!roleSelectMenu) return;
97-
try {
98-
roleSelectMenu.execute(client, interaction);
99-
} catch (error) {
100-
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
101-
console.log(error);
102-
}
103-
}
104-
105-
if (interaction.isUserSelectMenu()) {
106-
const userSelectMenu = client.userSelectMenus.get(interaction.customId);
107-
if (!userSelectMenu) return;
108-
try {
109-
userSelectMenu.execute(client, interaction);
110-
} catch (error) {
111-
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
112-
console.log(error);
113-
}
114-
}
115-
11629
} catch (error) {
11730
return console.log(error);
11831
}

src/events/messageCreate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88

99
if (message.content.startsWith(config.bot.prefix)) {
1010
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]));
11+
const command = client.interaction.get(`messageCommand-${args[0]}`);
1212
if (!command) return;
1313
try {
1414
command.execute(client, message, args);

src/handlers/application.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import config from '../data/config.js';
2+
import chalk from 'chalk';
3+
import { REST, Routes } from 'discord.js';
4+
const rest = new REST({ version: '10' }).setToken(config.bot.token);
5+
6+
async function registerApplicationCommands(client) {
7+
try {
8+
let commands = new Map();
9+
for (const [key, value] of client.interaction) {
10+
if (key.startsWith('slashCommand-') || key.startsWith('contextMenu-')) commands.set(key, value);
11+
}
12+
13+
await rest.put(
14+
Routes.applicationCommands(client.user.id),
15+
{ body: [...commands.values()].map(command => command.data.toJSON()) },
16+
);
17+
console.log(chalk.greenBright('[APPLICATION] Successfully registered application commands.'));
18+
} catch (err) {
19+
console.error(err);
20+
}
21+
}
22+
23+
export default registerApplicationCommands;

src/handlers/buttons.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/handlers/channelSelectMenus.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/handlers/contextMenus.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/handlers/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ async function loadEvents(client) {
1111
}
1212
}
1313

14-
export default { loadEvents };
14+
export default loadEvents;

src/handlers/handler.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import fs from 'fs';
2+
import chalk from 'chalk';
3+
4+
async function loadInteractions(folderPath, client) {
5+
try {
6+
let files = fs.readdirSync(folderPath);
7+
8+
for (let i = 0; i < files.length; i++) {
9+
let file = files[i];
10+
let filePath = folderPath + '/' + file;
11+
let stats = fs.statSync(filePath);
12+
13+
if (stats.isDirectory()) {
14+
await loadInteractions(filePath, client);
15+
} else {
16+
let interactionPath = '.' + filePath;
17+
let interaction = await import(`../${interactionPath}?${Date.now()}`);
18+
19+
if (interaction.default.disabled === true) return;
20+
if (interaction.default.type === 'messageCommand') {
21+
client.interaction.set(`messageCommand-${interaction.default.id}`, interaction.default);
22+
console.log(chalk.greenBright(`[${interaction.default.type.toUpperCase()}] Successfully Loaded ${(chalk.yellow(file))} ${(chalk.yellow(interaction.default.id))}`));
23+
if (interaction.default.aliases) {
24+
for (let i = 0; i < interaction.default.aliases.length; i++) {
25+
client.interaction.set(`messageCommand-${interaction.default.aliases[i]}`, interaction.default);
26+
}
27+
}
28+
} else {
29+
client.interaction.set(`${interaction.default.type}-${interaction.default.id}`, interaction.default);
30+
console.log(chalk.greenBright(`[${interaction.default.type.toUpperCase()}] Successfully Loaded ${(chalk.yellow(file))} ${(chalk.yellow(interaction.default.id))}`));
31+
}
32+
}
33+
}
34+
} catch (err) {
35+
console.error(err);
36+
}
37+
}
38+
39+
export default loadInteractions;

src/handlers/mentionableSelectMenus.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)