gbfcommands
Version:
A feature rich and easy to setup Discord Bot Command Handler
92 lines (91 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlerChecks = void 0;
const discord_js_1 = require("discord.js");
const GBF_1 = require("../GBF");
const Utils_1 = require("../../Utils/Utils");
const Context_Handler_1 = require("../Command Handlers/Context Handler");
/** Represents a collection to store command cooldowns. */
const CommandCooldowns = new discord_js_1.Collection();
class HandlerChecks {
constructor(client, commandUser, commandMember, userBlacklistData, guildSettings, command, actionType) {
this.client = client;
this.CommandUser = commandUser;
this.CommandMember = commandMember;
this.UserBlacklistData = userBlacklistData;
this.GuildSettings = guildSettings;
this.Command = command;
this.ActionType = actionType;
}
async RunChecks() {
const ErrorEmbed = new discord_js_1.EmbedBuilder()
.setColor(GBF_1.ColorCodes.ErrorRed)
.setTimestamp();
const InGuild = !(this.ActionType.channel instanceof discord_js_1.DMChannel);
if (this.UserBlacklistData) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} Account Ban Notice`).setDescription(`Your account has been banned from ${this.client.user.username} due to a violation of ${this.client.user.username}'s terms of service`);
return [ErrorEmbed, false];
}
if (this.Command.CommandOptions.development &&
!this.client.TestServers.includes(this.ActionType.guildId)) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't use that`).setDescription(`${this.Command.CommandOptions.name} is disabled globally.`);
return [ErrorEmbed, false];
}
if (!(this.Command instanceof Context_Handler_1.ContextCommand))
if (!this.Command.CommandOptions.DMEnabled && !InGuild) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't use that here`).setDescription(`${this.Command.CommandOptions.name} is disabled in DMs.`);
return [ErrorEmbed, false];
}
if (this.Command.CommandOptions.DeveloperOnly &&
!this.client.Developers.includes(this.CommandUser.id)) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't use that`).setDescription(`${this.Command.CommandOptions.name} is a developer only command.`);
return [ErrorEmbed, false];
}
if (this.Command.CommandOptions.NSFW &&
InGuild &&
!this.ActionType.channel.nsfw) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't use that`).setDescription(`You cannot use NSFW commands in non-NSFW channels.`);
return [ErrorEmbed, false];
}
if (InGuild) {
if (this.Command.CommandOptions.UserPermissions &&
!this.CommandMember.permissions.has(this.Command.CommandOptions.UserPermissions, true)) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't use that`).setDescription(`${this.CommandUser.username}, You are missing the following permissions: ${(0, Utils_1.MissingPermissions)(this.CommandMember, this.Command.CommandOptions.UserPermissions)}`);
return [ErrorEmbed, false];
}
if (this.Command.CommandOptions.BotPermissions &&
!this.ActionType.channel
.permissionsFor(this.ActionType.guild.members.me)
.has(this.Command.CommandOptions.BotPermissions, true)) {
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't do that`).setDescription(`${this.CommandUser.username}, I am missing the following permissions: ${(0, Utils_1.MissingPermissions)(this.ActionType.guild.members.me, this.Command.CommandOptions.BotPermissions)}`);
return [ErrorEmbed, false];
}
}
if (!this.Command.CommandOptions.DeveloperBypass ||
(this.Command.CommandOptions.DeveloperBypass &&
!this.client.Developers.includes(this.CommandUser.id))) {
/**@unit Seconds */
const ProvidedCooldownTime = this.Command.CommandOptions.cooldown;
if (ProvidedCooldownTime) {
if (!CommandCooldowns.has(this.Command.CommandOptions.name))
CommandCooldowns.set(this.Command.CommandOptions.name, new Map());
const CurrentTime = Date.now();
const Timestamps = CommandCooldowns.get(this.Command.CommandOptions.name);
/**@unit Miliseconds */
const CooldownTime = ProvidedCooldownTime * 1000;
if (Timestamps.has(this.CommandUser.id)) {
const ExpirationTime = Timestamps.get(this.CommandUser.id) + CooldownTime;
if (CurrentTime < ExpirationTime) {
const RemainingTime = Number((ExpirationTime - CurrentTime).toFixed(1));
const UnixFormat = Math.round(Date.now() / 1000 + RemainingTime / 1000);
ErrorEmbed.setTitle(`${GBF_1.Emojis.Error} You can't use that yet`).setDescription(`${this.CommandUser}, You can use "${this.Command.CommandOptions.name}" <t:${UnixFormat}:R>`);
}
}
Timestamps.set(this.CommandUser.id, CurrentTime);
setTimeout(() => Timestamps.delete(this.CommandUser.id), CooldownTime).unref();
}
}
return [ErrorEmbed, true];
}
}
exports.HandlerChecks = HandlerChecks;