Files
DiscordBotOceano/src/events/interactionCreate/handleCommands.ts
2024-04-07 12:44:16 +02:00

76 lines
2.7 KiB
TypeScript

import { Client, Interaction } from "discord.js";
import { devs, testServer } from "../../../config.json"
import getLocalCommands from "../../utils/getLocalCommands"
import CommandInterface from "../../utils/commandInterface";
export default async (client: Client<true>, interaction: Interaction) => {
if(!interaction.isChatInputCommand()) return;
const localCommands = await getLocalCommands() as CommandInterface[];
try {
const commandObject = localCommands.find((cmd) => cmd.name == interaction.commandName);
if(!commandObject) return;
console.log(`/${interaction.commandName} wurde von ${interaction.user.username} ausgeführt!`)
if(commandObject.devOnly){
if(!devs.includes(interaction.user.id)) {
interaction.reply({
content: "Nur Developer können diesen Befehl ausführen!",
ephemeral: true,
});
return;
}
}
if(commandObject.testOnly){
if(!(interaction.guildId === testServer)) {
interaction.reply({
content: "Diese Command kann bisher nur im Hauseigenden Test-Discord genutzt werden!",
ephemeral: true,
})
return;
}
}
if(commandObject.permissionsRequired?.length){
for(const permission of commandObject.permissionsRequired){
if(!interaction.memberPermissions?.has(permission)){
interaction.reply({
content: "Dir fehlen die Nötigen Berechtigungen zum Ausführen dieses Commands!",
ephemeral: true,
})
return;
}
}
}
if(commandObject.botPermissions?.length){
for(const permission of commandObject.botPermissions){
const bot = interaction.guild?.members.me;
if(!bot?.permissions.has(permission)){
interaction.reply({
content: "Ich habe nicht genug Rechte um dies Auszuführen! Benötigt: " + commandObject.botPermissions,
ephemeral: true,
});
return;
}
}
}
if(commandObject.onlyServerCommand){
if(!interaction.inGuild()){
interaction.reply("Dieser Command kann nur in einem Server ausgefürt werden!");
return;
}
}
await commandObject.callback(client, interaction);
} catch (error) {
console.log(`Es gab einen Fehler den Comand auszuführen | ${error}`);
}
};