2022-09-24 13:06:19 -04:00
|
|
|
require("dotenv").config();
|
2023-01-08 02:50:40 -05:00
|
|
|
const fs = require("fs").promises;
|
2022-09-24 13:06:19 -04:00
|
|
|
|
|
|
|
module.exports = async (client) => {
|
|
|
|
// Slash Commands
|
2023-01-08 02:50:40 -05:00
|
|
|
const slashCommands = await fs.readdir(`${process.cwd()}/commands/*/*.js`);
|
|
|
|
const arrayOfSlashCommands = slashCommands.map((filename) => {
|
|
|
|
const file = require(`${process.cwd()}/commands/${filename}`);
|
2022-09-24 13:06:19 -04:00
|
|
|
if (!file?.name) return;
|
|
|
|
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
|
2023-01-08 02:50:40 -05:00
|
|
|
return file;
|
2022-09-24 13:06:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Events
|
2023-01-08 02:50:40 -05:00
|
|
|
const eventFiles = await fs.readdir(`${process.cwd()}/events/*.js`);
|
|
|
|
eventFiles.forEach((filename) => require(`${process.cwd()}/events/${filename}`));
|
2022-09-24 13:06:19 -04:00
|
|
|
|
|
|
|
// Slash Commands Register
|
|
|
|
client.on("ready", async () => {
|
|
|
|
// Register for all the guilds the bot is in
|
|
|
|
await client.application.commands.set(arrayOfSlashCommands);
|
2023-01-08 02:50:40 -05:00
|
|
|
|
2022-09-24 13:06:19 -04:00
|
|
|
});
|
|
|
|
|
2023-01-08 02:50:40 -05:00
|
|
|
client.on("interactionCreate", async (interaction) => {
|
|
|
|
// Slash Command Handling
|
|
|
|
if (interaction.isChatInputCommand()) {
|
|
|
|
let commandData = arrayOfSlashCommands.find((command) => command.name === interaction.commandName);
|
|
|
|
if (!commandData) return interaction.followUp({ content: "An error has occurred " });
|
|
|
|
const cmd = client.slashCommands.get(interaction.commandName);
|
|
|
|
if (!cmd) return interaction.followUp({ content: "An error has occurred " });
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
interaction.member = interaction.guild.members.cache.get(interaction.user.id);
|
|
|
|
|
|
|
|
if (commandData.private) {
|
|
|
|
await interaction.deferReply({ ephemeral: true }).catch(() => { });
|
|
|
|
} else {
|
|
|
|
await interaction.deferReply({ ephemeral: false }).catch(() => { });
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|