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

feat: add interaction support to args #728

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
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
8 changes: 5 additions & 3 deletions src/arguments/CoreChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ 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> {
const resolved = resolveChannel(parameter, context.message);
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreDMChannel.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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> {
const resolved = resolveDMChannel(parameter, context.message);
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({
parameter,
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
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildCategoryChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
8 changes: 5 additions & 3 deletions src/arguments/CoreGuildChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildNewsChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildNewsThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildPrivateThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildPublicThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildStageVoiceChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildTextChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildThreadChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +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 { 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> {
const { guild } = context.message;
public run(parameter: string | CommandInteractionOption, context: Argument.Context): Argument.Result<ThreadChannel> {
const { guild } = context.messageOrInteraction;
if (!guild) {
return this.error({
parameter,
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
9 changes: 5 additions & 4 deletions src/arguments/CoreGuildVoiceChannel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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> {
const { guild } = context.message;
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({
parameter,
Expand Down
Loading
Loading