diff --git a/src/lib/gateway.ts b/src/lib/gateway.ts index 8229d48..f8cd358 100644 --- a/src/lib/gateway.ts +++ b/src/lib/gateway.ts @@ -14,10 +14,10 @@ import { ActivityType, CommandFuntion, Interaction, - InteractionFunction, - InteractionContext + InteractionFunction } from '../types'; import { Context } from './context'; +import { InteractionContext } from './interaction'; export class Gateway { socket: WebSocket; diff --git a/src/lib/index.ts b/src/lib/index.ts index 5374852..0f230cd 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -2,4 +2,5 @@ export * from './gateway'; export * from './cache'; export * from './guilds'; export * from './context'; -export * from './message'; \ No newline at end of file +export * from './message'; +export * from './interaction'; diff --git a/src/lib/interaction.ts b/src/lib/interaction.ts new file mode 100644 index 0000000..eef604d --- /dev/null +++ b/src/lib/interaction.ts @@ -0,0 +1,61 @@ +import { Client } from ".."; +import { InteractionResponseType } from "../types"; +import { Message } from "./message"; + +export class InteractionContext { + public client: Client; + public interaction: Record; + constructor({ client, interaction }: { client: Client; interaction: Record }) { + this.client = client; + this.interaction = interaction; + } + + ack = (type: InteractionResponseType = InteractionResponseType.Pong, data?: Record) => { + return this.client.api.post(`/interactions/${this.interaction.id}/${this.interaction.token}/callback`, { + type, + data + }); + }; + + reply = (message: Message | string) => { + if (typeof message === 'string') { + message = new Message().setContent(message); + } + return this.ack(InteractionResponseType.Reply, { + ...message + }); + }; + + edit = (message: Message | string) => { + if (typeof message === 'string') { + message = new Message().setContent(message); + } + return this.ack(InteractionResponseType.UpdateMessage, { + ...message + }); + }; + + editOriginal = (message: Message | string) => { + if (typeof message === 'string') { + message = new Message().setContent(message); + } + return this.client.api.patch( + `/webhooks/${this.client.application_id}/${this.interaction.token}/messages/@original`, + { + type: InteractionResponseType.DeferredUpdateMessage, + ...message + } + ); + }; + + deleteOriginal = () => { + return this.client.api.delete( + `/webhooks/${this.client.application_id}/${this.interaction.token}/messages/@original` + ); + }; + + loading = async () => { + await this.ack(InteractionResponseType.DeferredReply); + return new InteractionContext({ client: this.client, interaction: this.interaction }); + }; +} diff --git a/src/types/interaction.ts b/src/types/interaction.ts index 3f243b9..4f4c2b1 100644 --- a/src/types/interaction.ts +++ b/src/types/interaction.ts @@ -1,4 +1,5 @@ -import { Client, Message } from '..'; +import { Client } from '..'; +import { InteractionContext } from '../lib'; export abstract class Interaction { abstract custom_id: string | string[]; @@ -16,61 +17,3 @@ export enum InteractionResponseType { AutocompleteResult = 8, Modal = 9 } - -export class InteractionContext { - public client: Client; - public interaction: Record; - constructor({ client, interaction }: { client: Client; interaction: Record }) { - this.client = client; - this.interaction = interaction; - } - - ack = (type: InteractionResponseType = InteractionResponseType.Pong, data?: Record) => { - return this.client.api.post(`/interactions/${this.interaction.id}/${this.interaction.token}/callback`, { - type, - data - }); - }; - - reply = (message: Message | string) => { - if (typeof message === 'string') { - message = new Message().setContent(message); - } - return this.ack(InteractionResponseType.Reply, { - ...message - }); - }; - - edit = (message: Message | string) => { - if (typeof message === 'string') { - message = new Message().setContent(message); - } - return this.ack(InteractionResponseType.UpdateMessage, { - ...message - }); - }; - - editOriginal = (message: Message | string) => { - if (typeof message === 'string') { - message = new Message().setContent(message); - } - return this.client.api.patch( - `/webhooks/${this.client.application_id}/${this.interaction.token}/messages/@original`, - { - type: InteractionResponseType.DeferredUpdateMessage, - ...message - } - ); - }; - - deleteOriginal = () => { - return this.client.api.delete( - `/webhooks/${this.client.application_id}/${this.interaction.token}/messages/@original` - ); - }; - - loading = async () => { - await this.ack(InteractionResponseType.DeferredReply); - return new InteractionContext({ client: this.client, interaction: this.interaction }); - }; -}