CatalystMoon/events/interactionCreate.js

89 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-07-22 18:09:13 -04:00
const client = require("../index");
require("dotenv").config();
const { glob } = require("glob");
const { promisify } = require("util");
const globPromise = promisify(glob);
2022-07-22 18:09:13 -04:00
client.on("interactionCreate", async (interaction) => {
// Slash Commands
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
const arrayOfSlashCommands = [];
2024-10-03 18:00:03 -04:00
// Map the slash commands into data to be processed
slashCommands.map((value) => {
2024-10-03 18:00:03 -04:00
const file = require(value);
const splitted = value.split("/");
const directory = splitted[splitted.length - 2];
2024-10-03 18:00:03 -04:00
if (!file?.name) return;
2024-10-03 18:00:03 -04:00
const properties = { directory, ...file };
client.slashCommands.set(file.name, properties);
2024-10-03 18:00:03 -04:00
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
2024-10-03 18:00:03 -04:00
// Push the data
const JSONCommand = {
...file,
integration_types: [0, 1], // 0 for guild, 1 for user
contexts: [0, 1, 2], // 0 for guild, 1 for app DMs, 2 for GDMs and other DMs
};
arrayOfSlashCommands.push(JSONCommand);
});
2022-07-22 18:09:13 -04:00
// Slash Command Handling
if (interaction.isChatInputCommand()) {
2024-10-03 18:00:03 -04:00
let commandData = [];
2022-07-22 18:09:13 -04:00
2024-10-03 18:00:03 -04:00
// Filter to find the command for this interaction
await arrayOfSlashCommands.forEach(command => {
if (command.name === interaction.commandName) {
commandData.push(command);
}
});
2024-10-03 18:00:03 -04:00
// Process and parse the command data
const parsedData = commandData[0];
2023-04-22 17:11:37 -04:00
2024-10-03 18:00:03 -04:00
// Defer reply based on privacy settings
if (interaction.commandName === "modal-example") {
console.log("Modal - Skipping defer");
} else {
const isPrivate = parsedData?.private;
await interaction.deferReply({
ephemeral: !!isPrivate,
}).catch(() => {});
}
2024-10-03 18:00:03 -04:00
const cmd = client.slashCommands.get(interaction.commandName);
if (!cmd) {
return interaction.followUp({ content: "An error has occurred" });
2023-04-22 17:11:37 -04:00
}
2022-07-22 18:09:13 -04:00
2024-10-03 18:00:03 -04:00
const args = [];
for (let option of interaction.options.data) {
if (option.type === "SUB_COMMAND") {
if (option.name) args.push(option.name);
option.options?.forEach((x) => {
if (x.value) args.push(x.value);
});
} else if (option.value) args.push(option.value);
}
2022-07-22 18:09:13 -04:00
2024-10-03 18:00:03 -04:00
// Check if the interaction is in a guild and assign member accordingly
if (interaction.inGuild()) {
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
2024-10-03 18:00:03 -04:00
}
2022-07-22 18:09:13 -04:00
2024-10-03 18:00:03 -04:00
// Run the command
cmd.run(client, interaction, args);
2022-07-22 18:09:13 -04:00
}
// Context Menu Handling
if (interaction.isContextMenuCommand()) {
2024-10-03 18:00:03 -04:00
await interaction.deferReply({ ephemeral: false });
const command = client.slashCommands.get(interaction.commandName);
if (command) command.run(client, interaction);
2022-07-22 18:09:13 -04:00
}
2024-10-03 18:00:03 -04:00
});