feat(slash): complete types
This commit is contained in:
		
							parent
							
								
									71913ce239
								
							
						
					
					
						commit
						999fe88aa4
					
				
					 5 changed files with 67 additions and 9 deletions
				
			
		|  | @ -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() { | export function slashModule() { | ||||||
|   return function (client: Client, prop: string) { |   return function (client: Client, prop: string) { | ||||||
|     if (client._decoratedSlashModules === undefined) |     if (client._decoratedSlashModules === undefined) | ||||||
|  |  | ||||||
|  | @ -34,7 +34,7 @@ export class SlashCommand { | ||||||
|     this.applicationID = data.application_id |     this.applicationID = data.application_id | ||||||
|     this.name = data.name |     this.name = data.name | ||||||
|     this.description = data.description |     this.description = data.description | ||||||
|     this.options = data.options |     this.options = data.options ?? [] | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   async delete(): Promise<void> { |   async delete(): Promise<void> { | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ import { MessageOption } from '../types/channel.ts' | ||||||
| import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts' | import { INTERACTION_CALLBACK, WEBHOOK_MESSAGE } from '../types/endpoint.ts' | ||||||
| import { | import { | ||||||
|   InteractionData, |   InteractionData, | ||||||
|  |   InteractionOption, | ||||||
|   InteractionPayload, |   InteractionPayload, | ||||||
|   InteractionResponsePayload, |   InteractionResponsePayload, | ||||||
|   InteractionResponseType |   InteractionResponseType | ||||||
|  | @ -76,10 +77,15 @@ export class Interaction { | ||||||
|     return this.data.name |     return this.data.name | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   option<T = any>(name: string): T { |   get options(): InteractionOption[] { | ||||||
|     return this.data.options.find((e) => e.name === name)?.value |     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> { |   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, | ||||||
|  | @ -105,6 +111,7 @@ export class Interaction { | ||||||
|     return this |     return this | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** Edit the original Interaction response */ | ||||||
|   async editResponse(data: { |   async editResponse(data: { | ||||||
|     content?: string |     content?: string | ||||||
|     embeds?: Embed[] |     embeds?: Embed[] | ||||||
|  | @ -121,6 +128,7 @@ export class Interaction { | ||||||
|     return this |     return this | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** Delete the original Interaction Response */ | ||||||
|   async deleteResponse(): Promise<Interaction> { |   async deleteResponse(): Promise<Interaction> { | ||||||
|     const url = WEBHOOK_MESSAGE( |     const url = WEBHOOK_MESSAGE( | ||||||
|       this.client.user?.id as string, |       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}` |     return `https://discord.com/api/v8/webhooks/${this.client.user?.id}/${this.token}` | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** Send a followup message */ | ||||||
|   async send( |   async send( | ||||||
|     text?: string | AllWebhookMessageOptions, |     text?: string | AllWebhookMessageOptions, | ||||||
|     option?: AllWebhookMessageOptions |     option?: AllWebhookMessageOptions | ||||||
|  | @ -195,6 +204,7 @@ export class Interaction { | ||||||
|     return res |     return res | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** Edit a Followup message */ | ||||||
|   async editMessage( |   async editMessage( | ||||||
|     msg: Message | string, |     msg: Message | string, | ||||||
|     data: { |     data: { | ||||||
|  |  | ||||||
|  | @ -12,7 +12,7 @@ export class MyClient extends Client { | ||||||
|   @slash() |   @slash() | ||||||
|   send(d: Interaction): void { |   send(d: Interaction): void { | ||||||
|     d.respond({ |     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!' |         content: 'This command can only be used by owner!' | ||||||
|       }) |       }) | ||||||
|     } else { |     } else { | ||||||
|       const code = d.data.options.find((e) => e.name === 'code') |       const code = d.data.options?.find((e) => e.name === 'code') | ||||||
|         ?.value as string |         ?.value as string | ||||||
|       try { |       try { | ||||||
|         // eslint-disable-next-line no-eval
 |         // eslint-disable-next-line no-eval
 | ||||||
|  | @ -50,7 +50,7 @@ export class MyClient extends Client { | ||||||
| 
 | 
 | ||||||
|   @slash() |   @slash() | ||||||
|   async hug(d: Interaction): Promise<void> { |   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 user = (await client.users.get(id)) ?? (await client.users.fetch(id)) | ||||||
|     const url = await fetch('https://nekos.life/api/v2/img/hug') |     const url = await fetch('https://nekos.life/api/v2/img/hug') | ||||||
|       .then((r) => r.json()) |       .then((r) => r.json()) | ||||||
|  | @ -68,7 +68,7 @@ export class MyClient extends Client { | ||||||
| 
 | 
 | ||||||
|   @slash() |   @slash() | ||||||
|   async kiss(d: Interaction): Promise<void> { |   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 user = (await client.users.get(id)) ?? (await client.users.fetch(id)) | ||||||
|     const url = await fetch('https://nekos.life/api/v2/img/kiss') |     const url = await fetch('https://nekos.life/api/v2/img/kiss') | ||||||
|       .then((r) => r.json()) |       .then((r) => r.json()) | ||||||
|  |  | ||||||
|  | @ -2,34 +2,54 @@ import { EmbedPayload } from './channel.ts' | ||||||
| import { MemberPayload } from './guild.ts' | import { MemberPayload } from './guild.ts' | ||||||
| 
 | 
 | ||||||
| export interface InteractionOption { | export interface InteractionOption { | ||||||
|  |   /** Option name */ | ||||||
|   name: string |   name: string | ||||||
|  |   /** Value of the option */ | ||||||
|   value?: any |   value?: any | ||||||
|  |   /** Sub options */ | ||||||
|   options?: any[] |   options?: any[] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface InteractionData { | export interface InteractionData { | ||||||
|  |   /** Name of the Slash Command */ | ||||||
|   name: string |   name: string | ||||||
|  |   /** Unique ID of the Slash Command */ | ||||||
|   id: string |   id: string | ||||||
|  |   /** Options (arguments) sent with Interaction */ | ||||||
|   options: InteractionOption[] |   options: InteractionOption[] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export enum InteractionType { | export enum InteractionType { | ||||||
|  |   /** Ping sent by the API (HTTP-only) */ | ||||||
|   PING = 1, |   PING = 1, | ||||||
|  |   /** Slash Command Interaction */ | ||||||
|   APPLICATION_COMMAND = 2 |   APPLICATION_COMMAND = 2 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface InteractionPayload { | export interface InteractionPayload { | ||||||
|  |   /** Type of the Interaction */ | ||||||
|   type: InteractionType |   type: InteractionType | ||||||
|  |   /** Token of the Interaction to respond */ | ||||||
|   token: string |   token: string | ||||||
|  |   /** Member object of user who invoked */ | ||||||
|   member: MemberPayload |   member: MemberPayload | ||||||
|  |   /** ID of the Interaction */ | ||||||
|   id: string |   id: string | ||||||
|  |   /** | ||||||
|  |    * Data sent with the interaction | ||||||
|  |    * **This can be undefined only when Interaction is not a Slash Command** | ||||||
|  |    */ | ||||||
|   data: InteractionData |   data: InteractionData | ||||||
|  |   /** ID of the Guild in which Interaction was invoked */ | ||||||
|   guild_id: string |   guild_id: string | ||||||
|  |   /** ID of the Channel in which Interaction was invoked */ | ||||||
|   channel_id: string |   channel_id: string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface SlashCommandChoice { | export interface SlashCommandChoice { | ||||||
|  |   /** (Display) name of the Choice */ | ||||||
|   name: string |   name: string | ||||||
|  |   /** Actual value to be sent in Interaction */ | ||||||
|   value: string |   value: string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -48,7 +68,8 @@ export interface SlashCommandOption { | ||||||
|   name: string |   name: string | ||||||
|   description: string |   description: string | ||||||
|   type: SlashCommandOptionType |   type: SlashCommandOptionType | ||||||
|   required: boolean |   required?: boolean | ||||||
|  |   default?: boolean | ||||||
|   choices?: SlashCommandChoice[] |   choices?: SlashCommandChoice[] | ||||||
|   options?: SlashCommandOption[] |   options?: SlashCommandOption[] | ||||||
| } | } | ||||||
|  | @ -56,7 +77,7 @@ export interface SlashCommandOption { | ||||||
| export interface SlashCommandPartial { | export interface SlashCommandPartial { | ||||||
|   name: string |   name: string | ||||||
|   description: string |   description: string | ||||||
|   options: SlashCommandOption[] |   options?: SlashCommandOption[] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface SlashCommandPayload extends SlashCommandPartial { | export interface SlashCommandPayload extends SlashCommandPartial { | ||||||
|  | @ -65,10 +86,15 @@ export interface SlashCommandPayload extends SlashCommandPartial { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export enum InteractionResponseType { | export enum InteractionResponseType { | ||||||
|  |   /** Just ack a ping, Http-only. */ | ||||||
|   PONG = 1, |   PONG = 1, | ||||||
|  |   /** Do nothing, just acknowledge the Interaction */ | ||||||
|   ACKNOWLEDGE = 2, |   ACKNOWLEDGE = 2, | ||||||
|  |   /** Send a channel message without "<User> used /<Command> with <Bot>" */ | ||||||
|   CHANNEL_MESSAGE = 3, |   CHANNEL_MESSAGE = 3, | ||||||
|  |   /** Send a channel message with "<User> used /<Command> with <Bot>" */ | ||||||
|   CHANNEL_MESSAGE_WITH_SOURCE = 4, |   CHANNEL_MESSAGE_WITH_SOURCE = 4, | ||||||
|  |   /** Send nothing further, but send "<User> used /<Command> with <Bot>" */ | ||||||
|   ACK_WITH_SOURCE = 5 |   ACK_WITH_SOURCE = 5 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -88,3 +114,8 @@ export interface InteractionResponseDataPayload { | ||||||
|   } |   } | ||||||
|   flags?: number |   flags?: number | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export enum InteractionResponseFlags { | ||||||
|  |   /** A Message which is only visible to Interaction User, and is not saved on backend */ | ||||||
|  |   EPHEMERAL = 1 << 6 | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue