Skip to content

Commit

Permalink
refactor: cleanup types and ensure proper imports in dist (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Aug 16, 2022
1 parent 694bd88 commit 7dd13af
Show file tree
Hide file tree
Showing 22 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bump": "cliff-jumper",
"check-update": "cliff-jumper --dry-run",
"postinstall": "husky install .github/husky",
"prepack": "rollup-type-bundler -v -e url events && pinst --disable",
"prepack": "rollup-type-bundler -v -e node:url node:events && pinst --disable",
"postpack": "pinst --enable"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/CoreHyperlink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import type { URL } from 'url';
import type { URL } from 'node:url';
import { resolveHyperlink } from '../lib/resolvers';
import { Argument } from '../lib/structures/Argument';

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { CorePrecondition as GuildOnly } from './preconditions/GuildOnly';
import { CorePrecondition as GuildPrivateThreadOnly } from './preconditions/GuildPrivateThreadOnly';
import { CorePrecondition as GuildPublicThreadOnly } from './preconditions/GuildPublicThreadOnly';
import { CorePrecondition as GuildTextOnly } from './preconditions/GuildTextOnly';
import { CorePrecondition as GuildVoiceOnly } from './preconditions/GuildVoiceOnly';
import { CorePrecondition as GuildThreadOnly } from './preconditions/GuildThreadOnly';
import { CorePrecondition as GuildVoiceOnly } from './preconditions/GuildVoiceOnly';
import { CorePrecondition as NSFW } from './preconditions/NSFW';
import { CorePrecondition as UserPermissions } from './preconditions/UserPermissions';

Expand Down
6 changes: 3 additions & 3 deletions src/lib/parsers/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
User,
VoiceChannel
} from 'discord.js';
import type { URL } from 'url';
import type { URL } from 'node:url';
import { ArgumentError } from '../errors/ArgumentError';
import { Identifiers } from '../errors/Identifiers';
import { UserError } from '../errors/UserError';
Expand Down Expand Up @@ -640,7 +640,7 @@ export class Args {
return { message: this.message, command: this.command, commandContext: this.commandContext };
}

