-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add UserPermissions precondition (#252)
- Loading branch information
Showing
8 changed files
with
128 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/lib/utils/preconditions/containers/ClientPermissionsPrecondition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { PermissionResolvable, Permissions } from 'discord.js'; | ||
import type { PreconditionSingleResolvableDetails } from '../PreconditionContainerSingle'; | ||
|
||
/** | ||
* Constructs a contextful permissions precondition requirement. | ||
* @since 1.0.0 | ||
* @example | ||
* ```typescript | ||
* export class CoreCommand extends Command { | ||
* public constructor(context: PieceContext) { | ||
* super(context, { | ||
* preconditions: [ | ||
* 'GuildOnly', | ||
* new ClientPermissionsPrecondition('ADD_REACTIONS') | ||
* ] | ||
* }); | ||
* } | ||
* | ||
* public run(message: Message, args: Args) { | ||
* // ... | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export class ClientPermissionsPrecondition implements PreconditionSingleResolvableDetails<'ClientPermissions'> { | ||
public name: 'ClientPermissions'; | ||
public context: { permissions: Permissions }; | ||
|
||
/** | ||
* Constructs a precondition container entry. | ||
* @param permissions The permissions that will be required by this command. | ||
*/ | ||
public constructor(permissions: PermissionResolvable) { | ||
this.name = 'ClientPermissions'; | ||
this.context = { | ||
permissions: new Permissions(permissions) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Message, NewsChannel, Permissions, TextChannel } from 'discord.js'; | ||
import { Identifiers } from '../lib/errors/Identifiers'; | ||
import type { Command } from '../lib/structures/Command'; | ||
import { Precondition, PreconditionContext, PreconditionResult } from '../lib/structures/Precondition'; | ||
import { CorePrecondition as ClientPermissionsPrecondition } from './ClientPermissions'; | ||
|
||
export class CorePrecondition extends Precondition { | ||
private readonly dmChannelPermissions = new Permissions( | ||
~new Permissions([ | ||
'ADD_REACTIONS', | ||
'ATTACH_FILES', | ||
'EMBED_LINKS', | ||
'READ_MESSAGE_HISTORY', | ||
'SEND_MESSAGES', | ||
'USE_EXTERNAL_EMOJIS', | ||
'VIEW_CHANNEL', | ||
'USE_EXTERNAL_STICKERS', | ||
'MENTION_EVERYONE' | ||
]).bitfield & Permissions.ALL | ||
).freeze(); | ||
|
||
public run(message: Message, _command: Command, context: PreconditionContext): PreconditionResult { | ||
const required = (context.permissions as Permissions) ?? new Permissions(); | ||
const channel = message.channel as TextChannel | NewsChannel; | ||
|
||
const permissions = message.guild ? channel.permissionsFor(message.client.id!)! : this.dmChannelPermissions; | ||
const missing = permissions.missing(required); | ||
return missing.length === 0 | ||
? this.ok() | ||
: this.error({ | ||
identifier: Identifiers.PreconditionUserPermissions, | ||
message: `You are missing the following permissions to run this command: ${missing | ||
.map((perm) => ClientPermissionsPrecondition.readablePermissions[perm]) | ||
.join(', ')}`, | ||
context: { missing } | ||
}); | ||
} | ||
} |