Skip to content

Commit

Permalink
Improved Code
Browse files Browse the repository at this point in the history
  • Loading branch information
MastiDev committed Jun 4, 2023
1 parent d0b80c6 commit 74d2dab
Show file tree
Hide file tree
Showing 37 changed files with 178 additions and 569 deletions.
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Discord.js-Advanced-Command-Handler

Simple and easy to use Slash Command Handler for Discord.js v14.

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

## Categories
Category folders are optional, but you cant have a subfolder in a category.
```
├───interactions
│ ├───slashCommands
│ │ ├───example.js
│ │ └───category
│ │ └───example.js
│ ├───selectMenus
...
```

## Select Menus
In order to use select menus you can either put them all in ``/interactions/selectMenus`` or divided by their category in their according folder.
Example: A user select menu would be in ``/interactions/userSelectMenus/example.js``
You can create as many subfolders as you want, and each of these folders can have an unlimited number of nested subfolders.
You only need to specify the interaction like the already existing ones in the ``/interactions`` folder.

## Contributing
Just make a pull request and I will review it.
Expand Down
129 changes: 21 additions & 108 deletions src/events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,116 +3,29 @@ export default {
async execute(client, interaction) {
try {

if (interaction.isUserContextMenuCommand() === true) {
const contextMenu = client.contextMenus.get(interaction.commandName);
if (!contextMenu) return;
try {
contextMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this context menu!', ephemeral: true });
console.log(error);
}
const interactionType = (() => {
if (interaction.isContextMenuCommand()) return 'contextMenu';
if (interaction.isMessageContextMenuCommand()) return 'messageContextMenu';
if (interaction.isUserContextMenuCommand()) return 'userContextMenu';
if (interaction.isCommand()) return 'slashCommand';
if (interaction.isModalSubmit()) return 'modal';
if (interaction.isButton()) return 'button';
if (interaction.isStringSelectMenu()) return 'stringSelectMenu';
if (interaction.isChannelSelectMenu()) return 'channelSelectMenu';
if (interaction.isMentionableSelectMenu()) return 'mentionableSelectMenu';
if (interaction.isRoleSelectMenu()) return 'roleSelectMenu';
if (interaction.isUserSelectMenu()) return 'userSelectMenu';
})();

let action = client.interaction.get(`${interactionType}-${interaction.customId || interaction.commandName}`);
if (!action) return;
try {
action.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this context menu!', ephemeral: true });
console.log(error);
}

if (interaction.isCommand()) {
const command = client.slashCommands.get(interaction.commandName);
if (!command) return;
try {
command.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
console.log(error);
}
}

if (interaction.isModalSubmit()) {
const modal = client.modals.get(interaction.customId);
if (!modal) return;
try {
modal.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this modal!', ephemeral: true });
console.log(error);
}
}

if (interaction.isButton()) {
const button = client.buttons.get(interaction.customId);
if (!button) return;
try {
button.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this button!', ephemeral: true });
console.log(error);
}
}

if (interaction.isAnySelectMenu()) {
const selectMenu = client.selectMenus.get(interaction.customId);
if (!selectMenu) return;
try {
selectMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
console.log(error);
}
}

if (interaction.isStringSelectMenu()) {
const stringSelectMenu = client.stringSelectMenus.get(interaction.customId);
if (!stringSelectMenu) return;
try {
stringSelectMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
console.log(error);
}
}

if (interaction.isChannelSelectMenu()) {
const channelSelectMenu = client.channelSelectMenus.get(interaction.customId);
if (!channelSelectMenu) return;
try {
channelSelectMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
console.log(error);
}
}

if (interaction.isMentionableSelectMenu()) {
const mentionableSelectMenu = client.mentionableSelectMenus.get(interaction.customId);
if (!mentionableSelectMenu) return;
try {
mentionableSelectMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
console.log(error);
}
}

if (interaction.isRoleSelectMenu()) {
const roleSelectMenu = client.roleSelectMenus.get(interaction.customId);
if (!roleSelectMenu) return;
try {
roleSelectMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
console.log(error);
}
}

if (interaction.isUserSelectMenu()) {
const userSelectMenu = client.userSelectMenus.get(interaction.customId);
if (!userSelectMenu) return;
try {
userSelectMenu.execute(client, interaction);
} catch (error) {
interaction.reply({ content: 'There was an error while executing this select menu!', ephemeral: true });
console.log(error);
}
}

} catch (error) {
return console.log(error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {

if (message.content.startsWith(config.bot.prefix)) {
const args = message.content.slice(config.bot.prefix.length).trim().split(/ +/);
const command = client.messageCommands.get(args[0]) || client.messageCommands.find(cmd => cmd.aliases && cmd.aliases.includes(args[0]));
const command = client.interaction.get(`messageCommand-${args[0]}`);
if (!command) return;
try {
command.execute(client, message, args);
Expand Down
23 changes: 23 additions & 0 deletions src/handlers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import config from '../data/config.js';
import chalk from 'chalk';
import { REST, Routes } from 'discord.js';
const rest = new REST({ version: '10' }).setToken(config.bot.token);

async function registerApplicationCommands(client) {
try {
let commands = new Map();
for (const [key, value] of client.interaction) {
if (key.startsWith('slashCommand-') || key.startsWith('contextMenu-')) commands.set(key, value);
}

await rest.put(
Routes.applicationCommands(client.user.id),
{ body: [...commands.values()].map(command => command.data.toJSON()) },
);
console.log(chalk.greenBright('[APPLICATION] Successfully registered application commands.'));
} catch (err) {
console.error(err);
}
}

export default registerApplicationCommands;
29 changes: 0 additions & 29 deletions src/handlers/buttons.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/handlers/channelSelectMenus.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/handlers/contextMenus.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/handlers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ async function loadEvents(client) {
}
}

export default { loadEvents };
export default loadEvents;
39 changes: 39 additions & 0 deletions src/handlers/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'fs';
import chalk from 'chalk';

async function loadInteractions(folderPath, client) {
try {
let files = fs.readdirSync(folderPath);

for (let i = 0; i < files.length; i++) {
let file = files[i];
let filePath = folderPath + '/' + file;
let stats = fs.statSync(filePath);

if (stats.isDirectory()) {
await loadInteractions(filePath, client);
} else {
let interactionPath = '.' + filePath;
let interaction = await import(`../${interactionPath}?${Date.now()}`);

if (interaction.default.disabled === true) return;
if (interaction.default.type === 'messageCommand') {
client.interaction.set(`messageCommand-${interaction.default.id}`, interaction.default);
console.log(chalk.greenBright(`[${interaction.default.type.toUpperCase()}] Successfully Loaded ${(chalk.yellow(file))} ${(chalk.yellow(interaction.default.id))}`));
if (interaction.default.aliases) {
for (let i = 0; i < interaction.default.aliases.length; i++) {
client.interaction.set(`messageCommand-${interaction.default.aliases[i]}`, interaction.default);
}
}
} else {
client.interaction.set(`${interaction.default.type}-${interaction.default.id}`, interaction.default);
console.log(chalk.greenBright(`[${interaction.default.type.toUpperCase()}] Successfully Loaded ${(chalk.yellow(file))} ${(chalk.yellow(interaction.default.id))}`));
}
}
}
} catch (err) {
console.error(err);
}
}

export default loadInteractions;
29 changes: 0 additions & 29 deletions src/handlers/mentionableSelectMenus.js

This file was deleted.

Loading

0 comments on commit 74d2dab

Please sign in to comment.