Permission or Role check with decorators

is it possible with sapphire something like if user has role with id "1098270502058995842" or if user has role "Moderator(Object)"
Solution:
```ts import type { Message } from 'discord.js'; import { createFunctionPrecondition } from '@sapphire/decorators'; export function RequiresRoleAccess(): MethodDecorator {...
Jump to solution
14 Replies
Favna
Favna2y ago
we don't have a built in decorator for it but you can totally write your custom one yes
fenish
fenishOP2y ago
ah like preconditions ?
Solution
Favna
Favna2y ago
import type { Message } from 'discord.js';
import { createFunctionPrecondition } from '@sapphire/decorators';

export function RequiresRoleAccess(): MethodDecorator {
return createFunctionPrecondition((message: Message) => /* Do your checks here */, /* write your fallback function here */);
}
import type { Message } from 'discord.js';
import { createFunctionPrecondition } from '@sapphire/decorators';

export function RequiresRoleAccess(): MethodDecorator {
return createFunctionPrecondition((message: Message) => /* Do your checks here */, /* write your fallback function here */);
}
(replace message: Message with for example interaction: Command.ChatInputCommandInteraction for chat input run)
fenish
fenishOP2y ago
Sapphire Framework
Creating your own preconditions | Sapphire
Just as we did in Creating Commands, we will start by creating a preconditions subdirectory in
fenish
fenishOP2y ago
i just follow this
Favna
Favna2y ago
that's not a decorator
fenish
fenishOP2y ago
anyways thanks yea yea
Favna
Favna2y ago
you asked for decorators omegalul
fenish
fenishOP2y ago
can't i code like this but decorator version what am i talking about
Favna
Favna2y ago
idk you tell me what you're talking about
fenish
fenishOP2y ago
i was surfing at docs and this seems fine maybe in the future i'll try decorators but i just wanted to try this precondition for experience
Favna
Favna2y ago
The way decorators work internally they will never be preconditions. Decorators work by first processing the decorator function, then calling the original function with originalMethod.call(this, ...args). Preconditions however, run explicitly before the messageRun etc is actually called, that is to say, before decorators are even considered.
fenish
fenishOP2y ago
maybe i'll try this too who knows btw is it possible to hide command from member who doesn't have permission
Favna
Favna2y ago
for chat input commands you set setDefaultMemberPermissions in the registry and beyond that it's up to the digression of the server owners. That said, do not let it be the only check, because a server owner can theoretically allow @ everyone to use for example ban and then blame you as a bot developer for the fact that a member just banned the staff.
Want results from more Discord servers?
Add your server