harmony/src/structures/slash.ts

246 lines
6.3 KiB
TypeScript
Raw Normal View History

2020-12-10 04:36:36 +00:00
import { Client } from '../models/client.ts'
import { MessageOptions } from '../types/channel.ts'
2020-12-11 10:17:05 +00:00
import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts'
2020-12-10 04:36:36 +00:00
import {
InteractionData,
2020-12-16 10:42:52 +00:00
InteractionOption,
2020-12-10 04:36:36 +00:00
InteractionPayload,
InteractionResponsePayload,
InteractionResponseType
} from '../types/slash.ts'
import { SnowflakeBase } from './base.ts'
2020-12-10 04:36:36 +00:00
import { Embed } from './embed.ts'
2020-12-10 06:55:52 +00:00
import { Guild } from './guild.ts'
import { Member } from './member.ts'
2020-12-11 10:17:05 +00:00
import { Message } from './message.ts'
import { GuildTextChannel, TextChannel } from './textChannel.ts'
2020-12-10 06:55:52 +00:00
import { User } from './user.ts'
2020-12-11 10:17:05 +00:00
import { Webhook } from './webhook.ts'
interface WebhookMessageOptions extends MessageOptions {
2020-12-11 10:17:05 +00:00
embeds?: Embed[]
name?: string
avatar?: string
}
type AllWebhookMessageOptions = string | WebhookMessageOptions
2020-12-10 04:36:36 +00:00
export interface InteractionResponse {
type?: InteractionResponseType
content?: string
embeds?: Embed[]
tts?: boolean
flags?: number
2020-12-10 10:42:03 +00:00
temp?: boolean
2020-12-11 10:17:05 +00:00
allowedMentions?: {
parse?: string
roles?: string[]
users?: string[]
everyone?: boolean
}
2020-12-10 04:36:36 +00:00
}
export class Interaction extends SnowflakeBase {
2020-12-10 04:36:36 +00:00
client: Client
type: number
token: string
id: string
data: InteractionData
2020-12-10 06:55:52 +00:00
channel: GuildTextChannel
guild: Guild
member: Member
2020-12-11 10:17:05 +00:00
_savedHook?: Webhook
2020-12-10 04:36:36 +00:00
2020-12-10 06:55:52 +00:00
constructor(
client: Client,
data: InteractionPayload,
others: {
channel: GuildTextChannel
guild: Guild
member: Member
}
) {
super(client)
2020-12-10 04:36:36 +00:00
this.client = client
this.type = data.type
this.token = data.token
2020-12-10 06:55:52 +00:00
this.member = others.member
2020-12-10 04:36:36 +00:00
this.id = data.id
this.data = data.data
2020-12-10 06:55:52 +00:00
this.guild = others.guild
this.channel = others.channel
}
get user(): User {
return this.member.user
}
get name(): string {
return this.data.name
2020-12-10 04:36:36 +00:00
}
2020-12-16 10:42:52 +00:00
get options(): InteractionOption[] {
return this.data.options ?? []
}
option<T = any>(name: string): T {
2020-12-16 10:42:52 +00:00
return this.options.find((e) => e.name === name)?.value
2020-12-11 10:17:05 +00:00
}
2020-12-16 10:42:52 +00:00
/** Respond to an Interaction */
2020-12-10 04:36:36 +00:00
async respond(data: InteractionResponse): Promise<Interaction> {
const payload: InteractionResponsePayload = {
type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data:
data.type === undefined ||
data.type === InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE ||
data.type === InteractionResponseType.CHANNEL_MESSAGE
? {
content: data.content ?? '',
embeds: data.embeds,
tts: data.tts ?? false,
2020-12-11 10:17:05 +00:00
flags: data.temp === true ? 64 : data.flags ?? undefined,
allowed_mentions: (data.allowedMentions ?? undefined) as any
2020-12-10 04:36:36 +00:00
}
: undefined
}
await this.client.rest.post(
INTERACTION_CALLBACK(this.id, this.token),
payload
)
return this
}
2020-12-11 10:17:05 +00:00
2020-12-16 10:42:52 +00:00
/** Edit the original Interaction response */
2020-12-11 10:17:05 +00:00
async editResponse(data: {
content?: string
embeds?: Embed[]
}): Promise<Interaction> {
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
}
2020-12-16 10:42:52 +00:00
/** Delete the original Interaction Response */
async deleteResponse(): Promise<Interaction> {
const url = WEBHOOK_MESSAGE(
this.client.user?.id as string,
this.token,
'@original'
)
await this.client.rest.delete(url)
return this
}
2020-12-11 10:17:05 +00:00
get url(): string {
return `https://discord.com/api/v8/webhooks/${this.client.user?.id}/${this.token}`
}
2020-12-16 10:42:52 +00:00
/** Send a followup message */
2020-12-11 10:17:05 +00:00
async send(
text?: string | AllWebhookMessageOptions,
option?: AllWebhookMessageOptions
): Promise<Message> {
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
}
2020-12-16 10:42:52 +00:00
/** Edit a Followup message */
2020-12-11 10:17:05 +00:00
async editMessage(
msg: Message | string,
data: {
content?: string
embeds?: Embed[]
file?: any
allowed_mentions?: {
parse?: string
roles?: string[]
users?: string[]
everyone?: boolean
}
}
): Promise<Interaction> {
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<Interaction> {
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
}
2020-12-10 04:36:36 +00:00
}