Skip to content

Commit

Permalink
Merge pull request #2 from BLACK4585/master
Browse files Browse the repository at this point in the history
Added handlers for every Select Menu
  • Loading branch information
MastiDev authored Jun 4, 2023
2 parents 0a4367f + 035d350 commit d0b80c6
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Simple and easy to use Slash Command Handler for Discord.js v14.

## Prerequisites
- Node.js v20.0.0 or newer is required. You can download it [here](https://nodejs.org/en/download/).
- Node.js v18.16.0 or newer is recommended. Older versions might work. You can download it [here](https://nodejs.org/en/download/).

## Installation
1. Fill out the `config.js.TEMPLATE` located in the `src/data` folder.
Expand All @@ -24,6 +24,10 @@ Category folders are optional, but you cant have a subfolder in a category.
...
```

## 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``

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

Expand Down
57 changes: 56 additions & 1 deletion src/events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
}
}

if (interaction.isStringSelectMenu()) {
if (interaction.isAnySelectMenu()) {
const selectMenu = client.selectMenus.get(interaction.customId);
if (!selectMenu) return;
try {
Expand All @@ -57,6 +57,61 @@ export default {
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
29 changes: 29 additions & 0 deletions src/handlers/channelSelectMenus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readdirSync } from 'fs';
import chalk from 'chalk';

async function loadChannelSelectMenus(client) {
client.channelSelectMenus.clear();
let files = 0;
const channelSelectMenuFiles = readdirSync('./src/interactions/channelSelectMenus').filter(file => file.endsWith('.js'));
if (!client.channelSelectMenus) return;
for (let i = 0; i < channelSelectMenuFiles.length; i++) {
const channelSelectMenu = await import(`../interactions/channelSelectMenus/${channelSelectMenuFiles[i]}?${Date.now()}`);
await client.channelSelectMenus.set(channelSelectMenu.default.id, channelSelectMenu.default);
console.log(chalk.greenBright(`[CHANNELSELECTMENU] Loaded ${(chalk.yellow(channelSelectMenuFiles[i]))} with channelSelectMenu ${(chalk.yellow(channelSelectMenu.default.id))}`));
files++;
}
const channelSelectMenuFolders = readdirSync('./src/interactions/channelSelectMenus', { withFileTypes: true }).filter(file => file.isDirectory());
if (!channelSelectMenuFolders) return;
for (let i = 0; i < channelSelectMenuFolders.length; i++) {
const channelSelectMenuFiles = readdirSync(`./src/interactions/channelSelectMenus/${channelSelectMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
for (let j = 0; j < channelSelectMenuFiles.length; j++) {
const channelSelectMenu = await import(`../interactions/channelSelectMenus/${channelSelectMenuFolders[i].name}/${channelSelectMenuFiles[j]}?${Date.now()}`);
await client.channelSelectMenus.set(channelSelectMenu.default.id, channelSelectMenu.default);
console.log(chalk.greenBright(`[CHANNELSELECTMENU] Loaded ${(chalk.yellow(channelSelectMenuFiles[j]))} with channelSelectMenu ${(chalk.yellow(channelSelectMenu.default.id))}`));
files++;
}
}
return files;
}

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

async function loadMentionableSelectMenus(client) {
client.mentionableSelectMenus.clear();
let files = 0;
const mentionableSelectMenuFiles = readdirSync('./src/interactions/mentionableSelectMenus').filter(file => file.endsWith('.js'));
if (!client.mentionableSelectMenus) return;
for (let i = 0; i < mentionableSelectMenuFiles.length; i++) {
const mentionableSelectMenu = await import(`../interactions/mentionableSelectMenus/${mentionableSelectMenuFiles[i]}?${Date.now()}`);
await client.mentionableSelectMenus.set(mentionableSelectMenu.default.id, mentionableSelectMenu.default);
console.log(chalk.greenBright(`[MENTIONABLESELECTMENU] Loaded ${(chalk.yellow(mentionableSelectMenuFiles[i]))} with mentionableSelectMenu ${(chalk.yellow(mentionableSelectMenu.default.id))}`));
files++;
}
const mentionableSelectMenuFolders = readdirSync('./src/interactions/mentionableSelectMenus', { withFileTypes: true }).filter(file => file.isDirectory());
if (!mentionableSelectMenuFolders) return;
for (let i = 0; i < mentionableSelectMenuFolders.length; i++) {
const mentionableSelectMenuFiles = readdirSync(`./src/interactions/mentionableSelectMenus/${mentionableSelectMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
for (let j = 0; j < mentionableSelectMenuFiles.length; j++) {
const mentionableSelectMenu = await import(`../interactions/mentionableSelectMenus/${mentionableSelectMenuFolders[i].name}/${mentionableSelectMenuFiles[j]}?${Date.now()}`);
await client.mentionableSelectMenus.set(mentionableSelectMenu.default.id, mentionableSelectMenu.default);
console.log(chalk.greenBright(`[MENTIONABLESELECTMENU] Loaded ${(chalk.yellow(mentionableSelectMenuFiles[j]))} with mentionableSelectMenu ${(chalk.yellow(mentionableSelectMenu.default.id))}`));
files++;
}
}
return files;
}

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

async function loadRoleSelectMenus(client) {
client.roleSelectMenus.clear();
let files = 0;
const roleSelectMenuFiles = readdirSync('./src/interactions/roleSelectMenus').filter(file => file.endsWith('.js'));
if (!client.roleSelectMenus) return;
for (let i = 0; i < roleSelectMenuFiles.length; i++) {
const roleSelectMenu = await import(`../interactions/roleSelectMenus/${roleSelectMenuFiles[i]}?${Date.now()}`);
await client.roleSelectMenus.set(roleSelectMenu.default.id, roleSelectMenu.default);
console.log(chalk.greenBright(`[ROLESELECTMENU] Loaded ${(chalk.yellow(roleSelectMenuFiles[i]))} with roleSelectMenu ${(chalk.yellow(roleSelectMenu.default.id))}`));
files++;
}
const roleSelectMenuFolders = readdirSync('./src/interactions/roleSelectMenus', { withFileTypes: true }).filter(file => file.isDirectory());
if (!roleSelectMenuFolders) return;
for (let i = 0; i < roleSelectMenuFolders.length; i++) {
const roleSelectMenuFiles = readdirSync(`./src/interactions/roleSelectMenus/${roleSelectMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
for (let j = 0; j < roleSelectMenuFiles.length; j++) {
const roleSelectMenu = await import(`../interactions/roleSelectMenus/${roleSelectMenuFolders[i].name}/${roleSelectMenuFiles[j]}?${Date.now()}`);
await client.roleSelectMenus.set(roleSelectMenu.default.id, roleSelectMenu.default);
console.log(chalk.greenBright(`[ROLESELECTMENU] Loaded ${(chalk.yellow(roleSelectMenuFiles[j]))} with roleSelectMenu ${(chalk.yellow(roleSelectMenu.default.id))}`));
files++;
}
}
return files;
}

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

async function loadStringSelectMenus(client) {
client.stringSelectMenus.clear();
let files = 0;
const stringSelectMenuFiles = readdirSync('./src/interactions/stringSelectMenus').filter(file => file.endsWith('.js'));
if (!client.stringSelectMenus) return;
for (let i = 0; i < stringSelectMenuFiles.length; i++) {
const stringSelectMenu = await import(`../interactions/stringSelectMenus/${stringSelectMenuFiles[i]}?${Date.now()}`);
await client.stringSelectMenus.set(stringSelectMenu.default.id, stringSelectMenu.default);
console.log(chalk.greenBright(`[STRINGSELECTMENU] Loaded ${(chalk.yellow(stringSelectMenuFiles[i]))} with stringSelectMenu ${(chalk.yellow(stringSelectMenu.default.id))}`));
files++;
}
const stringSelectMenuFolders = readdirSync('./src/interactions/stringSelectMenus', { withFileTypes: true }).filter(file => file.isDirectory());
if (!stringSelectMenuFolders) return;
for (let i = 0; i < stringSelectMenuFolders.length; i++) {
const stringSelectMenuFiles = readdirSync(`./src/interactions/stringSelectMenus/${stringSelectMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
for (let j = 0; j < stringSelectMenuFiles.length; j++) {
const stringSelectMenu = await import(`../interactions/stringSelectMenus/${stringSelectMenuFolders[i].name}/${stringSelectMenuFiles[j]}?${Date.now()}`);
await client.stringSelectMenus.set(stringSelectMenu.default.id, stringSelectMenu.default);
console.log(chalk.greenBright(`[STRINGSELECTMENU] Loaded ${(chalk.yellow(stringSelectMenuFiles[j]))} with stringSelectMenu ${(chalk.yellow(stringSelectMenu.default.id))}`));
files++;
}
}
return files;
}

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

async function loadUserSelectMenus(client) {
client.userSelectMenus.clear();
let files = 0;
const userSelectMenuFiles = readdirSync('./src/interactions/userSelectMenus').filter(file => file.endsWith('.js'));
if (!client.userSelectMenus) return;
for (let i = 0; i < userSelectMenuFiles.length; i++) {
const userSelectMenu = await import(`../interactions/userSelectMenus/${userSelectMenuFiles[i]}?${Date.now()}`);
await client.userSelectMenus.set(userSelectMenu.default.id, userSelectMenu.default);
console.log(chalk.greenBright(`[USERSELECTMENU] Loaded ${(chalk.yellow(userSelectMenuFiles[i]))} with userSelectMenu ${(chalk.yellow(userSelectMenu.default.id))}`));
files++;
}
const userSelectMenuFolders = readdirSync('./src/interactions/userSelectMenus', { withFileTypes: true }).filter(file => file.isDirectory());
if (!userSelectMenuFolders) return;
for (let i = 0; i < userSelectMenuFolders.length; i++) {
const userSelectMenuFiles = readdirSync(`./src/interactions/userSelectMenus/${userSelectMenuFolders[i].name}`).filter(file => file.endsWith('.js'));
for (let j = 0; j < userSelectMenuFiles.length; j++) {
const userSelectMenu = await import(`../interactions/userSelectMenus/${userSelectMenuFolders[i].name}/${userSelectMenuFiles[j]}?${Date.now()}`);
await client.userSelectMenus.set(userSelectMenu.default.id, userSelectMenu.default);
console.log(chalk.greenBright(`[USERSELECTMENU] Loaded ${(chalk.yellow(userSelectMenuFiles[j]))} with userSelectMenu ${(chalk.yellow(userSelectMenu.default.id))}`));
files++;
}
}
return files;
}

export default { loadUserSelectMenus };
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import slashCommandHandler from './handlers/slashCommands.js';
import modalHandler from './handlers/modals.js';
import buttonHandler from './handlers/buttons.js';
import selectMenuHandler from './handlers/selectMenus.js';
import stringSelectMenuHandler from './handlers/stringSelectMenus.js';
import channelSelectMenuHandler from './handlers/channelSelectMenus.js';
import mentionableSelectMenuHandler from './handlers/mentionableSelectMenus.js';
import roleSelectMenuHandler from './handlers/roleSelectMenus.js';
import userSelectMenuHandler from './handlers/userSelectMenus.js';
import contextMenus from './handlers/contextMenus.js';
import register from './handlers/register.js';

Expand Down Expand Up @@ -34,6 +39,11 @@ client.slashCommands = new Discord.Collection();
client.modals = new Discord.Collection();
client.buttons = new Discord.Collection();
client.selectMenus = new Discord.Collection();
client.stringSelectMenus = new Discord.Collection();
client.channelSelectMenus = new Discord.Collection();
client.mentionableSelectMenus = new Discord.Collection();
client.roleSelectMenus = new Discord.Collection();
client.userSelectMenus = new Discord.Collection();
client.contextMenus = new Discord.Collection();

await eventHandler.loadEvents(client);
Expand All @@ -42,6 +52,11 @@ await slashCommandHandler.loadSlashCommands(client);
await modalHandler.loadModals(client);
await buttonHandler.loadButtons(client);
await selectMenuHandler.loadSelectMenus(client);
await stringSelectMenuHandler.loadStringSelectMenus(client);
await channelSelectMenuHandler.loadChannelSelectMenus(client);
await mentionableSelectMenuHandler.loadMentionableSelectMenus(client);
await roleSelectMenuHandler.loadRoleSelectMenus(client);
await userSelectMenuHandler.loadUserSelectMenus(client);
await contextMenus.loadContextMenus(client);
await register.load(client);

Expand Down
6 changes: 6 additions & 0 deletions src/interactions/channelSelectMenus/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
id: 'test',
async execute(client, interaction) {
interaction.reply('Hello World!');
}
};
6 changes: 6 additions & 0 deletions src/interactions/mentionableSelectMenus/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
id: 'test',
async execute(client, interaction) {
interaction.reply('Hello World!');
}
};
6 changes: 6 additions & 0 deletions src/interactions/roleSelectMenus/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
id: 'test',
async execute(client, interaction) {
interaction.reply('Hello World!');
}
};
6 changes: 6 additions & 0 deletions src/interactions/stringSelectMenus/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
id: 'test',
async execute(client, interaction) {
interaction.reply('Hello World!');
}
};
6 changes: 6 additions & 0 deletions src/interactions/userSelectMenus/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
id: 'test',
async execute(client, interaction) {
interaction.reply('Hello World!');
}
};

0 comments on commit d0b80c6

Please sign in to comment.