diff --git a/src/test/cmds/addemoji.ts b/src/structures/cmds/addemoji.ts similarity index 100% rename from src/test/cmds/addemoji.ts rename to src/structures/cmds/addemoji.ts diff --git a/src/test/cmds/eval.ts b/src/structures/cmds/eval.ts similarity index 100% rename from src/test/cmds/eval.ts rename to src/structures/cmds/eval.ts diff --git a/src/test/cmds/join.ts b/src/structures/cmds/join.ts similarity index 100% rename from src/test/cmds/join.ts rename to src/structures/cmds/join.ts diff --git a/src/test/cmds/leave.ts b/src/structures/cmds/leave.ts similarity index 100% rename from src/test/cmds/leave.ts rename to src/structures/cmds/leave.ts diff --git a/src/test/cmds/mentions.ts b/src/structures/cmds/mentions.ts similarity index 100% rename from src/test/cmds/mentions.ts rename to src/structures/cmds/mentions.ts diff --git a/src/test/cmds/ping.ts b/src/structures/cmds/ping.ts similarity index 100% rename from src/test/cmds/ping.ts rename to src/structures/cmds/ping.ts diff --git a/src/test/cmds/userinfo.ts b/src/structures/cmds/userinfo.ts similarity index 100% rename from src/test/cmds/userinfo.ts rename to src/structures/cmds/userinfo.ts diff --git a/src/structures/slash.ts b/src/structures/slash.ts index e8add8e..c3a5712 100644 --- a/src/structures/slash.ts +++ b/src/structures/slash.ts @@ -1,5 +1,6 @@ import { Client } from '../models/client.ts' -import { INTERACTION_CALLBACK } from '../types/endpoint.ts' +import { MessageOption } from '../types/channel.ts' +import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts' import { InteractionData, InteractionPayload, @@ -9,8 +10,18 @@ import { import { Embed } from './embed.ts' import { Guild } from './guild.ts' import { Member } from './member.ts' -import { GuildTextChannel } from './textChannel.ts' +import { Message } from './message.ts' +import { GuildTextChannel, TextChannel } from './textChannel.ts' import { User } from './user.ts' +import { Webhook } from './webhook.ts' + +interface WebhookMessageOptions extends MessageOption { + embeds?: Embed[] + name?: string + avatar?: string +} + +type AllWebhookMessageOptions = string | WebhookMessageOptions export interface InteractionResponse { type?: InteractionResponseType @@ -19,6 +30,12 @@ export interface InteractionResponse { tts?: boolean flags?: number temp?: boolean + allowedMentions?: { + parse?: string + roles?: string[] + users?: string[] + everyone?: boolean + } } export class Interaction { @@ -30,6 +47,7 @@ export class Interaction { channel: GuildTextChannel guild: Guild member: Member + _savedHook?: Webhook constructor( client: Client, @@ -58,6 +76,10 @@ export class Interaction { return this.data.name } + option(name: string): any { + return this.data.options.find((e) => e.name === name)?.value + } + async respond(data: InteractionResponse): Promise { const payload: InteractionResponsePayload = { type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, @@ -69,7 +91,8 @@ export class Interaction { content: data.content ?? '', embeds: data.embeds, tts: data.tts ?? false, - flags: data.temp === true ? 64 : data.flags ?? undefined + flags: data.temp === true ? 64 : data.flags ?? undefined, + allowed_mentions: (data.allowedMentions ?? undefined) as any } : undefined } @@ -81,4 +104,120 @@ export class Interaction { return this } + + async editResponse(data: { + content?: string + embeds?: Embed[] + }): Promise { + const url = WEBHOOK_MESSAGE( + this.client.user?.id as string, + this.token, + '@original' + ) + await this.client.rest.patch(url, { + content: data.content ?? '', + embeds: data.embeds ?? [] + }) + return this + } + + get url(): string { + return `https://discord.com/api/v8/webhooks/${this.client.user?.id}/${this.token}` + } + + async send( + text?: string | AllWebhookMessageOptions, + option?: AllWebhookMessageOptions + ): Promise { + if (typeof text === 'object') { + option = text + text = undefined + } + + if (text === undefined && option === undefined) { + throw new Error('Either text or option is necessary.') + } + + if (option instanceof Embed) + option = { + embeds: [option] + } + + const payload: any = { + content: text, + embeds: + (option as WebhookMessageOptions)?.embed !== undefined + ? [(option as WebhookMessageOptions).embed] + : (option as WebhookMessageOptions)?.embeds !== undefined + ? (option as WebhookMessageOptions).embeds + : undefined, + file: (option as WebhookMessageOptions)?.file, + tts: (option as WebhookMessageOptions)?.tts, + allowed_mentions: (option as WebhookMessageOptions)?.allowedMentions + } + + if ((option as WebhookMessageOptions)?.name !== undefined) { + payload.username = (option as WebhookMessageOptions)?.name + } + + if ((option as WebhookMessageOptions)?.avatar !== undefined) { + payload.avatar = (option as WebhookMessageOptions)?.avatar + } + + if ( + payload.embeds !== undefined && + payload.embeds instanceof Array && + payload.embeds.length > 10 + ) + throw new Error( + `Cannot send more than 10 embeds through Interaction Webhook` + ) + + const resp = await this.client.rest.post(`${this.url}?wait=true`, payload) + + const res = new Message( + this.client, + resp, + (this as unknown) as TextChannel, + (this as unknown) as User + ) + await res.mentions.fromPayload(resp) + return res + } + + async editMessage( + msg: Message | string, + data: { + content?: string + embeds?: Embed[] + file?: any + allowed_mentions?: { + parse?: string + roles?: string[] + users?: string[] + everyone?: boolean + } + } + ): Promise { + await this.client.rest.patch( + WEBHOOK_MESSAGE( + this.client.user?.id as string, + this.token ?? this.client.token, + typeof msg === 'string' ? msg : msg.id + ), + data + ) + return this + } + + async deleteMessage(msg: Message | string): Promise { + await this.client.rest.delete( + WEBHOOK_MESSAGE( + this.client.user?.id as string, + this.token ?? this.client.token, + typeof msg === 'string' ? msg : msg.id + ) + ) + return this + } } diff --git a/src/structures/webhook.ts b/src/structures/webhook.ts index ccc32f6..dd5dc8c 100644 --- a/src/structures/webhook.ts +++ b/src/structures/webhook.ts @@ -12,6 +12,7 @@ import { Message } from './message.ts' import { TextChannel } from './textChannel.ts' import { User } from './user.ts' import { fetchAuto } from 'https://raw.githubusercontent.com/DjDeveloperr/fetch-base64/main/mod.ts' +import { WEBHOOK_MESSAGE } from '../types/endpoint.ts' export interface WebhookMessageOptions extends MessageOption { embeds?: Embed[] @@ -191,4 +192,40 @@ export class Webhook { if (resp.response.status !== 204) return false else return true } + + async editMessage( + message: string | Message, + data: { + content?: string + embeds?: Embed[] + file?: any + allowed_mentions?: { + parse?: string + roles?: string[] + users?: string[] + everyone?: boolean + } + } + ): Promise { + await this.client?.rest.patch( + WEBHOOK_MESSAGE( + this.id, + (this.token ?? this.client.token) as string, + typeof message === 'string' ? message : message.id + ), + data + ) + return this + } + + async deleteMessage(message: string | Message): Promise { + await this.client?.rest.delete( + WEBHOOK_MESSAGE( + this.id, + (this.token ?? this.client.token) as string, + typeof message === 'string' ? message : message.id + ) + ) + return this + } }