Skip to content

Commit 6596378

Browse files
committed
Added Handlers for all Interactions
1 parent 7e21eae commit 6596378

File tree

19 files changed

+234
-42
lines changed

19 files changed

+234
-42
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module.exports = {
33
'env': {
44
'node': true,
5-
'commonjs': true,
5+
'es6': true
66
},
77
'extends': 'eslint:recommended',
88
'overrides': [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discord.js-advanced-command-handler",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "🤖 Discord.js V14 Command and Event Handler",
55
"main": "src/index.js",
66
"type": "module",

src/commands/reload.js

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

src/events/interactionCreate.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,50 @@ export default {
22
event: 'interactionCreate',
33
async execute(client, interaction) {
44
try {
5-
if (!interaction.isCommand()) return;
6-
const command = client.slashCommands.get(interaction.commandName);
7-
if (!command) return;
8-
try {
9-
command.execute(client, interaction);
10-
} catch (error) {
11-
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
12-
console.log(error);
5+
if (interaction.isCommand()) {
6+
const command = client.slashCommands.get(interaction.commandName);
7+
if (!command) return;
8+
try {
9+
command.execute(client, interaction);
10+
} catch (error) {
11+
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
12+
console.log(error);
13+
}
1314
}
15+
16+
if (interaction.isModalSubmit()) {
17+
const modal = client.modals.get(interaction.customId);
18+
if (!modal) return;
19+
try {
20+
modal.execute(client, interaction);
21+
} catch (error) {
22+
interaction.reply({ content: 'There was an error while executing this modal!', ephemeral: true });
23+
console.log(error);
24+
}
25+
}
26+
27+
if (interaction.isButton()) {
28+
const button = client.buttons.get(interaction.customId);
29+
if (!button) return;
30+
try {
31+
button.execute(client, interaction);
32+
} catch (error) {
33+
interaction.reply({ content: 'There was an error while executing this button!', ephemeral: true });
34+
console.log(error);
35+
}
36+
}
37+
38+
if (interaction.isStringSelectMenu()) {
39+
const selectMenu = client.selectMenus.get(interaction.customId);
40+
if (!selectMenu) return;
41+
try {
42+
selectMenu.execute(client, interaction);
43+
} catch (error) {
44+
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
45+
console.log(error);
46+
}
47+
}
48+
1449
} catch (error) {
1550
return console.log(error);
1651
}

src/handlers/buttons.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readdirSync } from 'fs';
2+
import chalk from 'chalk';
3+
4+
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) {
14+
client.buttons.clear();
15+
const buttonFiles = readdirSync('./src/interactions/buttons').filter(file => file.endsWith('.js'));
16+
for (let i = 0; i < buttonFiles.length; i++) {
17+
const button = await import(`../interactions/buttons/${buttonFiles[i]}?${Date.now()}`);
18+
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))}`));
20+
}
21+
return buttonFiles.length;
22+
}
23+
24+
export default { loadButtons, reloadButtons };

src/handlers/contextMenus.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readdirSync } from 'fs';
2+
import chalk from 'chalk';
3+
4+
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) {
14+
client.contextMenus.clear();
15+
const contextMenus = readdirSync('./src/interactions/contextMenus').filter(file => file.endsWith('.js'));
16+
for (let i = 0; i < contextMenus.length; i++) {
17+
const menus = await import(`../interactions/contextMenus/${contextMenus[i]}?${Date.now()}`);
18+
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)}`));
20+
}
21+
return contextMenus.length;
22+
}
23+
24+
export default { loadContextMenus, reloadContextMenus };

