@discord-rahmen/commander
Version:
Commandhandler Package for discord-rahmen Framework
171 lines (170 loc) • 6.67 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DRCommander = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
class DRCommander {
directory;
baseDirectory;
logger;
testing;
privilegedUsers;
options;
commands = new Map();
layer;
cooldown;
constructor(options) {
this.directory = options.directory;
this.baseDirectory = options.baseDirectory;
this.testing = options.testing;
this.privilegedUsers = options.privilegedUsers;
this.options = options;
}
get commandsMap() {
return this.commands;
}
get wrapperClient() {
return this.layer.wrapperClient;
}
async init(layer) {
this.layer = layer;
// TODO: Setting up and using Logger
console.log("Loading all Slashcommands...");
const commands = await this.loadCommands();
console.log("\nDeploying all Slashcommands...");
const commandsStatus = await this.deployCommands(commands);
console.log(`Deployed ${commandsStatus}\n`);
// Starting Interactionevent
// @ts-ignore
this.layer.newInteractionListener(false, this.handleInteraction);
}
async deployCommands(commands) {
if (this.testing.bot === this.wrapperClient?.user.id) {
await this.layer.setCommand(commands, this.testing.guild);
return "all Slashcommands in the Testguild! 🥳";
}
if (this.testing.bot !== this.wrapperClient?.user.id) {
await this.layer.setCommand(commands);
return "all Slashcommands globally! 🥳";
}
return "any Slashcommands because nothing matched with the ClientID! 🙁";
}
async loadCommands() {
const commands = [];
await this.getFiles((0, path_1.join)(this.baseDirectory, this.directory));
for (const [name, command] of this.commands) {
let status = "Unloaded";
const options = [];
if (command.options) {
for (let commandOption of command.options) {
commandOption.type = this.layer.convertOptionType(commandOption.type);
options.push(commandOption);
}
}
if (command.name) {
if (command.excludeInProduction && this.testing.bot === this.wrapperClient?.user.id)
continue;
commands.push({
name: command.name,
description: command.description,
options: options,
});
this.commands.set(command.name, command);
status = "Loaded";
}
console.log(`${name} - ${status}`);
}
return commands;
}
async getFiles(directory) {
const folder = (0, fs_1.readdirSync)(directory);
for (const file of folder) {
const stats = (0, fs_1.lstatSync)((0, path_1.join)(directory, file));
if (file.endsWith(".js") || file.endsWith(".ts")) {
const filePath = `${directory}/${file}`;
const command = Promise.resolve().then(() => __importStar(require(filePath)));
this.commands.set(command.name, command);
continue;
}
if (stats.isDirectory()) {
await this.getFiles((0, path_1.join)(directory, file));
}
}
}
async handleInteraction(interaction) {
if (!interaction.command)
return;
// validating command and getting its data
const command = this.commands.get(interaction.commandName);
if (!command)
return;
if (command.privilegedUsers && (!this.privilegedUsers.includes(interaction.user.id))) {
await interaction.reply({
content: "You aren't allowed to use this command",
ephemeral: true
});
}
// TODO: permissionscheck for bot and user
// Adding Cooldown-map to the command if it doesn't exist yet
if (!this.cooldown.has(command.name)) {
this.cooldown.set(command.name, new Map());
}
const now = Date.now();
const timestamps = this.cooldown.get(command.name);
// Converting the cooldownamount into seconds-format
const cooldownAmount = (command.cooldown || 1) * 1000;
// Checking if command is cooldowned
if (timestamps.has(interaction.user.id)) {
const experationTime = timestamps.get(interaction.user.id) + cooldownAmount;
if (now < experationTime) {
const timeLeft = (experationTime - now) / 1000;
await interaction.reply({
content: `Wait another \`${timeLeft.toFixed(1)}\`seconds before you run **${command.name}** again!`,
ephemeral: true
});
return;
}
}
try {
await command.run({
interaction: interaction,
client: this.layer.wrapperClient,
// TODO: make pluginsmap aviable to the user
// pluginsMap: this.plugins,
user: interaction.user,
member: interaction.member
});
}
catch (error) {
console.error(error);
return;
}
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
return;
}
}
exports.DRCommander = DRCommander;