How can I bypass required user permissions?

I want developers to be able to bypass them I can probably override the parseConstructorPreConditionsRequiredUserPermissions method so it adds an extended/custom precondition but that's my last resort since it feels hacky
Solution:
we dont have an option for that
Jump to solution
6 Replies
Silver
SilverOP•2y ago
that's for cooldowns 😄 I already have that set up actually
Favna
Favna•2y ago
oh permissions mb
Solution
Favna
Favna•2y ago
we dont have an option for that
Silver
SilverOP•2y ago
np, I'll extend the class and do it like that, thanks
Silver
SilverOP•2y ago
in case anyone is wondering how I implemented it in the future: CustomPreconditionContainerSingle.ts
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)
}
}
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)
}
}
CustomCommand.ts
export abstract class CustomCommand extends Command {
// Implementation to bypass required user permissions
protected override parseConstructorPreConditionsRequiredUserPermissions(options: Command.Options) {
const permissions = new PermissionsBitField(options.requiredUserPermissions)
if (permissions.bitfield !== 0n) {
const precondition = new CustomPreconditionContainerSingle({
name: 'UserPermissions',
context: { permissions }
})
this.preconditions.add(precondition)
}
}
}
export abstract class CustomCommand extends Command {
// Implementation to bypass required user permissions
protected override parseConstructorPreConditionsRequiredUserPermissions(options: Command.Options) {
const permissions = new PermissionsBitField(options.requiredUserPermissions)
if (permissions.bitfield !== 0n) {
const precondition = new CustomPreconditionContainerSingle({
name: 'UserPermissions',
context: { permissions }
})
this.preconditions.add(precondition)
}
}
}
Want results from more Discord servers?
Add your server