ravenbotv14/events/interactionCreate.js
2023-01-08 09:50:40 +02:00

45 lines
1.5 KiB
JavaScript

const client = require("../index");
require("dotenv").config();
const fs = require("fs").promises;
client.on("interactionCreate", async (interaction) => {
// Slash Commands
const slashCommands = await fs.readdir(`${process.cwd()}/commands/*/*.js`);
const arrayOfSlashCommands = slashCommands.map((filename) => {
const file = require(`${process.cwd()}/commands/${filename}`);
if (!file?.name) return;
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
return file;
});
// Slash Command Handling
if (interaction.isChatInputCommand()) {
const commandData = arrayOfSlashCommands.find(
(command) => command.name === interaction.commandName
);
if (!commandData) return;
const cmd = client.slashCommands.get(interaction.commandName);
if (!cmd) return interaction.followUp({ content: "An error has occurred " });
const args = interaction.options.data.map((option) => {
if (option.type === "SUB_COMMAND") {
if (option.name) return option.name;
return option.options?.map((x) => x.value).filter((x) => x);
}
return option.value;
});
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
cmd.run(client, interaction, args);
}
// Context Menu Handling
if (interaction.isContextMenuCommand()) {
await interaction.deferReply({ ephemeral: false });
const command = client.slashCommands.get(interaction.commandName);
if (command) command.run(client, interaction);
}
});