Code Optimization for all files

This commit is contained in:
Raven Scott
2023-01-08 09:50:40 +02:00
parent cb8a312d93
commit 85f2d8d04a
10 changed files with 322 additions and 382 deletions

View File

@ -1,63 +1,35 @@
const client = require("../index");
require("dotenv").config();
const { glob } = require("glob");
const { promisify } = require("util");
const globPromise = promisify(glob);
const fs = require("fs").promises;
client.on("interactionCreate", async (interaction) => {
// Slash Commands
const slashCommands = await globPromise(`${process.cwd()}/commands/*/*.js`);
const arrayOfSlashCommands = [];
slashCommands.map((value) => {
const file = require(value);
const splitted = value.split("/");
const directory = splitted[splitted.length - 2];
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;
const properties = { directory, ...file };
client.slashCommands.set(file.name, properties);
if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
arrayOfSlashCommands.push(file);
return file;
});
// Slash Command Handling
if (interaction.isChatInputCommand()) {
let commandData = []
await arrayOfSlashCommands.forEach(command => {
console.log(command.name)
if (command.name == interaction.commandName) {
commandData.push(command)
}
});
let dataToProcess = JSON.stringify(commandData[0])
let parsedData = JSON.parse(dataToProcess)
if (parsedData.private == true) {
await interaction.deferReply({ ephemeral: true }).catch(() => { });
} else {
await interaction.deferReply({ ephemeral: false }).catch(() => { });
}
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 " });
if (!cmd) return interaction.followUp({ content: "An error has occurred " });
const args = [];
for (let option of interaction.options.data) {
const args = interaction.options.data.map((option) => {
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);
}
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);
@ -69,5 +41,4 @@ client.on("interactionCreate", async (interaction) => {
const command = client.slashCommands.get(interaction.commandName);
if (command) command.run(client, interaction);
}
});