From e644b184f275370a5694906ed6e00f04784a7be3 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 4 May 2023 18:55:55 -0500 Subject: [PATCH] chore: add old context here until i figure out what to do --- src/classic/context.ts | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/classic/context.ts diff --git a/src/classic/context.ts b/src/classic/context.ts new file mode 100644 index 0000000..81597d1 --- /dev/null +++ b/src/classic/context.ts @@ -0,0 +1,91 @@ +import { + ChatInputCommandInteraction, + Client, + InteractionReplyOptions, + Message, + MessageReplyOptions, + Snowflake, + User +} from "discord.js"; +import { CoreContext } from "../core/structures/context"; +import { Result as Either, Ok as Right, Err as Left } from 'ts-results-es'; +import { ReplyOptions } from "../types/handler"; +/** + * @since 1.0.0 + * Provides values shared between + * Message and ChatInputCommandInteraction + */ +export default class Context extends CoreContext { + + get options() { + return this.interaction.options + } + protected constructor(protected ctx: Either) { + super(ctx) + } + + public get id(): Snowflake { + return this.ctx.val.id; + } + + public get channel() { + return this.ctx.val.channel; + } + /** + * If context is holding a message, message.author + * else, interaction.user + */ + public get user(): User { + return safeUnwrap(this.ctx.map(m => m.author).mapErr(i => i.user)); + } + + public get createdTimestamp(): number { + return this.ctx.val.createdTimestamp; + } + + public get guild() { + return this.ctx.val.guild; + } + + public get guildId() { + return this.ctx.val.guildId; + } + /* + * interactions can return APIGuildMember if the guild it is emitted from is not cached + */ + public get member() { + return this.ctx.val.member; + } + + public get client(): Client { + return this.ctx.val.client; + } + + public get inGuild(): boolean { + return this.ctx.val.inGuild(); + } + + public async reply(content: ReplyOptions) { + return safeUnwrap( + this.ctx + .map(m => m.reply(content as string | MessageReplyOptions)) + .mapErr(i => + i.reply(content as string | InteractionReplyOptions).then(() => i.fetchReply()), + ), + ); + } + + static override wrap(wrappable: ChatInputCommandInteraction | Message): Context { + if ('interaction' in wrappable) { + return new Context(Right(wrappable)); + } + return new Context(Left(wrappable)); + } +} + + +function safeUnwrap(res: Either) { + return res.val; +} + +