Skip to content

Commit 6bbe2c1

Browse files
committedOct 16, 2023
refactor: expose runInTypeIsSpecificsObject as public static method on Command
1 parent f2911a7 commit 6bbe2c1

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed
 

‎src/lib/structures/Command.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ import type { CommandStore } from './CommandStore';
2323

2424
const ChannelTypes = Object.values(ChannelType).filter((type) => typeof type === 'number') as readonly ChannelType[];
2525
const GuildChannelTypes = ChannelTypes.filter((type) => type !== ChannelType.DM && type !== ChannelType.GroupDM) as readonly ChannelType[];
26-
function runInTypeIsSpecificsObject(types: Command.Options['runIn']): types is CommandSpecificRunIn {
27-
if (!isObject(types)) {
28-
return false;
29-
}
30-
31-
const specificTypes = types as CommandSpecificRunIn;
32-
return Boolean(specificTypes.chatInputRun || specificTypes.messageRun || specificTypes.contextMenuRun);
33-
}
3426

3527
export class Command<PreParseReturn = Args, O extends Command.Options = Command.Options> extends AliasPiece<O> {
3628
/**
@@ -372,7 +364,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
372364
// Early return if there's no runIn option:
373365
if (isNullish(options.runIn)) return;
374366

375-
if (runInTypeIsSpecificsObject(options.runIn)) {
367+
if (Command.runInTypeIsSpecificsObject(options.runIn)) {
376368
const messageRunTypes = this.resolveConstructorPreConditionsRunType(options.runIn.messageRun);
377369
const chatInputRunTypes = this.resolveConstructorPreConditionsRunType(options.runIn.chatInputRun);
378370
const contextMenuRunTypes = this.resolveConstructorPreConditionsRunType(options.runIn.contextMenuRun);
@@ -497,6 +489,15 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
497489
// Return the resolved types in ascending order:
498490
return [...resolved].sort((a, b) => a - b);
499491
}
492+
493+
public static runInTypeIsSpecificsObject(types: Command.Options['runIn']): types is CommandSpecificRunIn {
494+
if (!isObject(types)) {
495+
return false;
496+
}
497+
498+
const specificTypes = types as CommandSpecificRunIn;
499+
return Boolean(specificTypes.chatInputRun || specificTypes.messageRun || specificTypes.contextMenuRun);
500+
}
500501
}
501502

502503
export type MessageCommand = Command & Required<Pick<Command, 'messageRun'>>;

‎src/preconditions/RunIn.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { ChannelType, ChatInputCommandInteraction, ContextMenuCommandInteraction, Message } from 'discord.js';
1+
import type { ChatInputCommandInteraction, ContextMenuCommandInteraction, Message } from 'discord.js';
22
import { Identifiers } from '../lib/errors/Identifiers';
3-
import type { ChatInputCommand, ContextMenuCommand, MessageCommand } from '../lib/structures/Command';
4-
import { AllFlowsPrecondition, type Preconditions, type RunInPreconditionCommandSpecificData } from '../lib/structures/Precondition';
3+
import { Command, type ChatInputCommand, type ContextMenuCommand, type MessageCommand } from '../lib/structures/Command';
4+
import { AllFlowsPrecondition, type Preconditions } from '../lib/structures/Precondition';
55

66
export interface RunInPreconditionContext extends AllFlowsPrecondition.Context {
77
types?: Preconditions['RunIn']['types'];
@@ -13,11 +13,11 @@ export class CorePrecondition extends AllFlowsPrecondition {
1313

1414
const channelType = message.channel.type;
1515

16-
if (typesIsArray(context.types)) {
17-
return context.types.includes(channelType) ? this.ok() : this.makeSharedError(context);
16+
if (Command.runInTypeIsSpecificsObject(context.types)) {
17+
return context.types.messageRun.includes(channelType) ? this.ok() : this.makeSharedError(context);
1818
}
1919

20-
return context.types.messageRun.includes(channelType) ? this.ok() : this.makeSharedError(context);
20+
return context.types.includes(channelType) ? this.ok() : this.makeSharedError(context);
2121
}
2222

2323
public override async chatInputRun(
@@ -29,11 +29,11 @@ export class CorePrecondition extends AllFlowsPrecondition {
2929

3030
const channelType = (await this.fetchChannelFromInteraction(interaction)).type;
3131

32-
if (typesIsArray(context.types)) {
33-
return context.types.includes(channelType) ? this.ok() : this.makeSharedError(context);
32+
if (Command.runInTypeIsSpecificsObject(context.types)) {
33+
return context.types.chatInputRun.includes(channelType) ? this.ok() : this.makeSharedError(context);
3434
}
3535

36-
return context.types.chatInputRun.includes(channelType) ? this.ok() : this.makeSharedError(context);
36+
return context.types.includes(channelType) ? this.ok() : this.makeSharedError(context);
3737
}
3838

3939
public override async contextMenuRun(
@@ -45,11 +45,11 @@ export class CorePrecondition extends AllFlowsPrecondition {
4545

4646
const channelType = (await this.fetchChannelFromInteraction(interaction)).type;
4747

48-
if (typesIsArray(context.types)) {
49-
return context.types.includes(channelType) ? this.ok() : this.makeSharedError(context);
48+
if (Command.runInTypeIsSpecificsObject(context.types)) {
49+
return context.types.contextMenuRun.includes(channelType) ? this.ok() : this.makeSharedError(context);
5050
}
5151

52-
return context.types.contextMenuRun.includes(channelType) ? this.ok() : this.makeSharedError(context);
52+
return context.types.includes(channelType) ? this.ok() : this.makeSharedError(context);
5353
}
5454

5555
private makeSharedError(context: RunInPreconditionContext): AllFlowsPrecondition.Result {
@@ -60,7 +60,3 @@ export class CorePrecondition extends AllFlowsPrecondition {
6060
});
6161
}
6262
}
63-
64-
function typesIsArray(types: readonly ChannelType[] | RunInPreconditionCommandSpecificData): types is readonly ChannelType[] {
65-
return Array.isArray(types);
66-
}

0 commit comments

Comments
 (0)
Please sign in to comment.