export class CustomPreconditionContainerSingle extends PreconditionContainerSingle {
/** Same as {@link CooldownOptions.filteredUsers} but for preconditions. */
private readonly filterUsers: Snowflake[] = [/* TODO: add user IDs here */]
public override messageRun(message: Message<boolean>, command: MessageCommand, context?: PreconditionContext | undefined): PreconditionResult {
const precondition = container.stores.get('preconditions').get(this.name)
if (precondition && this.checkBypass(message, precondition)) return precondition.ok()
return super.messageRun(message, command, context)
}
// Check if it should get bypassed before running the native method
public override chatInputRun(
interaction: ChatInputCommandInteraction<CacheType>,
command: ChatInputCommand,
context?: PreconditionContext | undefined
): PreconditionResult {
const precondition = container.stores.get('preconditions').get(this.name)
if (precondition && this.checkBypass(interaction, precondition)) return precondition.ok()
return super.chatInputRun(interaction, command, context)
}
public override contextMenuRun(
interaction: ContextMenuCommandInteraction<CacheType>,
command: ContextMenuCommand,
context?: PreconditionContext | undefined
): PreconditionResult {
const precondition = container.stores.get('preconditions').get(this.name)
if (precondition && this.checkBypass(interaction, precondition)) return precondition.ok()
return super.contextMenuRun(interaction, command, context)
}
/** Returns whether the precondition should get bypassed. */
private checkBypass(messageOrInteraction: MessageOrInteraction, precondition: Precondition<PreconditionOptions>) {
const isMessage = messageOrInteraction instanceof Message
const author = isMessage ? messageOrInteraction.author : messageOrInteraction.user
// Bypass required user permissions
return precondition.name === 'UserPermissions' && this.filterUsers.includes(author.id)
}
}