src/handlers/modals.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readdirSync } from 'fs';
2+
import chalk from 'chalk';
3+
4+
async function loadModals(client) {
5+
const modalFiles = readdirSync('./src/interactions/modals').filter(file => file.endsWith('.js'));
6+
for (let i = 0; i < modalFiles.length; i++) {
7+
const modal = await import(`../interactions/modals/${modalFiles[i]}`);
8+
await client.modals.set(modal.default.id, modal.default);
9+
console.log(chalk.greenBright(`[MODAL] Loaded ${(chalk.yellow(modalFiles[i]))} with modal ${(chalk.yellow(modal.default.id))}`));
10+
}
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))}`));
20+
}
21+
return modalFiles.length;
22+
}
23+
24+
export default { loadModals, reloadModals };

src/handlers/register.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import config from '../data/config.js';
2+
import { REST, Routes } from 'discord.js';
3+
const rest = new REST({ version: '10' }).setToken(config.bot.token);
4+
5+
async function load(client) {
6+
const commands = new Map();
7+
for (const [key, value] of client.slashCommands) {
8+
commands.set(key, value);
9+
}
10+
for (const [key, value] of client.contextMenus) {
11+
commands.set(key, value);
12+
}
13+
14+
await rest.put(
15+
Routes.applicationCommands(client.user.id),
16+
{ body: [...commands.values()].map(command => command.data.toJSON()) },
17+
);
18+
}
19+
20+
export default { load };

src/handlers/selectMenus.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readdirSync } from 'fs';
2+
import chalk from 'chalk';
3+
4+
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) {
14+
client.selectMenus.clear();
15+
const selectMenuFiles = readdirSync('./src/interactions/selectMenus').filter(file => file.endsWith('.js'));
16+
for (let i = 0; i < selectMenuFiles.length; i++) {
17+
const selectMenu = await import(`../interactions/selectMenus/${selectMenuFiles[i]}?${Date.now()}`);
18+
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))}`));
20+
}
21+
return selectMenuFiles.length;
22+
}
23+
24+
export default { loadSelectMenus, reloadSelectMenus };

src/handlers/slashCommands.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
1-
import { REST, Routes } from 'discord.js';
21
import { readdirSync } from 'fs';
32
import chalk from 'chalk';
4-
import config from '../data/config.js';
5-
6-
const rest = new REST({ version: '10' }).setToken(config.bot.token);
73

84
async function loadSlashCommands(client) {
9-
const slashCommands = readdirSync('./src/commands').filter(file => file.endsWith('.js'));
5+
const slashCommands = readdirSync('./src/interactions/commands').filter(file => file.endsWith('.js'));
106
for (let i = 0; i < slashCommands.length; i++) {
11-
const command = await import(`../commands/${slashCommands[i]}?${Date.now()}`);
7+
const command = await import(`../interactions/commands/${slashCommands[i]}`);
128
client.slashCommands.set(command.default.data.toJSON().name, command.default);
139
console.log(chalk.greenBright(`[SLASHCOMMAND] Loaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(command.default.data.toJSON().name)}`));
1410
}
15-
rest.put(
16-
Routes.applicationCommands(client.user.id),
17-
{ body: client.slashCommands.map(cmd => cmd.data.toJSON()) },
18-
);
1911
}
2012

2113
async function reloadSlashCommands(client) {
2214
client.slashCommands.clear();
23-
const slashCommands = readdirSync('./src/commands').filter(file => file.endsWith('.js'));
15+
const slashCommands = readdirSync('./src/interactions/commands').filter(file => file.endsWith('.js'));
2416
for (let i = 0; i < slashCommands.length; i++) {
25-
const command = await import(`../commands/${slashCommands[i]}`);
17+
const command = await import(`../interactions/commands/${slashCommands[i]}?${Date.now()}`);
2618
client.slashCommands.set(command.default.data.toJSON().name, command.default);
2719
console.log(chalk.greenBright(`[SLASHCOMMAND] Reloaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(command.default.data.toJSON().name)}`));
2820
}
29-
rest.put(
30-
Routes.applicationCommands(client.user.id),
31-
{ body: client.slashCommands.map(cmd => cmd.data.toJSON()) },
32-
);
3321
return slashCommands.length;
3422
}
3523

0 commit comments

Comments
 (0)