Skip to content
Merged
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
23 changes: 15 additions & 8 deletions src/preconditions/ClientPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ export class CorePrecondition extends AllFlowsPrecondition {
): AllFlowsPrecondition.AsyncResult {
const required = context.permissions ?? new PermissionsBitField();

const channel = await this.fetchChannelFromInteraction(interaction);
const availablePermissions = await this.getAvailablePermissions(interaction);

const permissions = await this.getPermissionsForChannel(channel, interaction);

return this.sharedRun(required, permissions, 'chat input');
return this.sharedRun(required, availablePermissions, 'chat input');
}

public async contextMenuRun(
Expand All @@ -69,11 +67,9 @@ export class CorePrecondition extends AllFlowsPrecondition {
): AllFlowsPrecondition.AsyncResult {
const required = context.permissions ?? new PermissionsBitField();

const channel = await this.fetchChannelFromInteraction(interaction);

const permissions = await this.getPermissionsForChannel(channel, interaction);
const availablePermissions = await this.getAvailablePermissions(interaction);

return this.sharedRun(required, permissions, 'context menu');
return this.sharedRun(required, availablePermissions, 'context menu');
}

private async getPermissionsForChannel(channel: TextBasedChannel, messageOrInteraction: Message | BaseInteraction) {
Expand Down Expand Up @@ -113,6 +109,17 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

private async getAvailablePermissions(interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction) {
if (interaction.channel) {
if (interaction.channel.isDMBased()) return this.dmChannelPermissions;

const channel = await this.fetchChannelFromInteraction(interaction);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fetch feels redundant now

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better safe than sorry and it's gonna come out of the cache anyway if it's there.

return this.getPermissionsForChannel(channel, interaction);
}

return interaction.appPermissions;
}

public static readonly readablePermissions: Record<PermissionsString, string> = {
AddReactions: 'Add Reactions',
Administrator: 'Administrator',
Expand Down