Skip to content

Commit

Permalink
feat: handle args with interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
samfundev committed Sep 29, 2024
1 parent 7462526 commit ca1c42c
Show file tree
Hide file tree
Showing 31 changed files with 146 additions and 91 deletions.
6 changes: 4 additions & 2 deletions src/arguments/CoreBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { container } from '@sapphire/pieces';
import { resolveBoolean } from '../lib/resolvers/boolean';
import { Argument } from '../lib/structures/Argument';
import type { BooleanArgumentContext } from '../lib/types/ArgumentContexts';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';

export class CoreArgument extends Argument<boolean> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'boolean' });
super(context, { name: 'boolean', optionType: ApplicationCommandOptionType.Boolean });
}

public run(parameter: string, context: BooleanArgumentContext): Argument.Result<boolean> {
public run(parameter: string | CommandInteractionOption, context: BooleanArgumentContext): Argument.Result<boolean> {
if (typeof parameter !== 'string') return this.ok(parameter.value as boolean);
const resolved = resolveBoolean(parameter, { truths: context.truths, falses: context.falses });
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { ChannelTypes } from '@sapphire/discord.js-utilities';
import { container } from '@sapphire/pieces';
import { resolveChannel } from '../lib/resolvers/channel';
import { Argument } from '../lib/structures/Argument';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';

export class CoreArgument extends Argument<ChannelTypes> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'channel' });
super(context, { name: 'channel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<ChannelTypes> {
public override run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<ChannelTypes> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const resolved = resolveChannel(parameter, context.messageOrInteraction);
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreDMChannel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { container } from '@sapphire/pieces';
import type { DMChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type DMChannel } from 'discord.js';
import { resolveDMChannel } from '../lib/resolvers/dmChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<DMChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'dmChannel' });
super(context, { name: 'dmChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<DMChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<DMChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const resolved = resolveDMChannel(parameter, context.messageOrInteraction);
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreDate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { container } from '@sapphire/pieces';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveDate } from '../lib/resolvers/date';
import { Argument } from '../lib/structures/Argument';
Expand All @@ -11,10 +12,11 @@ export class CoreArgument extends Argument<Date> {
} as const;

public constructor(context: Argument.LoaderContext) {
super(context, { name: 'date' });
super(context, { name: 'date', optionType: ApplicationCommandOptionType.String });
}

public run(parameter: string, context: Argument.Context): Argument.Result<Date> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<Date> {
if (typeof parameter !== 'string') parameter = parameter.value as string;
const resolved = resolveDate(parameter, { minimum: context.minimum, maximum: context.maximum });
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreEmoji.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { container } from '@sapphire/pieces';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';
import { resolveEmoji, type EmojiObject } from '../lib/resolvers/emoji';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<EmojiObject> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'emoji' });
super(context, { name: 'emoji', optionType: ApplicationCommandOptionType.String });
}

public run(parameter: string, context: Argument.Context): Argument.Result<EmojiObject> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<EmojiObject> {
if (typeof parameter !== 'string') parameter = parameter.value as string;
const resolved = resolveEmoji(parameter);
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreEnum.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { container } from '@sapphire/pieces';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';
import { resolveEnum } from '../lib/resolvers/enum';
import { Argument } from '../lib/structures/Argument';
import type { EnumArgumentContext } from '../lib/types/ArgumentContexts';

export class CoreArgument extends Argument<string> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'enum' });
super(context, { name: 'enum', optionType: ApplicationCommandOptionType.String });
}

public run(parameter: string, context: EnumArgumentContext): Argument.Result<string> {
public run(parameter: string | CommandInteractionOption, context: EnumArgumentContext): Argument.Result<string> {
if (typeof parameter !== 'string') parameter = parameter.value as string;
const resolved = resolveEnum(parameter, { enum: context.enum, caseInsensitive: context.caseInsensitive });
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreFloat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { container } from '@sapphire/pieces';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveFloat } from '../lib/resolvers/float';
import { Argument } from '../lib/structures/Argument';
Expand All @@ -11,10 +12,11 @@ export class CoreArgument extends Argument<number> {
} as const;

public constructor(context: Argument.LoaderContext) {
super(context, { name: 'float' });
super(context, { name: 'float', optionType: ApplicationCommandOptionType.Number });
}

public run(parameter: string, context: Argument.Context): Argument.Result<number> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<number> {
if (typeof parameter !== 'string') return this.ok(parameter.value as number);
const resolved = resolveFloat(parameter, { minimum: context.minimum, maximum: context.maximum });
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuild.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { container } from '@sapphire/pieces';
import type { Guild } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type Guild } from 'discord.js';
import { resolveGuild } from '../lib/resolvers/guild';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<Guild> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guild' });
super(context, { name: 'guild', optionType: ApplicationCommandOptionType.String });
}

