@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
77 lines (75 loc) • 3.24 kB
JavaScript
;
const require_lib_types_Enums = require('../lib/types/Enums.cjs');
const require_lib_errors_Identifiers = require('../lib/errors/Identifiers.cjs');
const require_lib_structures_Precondition = require('../lib/structures/Precondition.cjs');
let __sapphire_pieces = require("@sapphire/pieces");
let discord_js = require("discord.js");
let __sapphire_ratelimits = require("@sapphire/ratelimits");
//#region src/preconditions/Cooldown.ts
var CorePrecondition = class extends require_lib_structures_Precondition.AllFlowsPrecondition {
constructor(..._args) {
super(..._args);
this.buckets = /* @__PURE__ */ new WeakMap();
}
messageRun(message, command, context) {
const cooldownId = this.getIdFromMessage(message, context);
return this.sharedRun(message.author.id, command, context, cooldownId, "message");
}
chatInputRun(interaction, command, context) {
const cooldownId = this.getIdFromInteraction(interaction, context);
return this.sharedRun(interaction.user.id, command, context, cooldownId, "chat input");
}
contextMenuRun(interaction, command, context) {
const cooldownId = this.getIdFromInteraction(interaction, context);
return this.sharedRun(interaction.user.id, command, context, cooldownId, "context menu");
}
sharedRun(authorId, command, context, cooldownId, commandType) {
if (context.external) return this.ok();
if (!context.delay) return this.ok();
if (context.filteredUsers?.includes(authorId)) return this.ok();
const rateLimit = this.getManager(command, context).acquire(cooldownId);
if (rateLimit.limited) {
const remaining = rateLimit.remainingTime;
const nextAvailable = (0, discord_js.time)(Math.floor(rateLimit.expires / 1e3), discord_js.TimestampStyles.RelativeTime);
return this.error({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionCooldown,
message: `There is a cooldown in effect for this ${commandType} command. It'll be available ${nextAvailable}.`,
context: { remaining }
});
}
rateLimit.consume();
return this.ok();
}
getIdFromMessage(message, context) {
switch (context.scope) {
case require_lib_types_Enums.BucketScope.Global: return "global";
case require_lib_types_Enums.BucketScope.Channel: return message.channelId;
case require_lib_types_Enums.BucketScope.Guild: return message.guildId ?? message.channelId;
default: return message.author.id;
}
}
getIdFromInteraction(interaction, context) {
switch (context.scope) {
case require_lib_types_Enums.BucketScope.Global: return "global";
case require_lib_types_Enums.BucketScope.Channel: return interaction.channelId;
case require_lib_types_Enums.BucketScope.Guild: return interaction.guildId ?? interaction.channelId;
default: return interaction.user.id;
}
}
getManager(command, context) {
let manager = this.buckets.get(command);
if (!manager) {
manager = new __sapphire_ratelimits.RateLimitManager(context.delay, context.limit);
this.buckets.set(command, manager);
}
return manager;
}
};
__sapphire_pieces.container.stores.loadPiece({
name: "Cooldown",
piece: CorePrecondition,
store: "preconditions"
});
//#endregion
exports.CorePrecondition = CorePrecondition;
//# sourceMappingURL=Cooldown.cjs.map