feat(slash): complete types

This commit is contained in:
DjDeveloperr 2020-12-16 16:12:52 +05:30
parent 71913ce239
commit 999fe88aa4
5 changed files with 67 additions and 9 deletions

View File

@ -238,6 +238,23 @@ export function slash(name?: string, guild?: string) {
}
}
export function subslash(parent: string, name?: string, guild?: string) {
return function (client: Client | SlashModule, prop: string) {
if (client._decoratedSlash === undefined) client._decoratedSlash = []
const item = (client as { [name: string]: any })[prop]
if (typeof item !== 'function') {
item.parent = parent
client._decoratedSlash.push(item)
} else
client._decoratedSlash.push({
parent,
name: name ?? prop,
guild,
handler: item
})
}
}
export function slashModule() {
return function (client: Client, prop: string) {
if (client._decoratedSlashModules === undefined)

View File

@ -34,7 +34,7 @@ export class SlashCommand {
this.applicationID = data.application_id
this.name = data.name
this.description = data.description
this.options = data.options
this.options = data.options ?? []
}
async delete(): Promise<void> {

View File

@ -3,6 +3,7 @@ import { MessageOption } from '../types/channel.ts'
import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts'
import {
InteractionData,
InteractionOption,
InteractionPayload,
InteractionResponsePayload,
InteractionResponseType
@ -76,10 +77,15 @@ export class Interaction {
return this.data.name
}
option<T = any>(name: string): T {
return this.data.options.find((e) => e.name === name)?.value
get options(): InteractionOption[] {
return this.data.options ?? []
}
option<T = any>(name: string): T {
return this.options.find((e) => e.name === name)?.value
}
/** Respond to an Interaction */
async respond(data: InteractionResponse): Promise<Interaction> {
const payload: InteractionResponsePayload = {
type: data.type ?? InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
@ -105,6 +111,7 @@ export class Interaction {
return this
}
/** Edit the original Interaction response */
async editResponse(data: {
content?: string
embeds?: Embed[]
@ -121,6 +128,7 @@ export class Interaction {
return this
}
/** Delete the original Interaction Response */
async deleteResponse(): Promise<Interaction> {
const url = WEBHOOK_MESSAGE(
this.client.user?.id as string,
@ -135,6 +143,7 @@ export class Interaction {
return `https://discord.com/api/v8/webhooks/${this.client.user?.id}/${this.token}`
}
/** Send a followup message */
async send(
text?: string | AllWebhookMessageOptions,
option?: AllWebhookMessageOptions
@ -195,6 +204,7 @@ export class Interaction {
return res
}
/** Edit a Followup message */
async editMessage(
msg: Message | string,
data: {

View File

@ -12,7 +12,7 @@ export class MyClient extends Client {
@slash()
send(d: Interaction): void {
d.respond({
content: d.data.options.find((e) => e.name === 'content')?.value
content: d.data.options?.find((e) => e.name === 'content')?.value
})
}
@ -26,7 +26,7 @@ export class MyClient extends Client {
content: 'This command can only be used by owner!'
})
} else {
const code = d.data.options.find((e) => e.name === 'code')
const code = d.data.options?.find((e) => e.name === 'code')
?.value as string
try {
// eslint-disable-next-line no-eval
@ -50,7 +50,7 @@ export class MyClient extends Client {
@slash()
async hug(d: Interaction): Promise<void> {
const id = d.data.options.find((e) => e.name === 'user')?.value as string
const id = d.data.options?.find((e) => e.name === 'user')?.value as string
const user = (await client.users.get(id)) ?? (await client.users.fetch(id))
const url = await fetch('https://nekos.life/api/v2/img/hug')
.then((r) => r.json())
@ -68,7 +68,7 @@ export class MyClient extends Client {
@slash()
async kiss(d: Interaction): Promise<void> {
const id = d.data.options.find((e) => e.name === 'user')?.value as string
const id = d.data.options?.find((e) => e.name === 'user')?.value as string
const user = (await client.users.get(id)) ?? (await client.users.fetch(id))
const url = await fetch('https://nekos.life/api/v2/img/kiss')
.then((r) => r.json())

View File

@ -2,34 +2,54 @@ import { EmbedPayload } from './channel.ts'
import { MemberPayload } from './guild.ts'
export interface InteractionOption {
/** Option name */
name: string
/** Value of the option */
value?: any
/** Sub options */
options?: any[]
}
export interface InteractionData {
/** Name of the Slash Command */
name: string
/** Unique ID of the Slash Command */
id: string
/** Options (arguments) sent with Interaction */
options: InteractionOption[]
}
export enum InteractionType {
/** Ping sent by the API (HTTP-only) */
PING = 1,
/** Slash Command Interaction */
APPLICATION_COMMAND = 2
}
export interface InteractionPayload {
/** Type of the Interaction */
type: InteractionType
/** Token of the Interaction to respond */
token: string
/** Member object of user who invoked */
member: MemberPayload
/** ID of the Interaction */
id: string
/**
* Data sent with the interaction
* **This can be undefined only when Interaction is not a Slash Command**
*/
data: InteractionData
/** ID of the Guild in which Interaction was invoked */
guild_id: string
/** ID of the Channel in which Interaction was invoked */
channel_id: string
}
export interface SlashCommandChoice {
/** (Display) name of the Choice */
name: string
/** Actual value to be sent in Interaction */
value: string
}
@ -48,7 +68,8 @@ export interface SlashCommandOption {
name: string
description: string
type: SlashCommandOptionType
required: boolean
required?: boolean
default?: boolean
choices?: SlashCommandChoice[]
options?: SlashCommandOption[]
}
@ -56,7 +77,7 @@ export interface SlashCommandOption {
export interface SlashCommandPartial {
name: string
description: string
options: SlashCommandOption[]
options?: SlashCommandOption[]
}
export interface SlashCommandPayload extends SlashCommandPartial {
@ -65,10 +86,15 @@ export interface SlashCommandPayload extends SlashCommandPartial {
}
export enum InteractionResponseType {
/** Just ack a ping, Http-only. */
PONG = 1,
/** Do nothing, just acknowledge the Interaction */
ACKNOWLEDGE = 2,
/** Send a channel message without "<User> used /<Command> with <Bot>" */
CHANNEL_MESSAGE = 3,
/** Send a channel message with "<User> used /<Command> with <Bot>" */
CHANNEL_MESSAGE_WITH_SOURCE = 4,
/** Send nothing further, but send "<User> used /<Command> with <Bot>" */
ACK_WITH_SOURCE = 5
}
@ -88,3 +114,8 @@ export interface InteractionResponseDataPayload {
}
flags?: number
}
export enum InteractionResponseFlags {
/** A Message which is only visible to Interaction User, and is not saved on backend */
EPHEMERAL = 1 << 6
}