feat(slash): try add new methods
This commit is contained in:
parent
bb662267cb
commit
97fce78953
9 changed files with 179 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { Client } from '../models/client.ts'
|
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 {
|
import {
|
||||||
InteractionData,
|
InteractionData,
|
||||||
InteractionPayload,
|
InteractionPayload,
|
||||||
|
@ -9,8 +10,18 @@ import {
|
||||||
import { Embed } from './embed.ts'
|
import { Embed } from './embed.ts'
|
||||||
import { Guild } from './guild.ts'
|
import { Guild } from './guild.ts'
|
||||||
import { Member } from './member.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 { 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 {
|
export interface InteractionResponse {
|
||||||
type?: InteractionResponseType
|
type?: InteractionResponseType
|
||||||
|
@ -19,6 +30,12 @@ export interface InteractionResponse {
|
||||||
tts?: boolean
|
tts?: boolean
|
||||||
flags?: number
|
flags?: number
|
||||||
temp?: boolean
|
temp?: boolean
|
||||||
|
allowedMentions?: {
|
||||||
|
parse?: string
|
||||||
|
roles?: string[]
|
||||||
|
users?: string[]
|
||||||
|
everyone?: boolean
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Interaction {
|
export class Interaction {
|
||||||
|
@ -30,6 +47,7 @@ export class Interaction {
|
||||||
channel: GuildTextChannel
|
channel: GuildTextChannel
|
||||||
guild: Guild
|
guild: Guild
|
||||||
member: Member
|
member: Member
|
||||||
|
_savedHook?: Webhook
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
client: Client,
|
client: Client,
|
||||||
|
@ -58,6 +76,10 @@ export class Interaction {
|
||||||
return this.data.name
|
return this.data.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
option(name: string): any {
|
||||||
|
return this.data.options.find((e) => e.name === name)?.value
|
||||||
|
}
|
||||||
|
|
||||||
async respond(data: InteractionResponse): Promise<Interaction> {
|
async respond(data: InteractionResponse): Promise<Interaction> {
|
||||||
const payload: InteractionResponsePayload = {
|
const payload: InteractionResponsePayload = {
|
||||||
type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
@ -69,7 +91,8 @@ export class Interaction {
|
||||||
content: data.content ?? '',
|
content: data.content ?? '',
|
||||||
embeds: data.embeds,
|
embeds: data.embeds,
|
||||||
tts: data.tts ?? false,
|
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
|
: undefined
|
||||||
}
|
}
|
||||||
|
@ -81,4 +104,120 @@ export class Interaction {
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
get url(): string {
|
||||||
|
return `https://discord.com/api/v8/webhooks/${this.client.user?.id}/${this.token}`
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import { Message } from './message.ts'
|
||||||
import { TextChannel } from './textChannel.ts'
|
import { TextChannel } from './textChannel.ts'
|
||||||
import { User } from './user.ts'
|
import { User } from './user.ts'
|
||||||
import { fetchAuto } from 'https://raw.githubusercontent.com/DjDeveloperr/fetch-base64/main/mod.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 {
|
export interface WebhookMessageOptions extends MessageOption {
|
||||||
embeds?: Embed[]
|
embeds?: Embed[]
|
||||||
|
@ -191,4 +192,40 @@ export class Webhook {
|
||||||
if (resp.response.status !== 204) return false
|
if (resp.response.status !== 204) return false
|
||||||
else return true
|
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<Webhook> {
|
||||||
|
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<Webhook> {
|
||||||
|
await this.client?.rest.delete(
|
||||||
|
WEBHOOK_MESSAGE(
|
||||||
|
this.id,
|
||||||
|
(this.token ?? this.client.token) as string,
|
||||||
|
typeof message === 'string' ? message : message.id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return this
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue