djsbotbase-test
Version:
Discord.js tabanlı komut ve etkinlik sistemlerine sahip bir bot temeli
81 lines (80 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCooldown = checkCooldown;
exports.addCooldown = addCooldown;
exports.editCooldown = editCooldown;
exports.getCooldown = getCooldown;
const commandClass_1 = require("../commands/commandClass");
const cooldowns = new Map();
function checkCooldown(userId, command) {
const cooldownItem = getCooldown(userId, command);
const isValid = evaluateCooldownValidity(cooldownItem, command);
return [cooldownItem, isValid];
}
function addCooldown(userId, command, cooldownOverride) {
if (!shouldApplyCooldown(command)) {
return;
}
const cooldownItem = createCooldown(command, cooldownOverride);
setCooldown(userId, cooldownItem);
}
function editCooldown(userId, command, options) {
const existingCooldown = getCooldown(userId, command);
const baseCooldown = existingCooldown ?? createCooldown(command);
const updatedCooldown = { ...baseCooldown, ...options };
setCooldown(userId, updatedCooldown);
}
function getCooldown(userId, command) {
const userCooldowns = cooldowns.get(userId);
if (!userCooldowns) {
return undefined;
}
const commandInfo = getCommandInfo(command);
return userCooldowns.find(cooldown => cooldown.commandName === commandInfo.name &&
cooldown.commandType === commandInfo.type);
}
function evaluateCooldownValidity(cooldownItem, command) {
if (!cooldownItem) {
return true;
}
if (!shouldApplyCooldown(command)) {
return true;
}
if (Date.now() > cooldownItem.endsAt) {
return true;
}
return false;
}
function shouldApplyCooldown(command) {
return Number.isInteger(command.cooldown) && command.cooldown > 0;
}
function createCooldown(command, cooldownOverride) {
const commandInfo = getCommandInfo(command);
const cooldownDuration = cooldownOverride ?? command.cooldown;
const endsAt = Date.now() + cooldownDuration * 1000;
return {
commandName: commandInfo.name,
commandType: commandInfo.type,
endsAt,
messageShown: false,
};
}
function getCommandInfo(command) {
if (command instanceof commandClass_1.Command) {
return {
name: command.name,
type: "message",
};
}
return {
name: command.convertCommandData().name,
type: "slash",
};
}
function setCooldown(userId, newCooldown) {
const userCooldowns = cooldowns.get(userId) ?? [];
const filteredCooldowns = userCooldowns.filter(cooldown => !(cooldown.commandType === newCooldown.commandType &&
cooldown.commandName === newCooldown.commandName));
const updatedCooldowns = [...filteredCooldowns, newCooldown];
cooldowns.set(userId, updatedCooldowns);
}