public async run(parameter: string, context: Argument.Context): Argument.AsyncResult<Guild> {
public async run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.AsyncResult<Guild> {
if (typeof parameter !== 'string') parameter = parameter.value as string;
const resolved = await resolveGuild(parameter);
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildCategoryChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { CategoryChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CategoryChannel, type CommandInteractionOption } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildCategoryChannel } from '../lib/resolvers/guildCategoryChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<CategoryChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildCategoryChannel' });
super(context, { name: 'guildCategoryChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<CategoryChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<CategoryChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreGuildChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { GuildBasedChannelTypes } from '@sapphire/discord.js-utilities';
import { container } from '@sapphire/pieces';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildChannel } from '../lib/resolvers/guildChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<GuildBasedChannelTypes> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildChannel' });
super(context, { name: 'guildChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<GuildBasedChannelTypes> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<GuildBasedChannelTypes> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildNewsChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { NewsChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type NewsChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildNewsChannel } from '../lib/resolvers/guildNewsChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<NewsChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildNewsChannel' });
super(context, { name: 'guildNewsChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<NewsChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<NewsChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildNewsThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { ThreadChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type ThreadChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildNewsThreadChannel } from '../lib/resolvers/guildNewsThreadChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<ThreadChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildNewsThreadChannel' });
super(context, { name: 'guildNewsThreadChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<ThreadChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<ThreadChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildPrivateThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { ThreadChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type ThreadChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildPrivateThreadChannel } from '../lib/resolvers/guildPrivateThreadChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<ThreadChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildPrivateThreadChannel' });
super(context, { name: 'guildPrivateThreadChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<ThreadChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<ThreadChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildPublicThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { ThreadChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type ThreadChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildPublicThreadChannel } from '../lib/resolvers/guildPublicThreadChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<ThreadChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildPublicThreadChannel' });
super(context, { name: 'guildPublicThreadChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<ThreadChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<ThreadChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildStageVoiceChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { StageChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type StageChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildStageVoiceChannel } from '../lib/resolvers/guildStageVoiceChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<StageChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildStageVoiceChannel' });
super(context, { name: 'guildStageVoiceChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<StageChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<StageChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildTextChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { TextChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type TextChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildTextChannel } from '../lib/resolvers/guildTextChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<TextChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildTextChannel' });
super(context, { name: 'guildTextChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<TextChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<TextChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { container } from '@sapphire/pieces';
import type { ThreadChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type ThreadChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildThreadChannel } from '../lib/resolvers/guildThreadChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<ThreadChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildThreadChannel' });
super(context, { name: 'guildThreadChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<ThreadChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<ThreadChannel> {
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand All @@ -20,6 +20,7 @@ export class CoreArgument extends Argument<ThreadChannel> {
});
}

if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const resolved = resolveGuildThreadChannel(parameter, guild);
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
7 changes: 4 additions & 3 deletions src/arguments/CoreGuildVoiceChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { container } from '@sapphire/pieces';
import type { VoiceChannel } from 'discord.js';
import { ApplicationCommandOptionType, type CommandInteractionOption, type VoiceChannel } from 'discord.js';
import { Identifiers } from '../lib/errors/Identifiers';
import { resolveGuildVoiceChannel } from '../lib/resolvers/guildVoiceChannel';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<VoiceChannel> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'guildVoiceChannel' });
super(context, { name: 'guildVoiceChannel', optionType: ApplicationCommandOptionType.Channel });
}

public run(parameter: string, context: Argument.Context): Argument.Result<VoiceChannel> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<VoiceChannel> {
if (typeof parameter !== 'string') parameter = parameter.channel!.id;
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
Expand Down
6 changes: 4 additions & 2 deletions src/arguments/CoreHyperlink.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { container } from '@sapphire/pieces';
import { ApplicationCommandOptionType, type CommandInteractionOption } from 'discord.js';
import type { URL } from 'node:url';
import { resolveHyperlink } from '../lib/resolvers/hyperlink';
import { Argument } from '../lib/structures/Argument';

export class CoreArgument extends Argument<URL> {
public constructor(context: Argument.LoaderContext) {
super(context, { name: 'hyperlink', aliases: ['url'] });
super(context, { name: 'hyperlink', aliases: ['url'], optionType: ApplicationCommandOptionType.String });
}

public run(parameter: string, context: Argument.Context): Argument.Result<URL> {
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<URL> {
if (typeof parameter !== 'string') parameter = parameter.value as string;
const resolved = resolveHyperlink(parameter);
return resolved.mapErrInto((identifier) =>
this.error({
Expand Down
Loading

0 comments on commit ca1c42c

Please sign in to comment.