protected unavailableArgument<T>(type: string | IArgument<T>) {
protected unavailableArgument<T>(type: string | IArgument<T>): Result.Err<UserError> {
const name = typeof type === 'string' ? type : type.name;
return Result.err(
new UserError({
Expand All @@ -651,7 +651,7 @@ export class Args {
);
}

protected missingArguments() {
protected missingArguments(): Result.Err<UserError> {
return Result.err(new UserError({ identifier: Identifiers.ArgsMissing, message: 'There are no more arguments.', context: this.toJSON() }));
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/resolvers/hyperlink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Result } from '@sapphire/result';
import { URL } from 'url';
import { URL } from 'node:url';
import { Identifiers } from '../errors/Identifiers';

export function resolveHyperlink(parameter: string): Result<URL, Identifiers.ArgumentHyperlinkError> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/structures/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface IArgument<T> {
* ```typescript
* // TypeScript:
* import { Argument, PieceContext } from '@sapphire/framework';
* import { URL } from 'url';
* import { URL } from 'node:url';
*
* // Define a class extending `Argument`, then export it.
* // NOTE: You can use `export default` or `export =` too.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/structures/Listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Piece } from '@sapphire/pieces';
import { Result } from '@sapphire/result';
import type { Client, ClientEvents } from 'discord.js';
import type { EventEmitter } from 'events';
import type { EventEmitter } from 'node:events';
import { Events } from '../types/Events';

/**
Expand Down
10 changes: 7 additions & 3 deletions src/preconditions/ClientPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
]).bitfield & Permissions.ALL
).freeze();

public messageRun(message: Message, _: Command, context: PermissionPreconditionContext) {
public messageRun(message: Message, _: Command, context: PermissionPreconditionContext): AllFlowsPrecondition.Result {
const required = context.permissions ?? new Permissions();
const channel = message.channel as BaseGuildTextChannel;

Expand All @@ -45,7 +45,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
return this.sharedRun(required, permissions, 'message');
}

public async chatInputRun(interaction: CommandInteraction, _: Command, context: PermissionPreconditionContext) {
public async chatInputRun(interaction: CommandInteraction, _: Command, context: PermissionPreconditionContext): AllFlowsPrecondition.AsyncResult {
const required = context.permissions ?? new Permissions();

const channel = (await this.fetchChannelFromInteraction(interaction)) as GuildTextBasedChannel;
Expand All @@ -55,7 +55,11 @@ export class CorePrecondition extends AllFlowsPrecondition {
return this.sharedRun(required, permissions, 'chat input');
}

public async contextMenuRun(interaction: ContextMenuInteraction, _: Command, context: PermissionPreconditionContext) {
public async contextMenuRun(
interaction: ContextMenuInteraction,
_: Command,
context: PermissionPreconditionContext
): AllFlowsPrecondition.AsyncResult {
const required = context.permissions ?? new Permissions();
const channel = (await this.fetchChannelFromInteraction(interaction)) as GuildTextBasedChannel;

Expand Down
14 changes: 10 additions & 4 deletions src/preconditions/Cooldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ export interface CooldownPreconditionContext extends PreconditionContext {
export class CorePrecondition extends AllFlowsPrecondition {
public buckets = new WeakMap<Command, RateLimitManager<string>>();

public messageRun(message: Message, command: Command, context: CooldownPreconditionContext) {
public messageRun(message: Message, command: Command, context: CooldownPreconditionContext): AllFlowsPrecondition.Result {
const cooldownId = this.getIdFromMessage(message, context);

return this.sharedRun(message.author.id, command, context, cooldownId, 'message');
}

public chatInputRun(interaction: CommandInteraction, command: Command, context: CooldownPreconditionContext) {
public chatInputRun(interaction: CommandInteraction, command: Command, context: CooldownPreconditionContext): AllFlowsPrecondition.Result {
const cooldownId = this.getIdFromInteraction(interaction, context);

return this.sharedRun(interaction.user.id, command, context, cooldownId, 'chat input');
}

public contextMenuRun(interaction: ContextMenuInteraction, command: Command, context: CooldownPreconditionContext) {
public contextMenuRun(interaction: ContextMenuInteraction, command: Command, context: CooldownPreconditionContext): AllFlowsPrecondition.Result {
const cooldownId = this.getIdFromInteraction(interaction, context);

return this.sharedRun(interaction.user.id, command, context, cooldownId, 'context menu');
}

private sharedRun(authorId: string, command: Command, context: CooldownPreconditionContext, cooldownId: string, commandType: string) {
private sharedRun(
authorId: string,
command: Command,
context: CooldownPreconditionContext,
cooldownId: string,
commandType: string
): AllFlowsPrecondition.Result {
// If the command it is testing for is not this one, return ok:
if (context.external) return this.ok();

Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/DMOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return message.guild === null
? this.ok()
: this.error({ identifier: Identifiers.PreconditionDMOnly, message: 'You cannot run this message command outside DMs.' });
}

public chatInputRun(interaction: CommandInteraction) {
public chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.Result {
return interaction.guildId === null
? this.ok()
: this.error({ identifier: Identifiers.PreconditionDMOnly, message: 'You cannot run this chat input command outside DMs.' });
}

public contextMenuRun(interaction: ContextMenuInteraction) {
public contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.Result {
return interaction.guildId === null
? this.ok()
: this.error({ identifier: Identifiers.PreconditionDMOnly, message: 'You cannot run this context menu command outside DMs.' });
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/Enabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ export class CorePrecondition extends AllFlowsPrecondition {
super(context, { position: 10 });
}

public messageRun(_: Message, command: Command, context: AllFlowsPrecondition.Context) {
public messageRun(_: Message, command: Command, context: AllFlowsPrecondition.Context): AllFlowsPrecondition.Result {
return command.enabled
? this.ok()
: this.error({ identifier: Identifiers.CommandDisabled, message: 'This message command is disabled.', context });
}

public chatInputRun(_: CommandInteraction, command: Command, context: AllFlowsPrecondition.Context) {
public chatInputRun(_: CommandInteraction, command: Command, context: AllFlowsPrecondition.Context): AllFlowsPrecondition.Result {
return command.enabled
? this.ok()
: this.error({ identifier: Identifiers.CommandDisabled, message: 'This chat input command is disabled.', context });
}

public contextMenuRun(_: ContextMenuInteraction, command: Command, context: AllFlowsPrecondition.Context) {
public contextMenuRun(_: ContextMenuInteraction, command: Command, context: AllFlowsPrecondition.Context): AllFlowsPrecondition.Result {
return command.enabled
? this.ok()
: this.error({ identifier: Identifiers.CommandDisabled, message: 'This context menu command is disabled.', context });
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildNewsOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AllFlowsPrecondition } from '../lib/structures/Precondition';
export class CorePrecondition extends AllFlowsPrecondition {
private readonly allowedTypes: TextBasedChannelTypes[] = ['GUILD_NEWS', 'GUILD_NEWS_THREAD'];

public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return this.allowedTypes.includes(message.channel.type)
? this.ok()
: this.error({
Expand All @@ -14,7 +14,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async chatInputRun(interaction: CommandInteraction) {
public async chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return this.allowedTypes.includes(channel.type)
Expand All @@ -25,7 +25,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
public async contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return this.allowedTypes.includes(channel.type)
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildNewsThreadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return message.thread?.type === 'GUILD_NEWS_THREAD'
? this.ok()
: this.error({
Expand All @@ -12,7 +12,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async chatInputRun(interaction: CommandInteraction) {
public async chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.type === 'GUILD_NEWS_THREAD'
Expand All @@ -23,7 +23,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
public async contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.type === 'GUILD_NEWS_THREAD'
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return message.guildId === null
? this.error({ identifier: Identifiers.PreconditionGuildOnly, message: 'You cannot run this message command in DMs.' })
: this.ok();
}

public chatInputRun(interaction: CommandInteraction) {
public chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.Result {
return interaction.guildId === null
? this.error({ identifier: Identifiers.PreconditionGuildOnly, message: 'You cannot run this chat input command in DMs.' })
: this.ok();
}

public contextMenuRun(interaction: ContextMenuInteraction) {
public contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.Result {
return interaction.guildId === null
? this.error({ identifier: Identifiers.PreconditionGuildOnly, message: 'You cannot run this context menu command in DMs.' })
: this.ok();
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildPrivateThreadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return message.thread?.type === 'GUILD_PRIVATE_THREAD'
? this.ok()
: this.error({
Expand All @@ -12,7 +12,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async chatInputRun(interaction: CommandInteraction) {
public async chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.type === 'GUILD_PRIVATE_THREAD'
Expand All @@ -23,7 +23,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
public async contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.type === 'GUILD_PRIVATE_THREAD'
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildPublicThreadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return message.thread?.type === 'GUILD_PUBLIC_THREAD'
? this.ok()
: this.error({
Expand All @@ -12,7 +12,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async chatInputRun(interaction: CommandInteraction) {
public async chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.type === 'GUILD_PUBLIC_THREAD'
Expand All @@ -23,7 +23,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
public async contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.type === 'GUILD_PUBLIC_THREAD'
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildTextOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AllFlowsPrecondition } from '../lib/structures/Precondition';
export class CorePrecondition extends AllFlowsPrecondition {
private readonly allowedTypes: TextBasedChannelTypes[] = ['GUILD_TEXT', 'GUILD_PUBLIC_THREAD', 'GUILD_PRIVATE_THREAD'];

public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return this.allowedTypes.includes(message.channel.type)
? this.ok()
: this.error({
Expand All @@ -14,7 +14,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async chatInputRun(interaction: CommandInteraction) {
public async chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return this.allowedTypes.includes(channel.type)
Expand All @@ -25,7 +25,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
public async contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return this.allowedTypes.includes(channel.type)
Expand Down
6 changes: 3 additions & 3 deletions src/preconditions/GuildThreadOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Identifiers } from '../lib/errors/Identifiers';
import { AllFlowsPrecondition } from '../lib/structures/Precondition';

export class CorePrecondition extends AllFlowsPrecondition {
public messageRun(message: Message) {
public messageRun(message: Message): AllFlowsPrecondition.Result {
return message.thread
? this.ok()
: this.error({
Expand All @@ -12,7 +12,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async chatInputRun(interaction: CommandInteraction) {
public async chatInputRun(interaction: CommandInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.isThread()
Expand All @@ -23,7 +23,7 @@ export class CorePrecondition extends AllFlowsPrecondition {
});
}

public async contextMenuRun(interaction: ContextMenuInteraction) {
public async contextMenuRun(interaction: ContextMenuInteraction): AllFlowsPrecondition.AsyncResult {
const channel = await this.fetchChannelFromInteraction(interaction);

return channel.isThread()
Expand Down
Loading

0 comments on commit 7dd13af

Please sign in to comment.