ravenbotv14/events/interactionCreate.js

45 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-09-24 17:06:19 +00:00
const client = require("../index");
2022-09-28 21:58:24 +00:00
require("dotenv").config();
2023-01-08 07:50:40 +00:00
const fs = require("fs").promises;
2022-09-24 17:06:19 +00:00
client.on("interactionCreate", async (interaction) => {
2022-09-28 21:58:24 +00:00
// Slash Commands
2023-01-08 07:50:40 +00:00
const slashCommands = await fs.readdir(`${process.cwd()}/commands/*/*.js`);
const arrayOfSlashCommands = slashCommands.map((filename) => {
const file = require(`${process.cwd()}/commands/${filename}`);
2022-09-28 21:58:24 +00:00
if (!file?.name) return;
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
2023-01-08 07:50:40 +00:00
return file;
2022-09-28 21:58:24 +00:00
});
2022-09-24 17:06:19 +00:00
// Slash Command Handling
if (interaction.isChatInputCommand()) {
2023-01-08 07:50:40 +00:00
const commandData = arrayOfSlashCommands.find(
(command) => command.name === interaction.commandName
);
if (!commandData) return;
2022-09-28 21:58:24 +00:00
2022-09-24 17:06:19 +00:00
const cmd = client.slashCommands.get(interaction.commandName);
2023-01-08 07:50:40 +00:00
if (!cmd) return interaction.followUp({ content: "An error has occurred " });
2022-09-24 17:06:19 +00:00
2023-01-08 07:50:40 +00:00
const args = interaction.options.data.map((option) => {
2022-09-24 17:06:19 +00:00
if (option.type === "SUB_COMMAND") {
2023-01-08 07:50:40 +00:00
if (option.name) return option.name;
return option.options?.map((x) => x.value).filter((x) => x);
}
return option.value;
});
2022-09-24 17:06:19 +00:00
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);
}
});