From d37015a617882083a1cb6e3a233faee9a4de1570 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Sun, 8 Aug 2021 03:20:28 +0200 Subject: [PATCH] added ReplyMessageOptions to commands as a return value --- src/Command.ts | 35 +++++++++++++++++++++++++++++++---- src/types.ts | 14 ++++++++++++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Command.ts b/src/Command.ts index 620596f..95f3537 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -3,9 +3,9 @@ import { Permissions, Message, MessageEmbed, - Guild, CommandInteraction, GuildMember, + ReplyMessageOptions, } from "discord.js"; import { CommandBuilder, PermissionCheckTypes } from "./types.js"; import { PermissionsError } from "./errors.js"; @@ -26,7 +26,12 @@ export class Command { private function: ( interaction?: Message | CommandInteraction, cmdParams?: InputParameter[] - ) => void | string | MessageEmbed | Promise; + ) => + | void + | string + | MessageEmbed + | ReplyMessageOptions + | Promise; /** * Command constructor @@ -93,7 +98,15 @@ export class Command { : memberPermissions.any(this.permissions, true)) ) { const fnResult = await this.function(interaction, cmdParams); - if (typeof fnResult == "string") { + if ( + "content" in (fnResult as ReplyMessageOptions) || + "embeds" in (fnResult as ReplyMessageOptions) || + "files" in (fnResult as ReplyMessageOptions) || + "components" in (fnResult as ReplyMessageOptions) || + "sticker" in (fnResult as ReplyMessageOptions) + ) { + await interaction.reply(fnResult as ReplyMessageOptions); + } else if (typeof fnResult == "string") { await interaction?.reply(fnResult); } else if (fnResult instanceof MessageEmbed) { await interaction?.channel.send({ embeds: [fnResult] }); @@ -126,7 +139,21 @@ export class Command { cmdParams ); try { - if (typeof fnResult == "string") { + if ( + "content" in (fnResult as ReplyMessageOptions) || + "embeds" in (fnResult as ReplyMessageOptions) || + "files" in (fnResult as ReplyMessageOptions) || + "components" in (fnResult as ReplyMessageOptions) || + "sticker" in (fnResult as ReplyMessageOptions) + ) { + interaction.replied + ? await interaction.editReply( + fnResult as ReplyMessageOptions + ) + : await interaction.reply( + fnResult as ReplyMessageOptions + ); + } else if (typeof fnResult == "string") { interaction.replied ? await interaction.editReply({ content: fnResult, diff --git a/src/types.ts b/src/types.ts index 115ea6d..5a6689e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,9 @@ -import { ClientOptions, ColorResolvable, CommandInteraction } from "discord.js"; +import { + ClientOptions, + ColorResolvable, + CommandInteraction, + ReplyMessageOptions, +} from "discord.js"; import { InputParameter, ParameterSchema } from "./Parameter.js"; import { PermissionResolvable, Message, MessageEmbed } from "discord.js"; import type { Command } from "./Command.js"; @@ -27,7 +32,12 @@ export interface CommandBuilder { function: ( interaction?: Message | CommandInteraction, cmdParams?: InputParameter[] - ) => void | string | MessageEmbed | Promise; + ) => + | void + | string + | MessageEmbed + | ReplyMessageOptions + | Promise; } export interface PhraseOccurrenceData { command: Command;