Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preconditions): properly check for null in ClientPermissions and UserPermissions #262

Merged
merged 4 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/errors/Identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const enum Identifiers {
PreconditionGuildTextOnly = 'preconditionGuildTextOnly',
PreconditionNSFW = 'preconditionNsfw',
PreconditionClientPermissions = 'preconditionClientPermissions',
PreconditionClientPermissionsNoClient = 'preconditionClientPermissionsNoClient',
PreconditionClientPermissionsNoPermissions = 'preconditionClientPermissionsNoPermissions',
PreconditionUserPermissions = 'preconditionUserPermissions',
PreconditionUserPermissionsNoPermissions = 'preconditionUserPermissionsNoPermissions',
PreconditionThreadOnly = 'preconditionThreadOnly'
}
17 changes: 16 additions & 1 deletion src/preconditions/ClientPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ export class CorePrecondition extends Precondition {
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;
if (!message.client.id) {
return this.error({
identifier: Identifiers.PreconditionClientPermissionsNoClient,
message: 'There was no client to validate the permissions for.'
});
}

const permissions = message.guild ? channel.permissionsFor(message.client.id) : this.dmChannelPermissions;

if (!permissions) {
return this.error({
identifier: Identifiers.PreconditionClientPermissionsNoPermissions,
message: 'I was unable to resolve my permissions in the command invocation channel.'
});
}

const missing = permissions.missing(required);
return missing.length === 0
? this.ok()
Expand Down
10 changes: 9 additions & 1 deletion src/preconditions/UserPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ export class CorePrecondition extends Precondition {
const required = (context.permissions as Permissions) ?? new Permissions();
const channel = message.channel as TextChannel | NewsChannel;

const permissions = message.guild ? channel.permissionsFor(message.author)! : this.dmChannelPermissions;
const permissions = message.guild ? channel.permissionsFor(message.author) : this.dmChannelPermissions;

if (!permissions) {
return this.error({
identifier: Identifiers.PreconditionClientPermissionsNoPermissions,
message: "I was unable to resolve the end-user's permissions in the command invocation channel."
});
}

const missing = permissions.missing(required);
return missing.length === 0
? this.ok()
Expand Down