interactions api design fix

This commit is contained in:
DjDeveloperr 2021-04-28 16:54:22 +05:30
parent 1beccb57ba
commit 2cdb671fb1
12 changed files with 7 additions and 180 deletions

2
mod.ts
View file

@ -40,10 +40,8 @@ export { GuildManager } from './src/managers/guilds.ts'
export * from './src/structures/base.ts' export * from './src/structures/base.ts'
export * from './src/structures/slash.ts' export * from './src/structures/slash.ts'
export * from './src/structures/interactions.ts' export * from './src/structures/interactions.ts'
export * from './src/structures/messageComponents.ts'
export * from './src/types/slashCommands.ts' export * from './src/types/slashCommands.ts'
export * from './src/types/interactions.ts' export * from './src/types/interactions.ts'
export * from './src/types/messageComponents.ts'
export { GuildEmojisManager } from './src/managers/guildEmojis.ts' export { GuildEmojisManager } from './src/managers/guildEmojis.ts'
export { MembersManager } from './src/managers/members.ts' export { MembersManager } from './src/managers/members.ts'
export { MessageReactionsManager } from './src/managers/messageReactions.ts' export { MessageReactionsManager } from './src/managers/messageReactions.ts'

View file

@ -22,7 +22,6 @@ import { RolePayload } from '../../types/role.ts'
import { InteractionChannelPayload } from '../../types/slashCommands.ts' import { InteractionChannelPayload } from '../../types/slashCommands.ts'
import { Message } from '../../structures/message.ts' import { Message } from '../../structures/message.ts'
import { TextChannel } from '../../structures/textChannel.ts' import { TextChannel } from '../../structures/textChannel.ts'
import { MessageComponentInteraction } from '../../structures/messageComponents.ts'
export const interactionCreate: GatewayEventHandler = async ( export const interactionCreate: GatewayEventHandler = async (
gateway: Gateway, gateway: Gateway,
@ -156,14 +155,6 @@ export const interactionCreate: GatewayEventHandler = async (
user, user,
resolved resolved
}) })
} else if (d.type === InteractionType.MESSAGE_COMPONENT) {
interaction = new MessageComponentInteraction(gateway.client, d, {
member,
guild,
channel,
user,
message
})
} else { } else {
interaction = new Interaction(gateway.client, d, { interaction = new Interaction(gateway.client, d, {
member, member,

View file

@ -69,7 +69,6 @@ import { applicationCommandCreate } from './applicationCommandCreate.ts'
import { applicationCommandDelete } from './applicationCommandDelete.ts' import { applicationCommandDelete } from './applicationCommandDelete.ts'
import { applicationCommandUpdate } from './applicationCommandUpdate.ts' import { applicationCommandUpdate } from './applicationCommandUpdate.ts'
import type { SlashCommand } from '../../interactions/slashCommand.ts' import type { SlashCommand } from '../../interactions/slashCommand.ts'
import { MessageComponentInteraction } from '../../structures/messageComponents.ts'
export const gatewayHandlers: { export const gatewayHandlers: {
[eventCode in GatewayEvents]: GatewayEventHandler | undefined [eventCode in GatewayEvents]: GatewayEventHandler | undefined
@ -365,12 +364,7 @@ export type ClientEvents = {
* An Interaction was created * An Interaction was created
* @param interaction Created interaction object * @param interaction Created interaction object
*/ */
interactionCreate: [ interactionCreate: [interaction: Interaction | SlashCommandInteraction]
interaction:
| Interaction
| SlashCommandInteraction
| MessageComponentInteraction
]
/** /**
* When debug message was made * When debug message was made

View file

@ -17,7 +17,6 @@ import { User } from '../structures/user.ts'
import { HarmonyEventEmitter } from '../utils/events.ts' import { HarmonyEventEmitter } from '../utils/events.ts'
import { encodeText, decodeText } from '../utils/encoding.ts' import { encodeText, decodeText } from '../utils/encoding.ts'
import { SlashCommandsManager } from './slashCommand.ts' import { SlashCommandsManager } from './slashCommand.ts'
import { MessageComponentInteraction } from '../structures/messageComponents.ts'
export type SlashCommandHandlerCallback = ( export type SlashCommandHandlerCallback = (
interaction: SlashCommandInteraction interaction: SlashCommandInteraction
@ -203,10 +202,7 @@ export class SlashClient extends HarmonyEventEmitter<SlashClientEvents> {
/** Process an incoming Interaction */ /** Process an incoming Interaction */
private async _process( private async _process(
interaction: interaction: Interaction | SlashCommandInteraction
| Interaction
| SlashCommandInteraction
| MessageComponentInteraction
): Promise<void> { ): Promise<void> {
if (!this.enabled) return if (!this.enabled) return

View file

@ -1,19 +0,0 @@
import {
MessageComponentData,
MessageComponentPayload
} from '../types/messageComponents.ts'
export function transformComponent(
d: MessageComponentData[]
): MessageComponentPayload[] {
return d.map((e: any) => {
if (e.customID !== undefined) {
e.custom_id = e.customID
delete e.customID
}
if (e.components !== undefined) {
e.components = transformComponent(e.components)
}
return e
})
}

View file

@ -11,7 +11,6 @@ import type {
import { CHANNEL } from '../types/endpoint.ts' import { CHANNEL } from '../types/endpoint.ts'
import getChannelByType from '../utils/channel.ts' import getChannelByType from '../utils/channel.ts'
import { BaseManager } from './base.ts' import { BaseManager } from './base.ts'
import { transformComponent } from './_util.ts'
export type AllMessageOptions = MessageOptions | Embed export type AllMessageOptions = MessageOptions | Embed
@ -101,10 +100,6 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
content: content, content: content,
embed: option?.embed, embed: option?.embed,
file: option?.file, file: option?.file,
components:
option?.components !== undefined
? transformComponent(option.components)
: undefined,
files: option?.files, files: option?.files,
tts: option?.tts, tts: option?.tts,
allowed_mentions: option?.allowedMentions, allowed_mentions: option?.allowedMentions,
@ -172,10 +167,6 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
embed: option?.embed !== undefined ? option.embed.toJSON() : undefined, embed: option?.embed !== undefined ? option.embed.toJSON() : undefined,
// Cannot upload new files with Message // Cannot upload new files with Message
// file: option?.file, // file: option?.file,
components:
option?.components !== undefined
? transformComponent(option.components)
: undefined,
tts: option?.tts, tts: option?.tts,
allowed_mentions: option?.allowedMentions allowed_mentions: option?.allowedMentions
}) })

View file

@ -1,5 +1,4 @@
import type { Client } from '../client/client.ts' import type { Client } from '../client/client.ts'
import { transformComponent } from '../managers/_util.ts'
import { import {
AllowedMentionsPayload, AllowedMentionsPayload,
ChannelTypes, ChannelTypes,
@ -14,10 +13,6 @@ import {
InteractionResponseType, InteractionResponseType,
InteractionType InteractionType
} from '../types/interactions.ts' } from '../types/interactions.ts'
import {
InteractionMessageComponentData,
MessageComponentData
} from '../types/messageComponents.ts'
import { import {
InteractionApplicationCommandData, InteractionApplicationCommandData,
InteractionChannelPayload InteractionChannelPayload
@ -50,7 +45,6 @@ export interface InteractionMessageOptions {
allowedMentions?: AllowedMentionsPayload allowedMentions?: AllowedMentionsPayload
/** Whether the Message Response should be Ephemeral (only visible to User) or not */ /** Whether the Message Response should be Ephemeral (only visible to User) or not */
ephemeral?: boolean ephemeral?: boolean
components?: MessageComponentData[]
} }
export interface InteractionResponse extends InteractionMessageOptions { export interface InteractionResponse extends InteractionMessageOptions {
@ -107,7 +101,7 @@ export class Interaction extends SnowflakeBase {
_httpResponded?: boolean _httpResponded?: boolean
applicationID: string applicationID: string
/** Data sent with Interaction. Only applies to Application Command */ /** Data sent with Interaction. Only applies to Application Command */
data?: InteractionApplicationCommandData | InteractionMessageComponentData data?: InteractionApplicationCommandData
message?: Message message?: Message
constructor( constructor(
@ -154,11 +148,7 @@ export class Interaction extends SnowflakeBase {
embeds: data.embeds, embeds: data.embeds,
tts: data.tts ?? false, tts: data.tts ?? false,
flags, flags,
allowed_mentions: data.allowedMentions ?? undefined, allowed_mentions: data.allowedMentions ?? undefined
components:
data.components === undefined
? undefined
: transformComponent(data.components)
} }
: undefined : undefined
} }
@ -231,7 +221,6 @@ export class Interaction extends SnowflakeBase {
embeds?: Array<Embed | EmbedPayload> embeds?: Array<Embed | EmbedPayload>
flags?: number | number[] flags?: number | number[]
allowedMentions?: AllowedMentionsPayload allowedMentions?: AllowedMentionsPayload
components?: MessageComponentData[]
}): Promise<Interaction> { }): Promise<Interaction> {
const url = WEBHOOK_MESSAGE(this.applicationID, this.token, '@original') const url = WEBHOOK_MESSAGE(this.applicationID, this.token, '@original')
await this.client.rest.patch(url, { await this.client.rest.patch(url, {
@ -241,11 +230,7 @@ export class Interaction extends SnowflakeBase {
typeof data.flags === 'object' typeof data.flags === 'object'
? data.flags.reduce((p, a) => p | a, 0) ? data.flags.reduce((p, a) => p | a, 0)
: data.flags, : data.flags,
allowed_mentions: data.allowedMentions, allowed_mentions: data.allowedMentions
components:
data.components === undefined
? undefined
: transformComponent(data.components)
}) })
return this return this
} }

View file

@ -1,42 +0,0 @@
import {
InteractionMessageComponentData,
MessageComponentType
} from '../types/messageComponents.ts'
import { Interaction } from './interactions.ts'
import type { Client } from '../client/mod.ts'
import { InteractionPayload } from '../types/interactions.ts'
import type { Guild } from './guild.ts'
import type { GuildTextChannel } from './guildTextChannel.ts'
import type { Member } from './member.ts'
import type { TextChannel } from './textChannel.ts'
import { User } from './user.ts'
import { Message } from './message.ts'
export class MessageComponentInteraction extends Interaction {
data: InteractionMessageComponentData
declare message: Message
constructor(
client: Client,
data: InteractionPayload,
others: {
channel?: TextChannel | GuildTextChannel
guild?: Guild
member?: Member
user: User
message?: Message
}
) {
super(client, data, others)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
this.data = data.data as InteractionMessageComponentData
}
get customID(): string {
return this.data.custom_id
}
get componentType(): MessageComponentType {
return this.data.component_type
}
}

View file

@ -7,10 +7,6 @@ import type { EmojiPayload } from './emoji.ts'
import type { MemberPayload } from './guild.ts' import type { MemberPayload } from './guild.ts'
import type { InteractionType } from './interactions.ts' import type { InteractionType } from './interactions.ts'
import type { UserPayload } from './user.ts' import type { UserPayload } from './user.ts'
import type {
MessageComponentData,
MessageComponentPayload
} from './messageComponents.ts'
export interface ChannelPayload { export interface ChannelPayload {
id: string id: string
@ -192,7 +188,6 @@ export interface MessagePayload {
flags?: number flags?: number
stickers?: MessageStickerPayload[] stickers?: MessageStickerPayload[]
interaction?: MessageInteractionPayload interaction?: MessageInteractionPayload
components?: MessageComponentPayload[]
} }
export enum AllowedMentionType { export enum AllowedMentionType {
@ -215,7 +210,6 @@ export interface MessageOptions {
files?: MessageAttachment[] files?: MessageAttachment[]
allowedMentions?: AllowedMentionsPayload allowedMentions?: AllowedMentionsPayload
reply?: Message | MessageReference | string reply?: Message | MessageReference | string
components?: MessageComponentData[]
} }
export interface ChannelMention { export interface ChannelMention {
@ -399,7 +393,6 @@ export interface EditMessagePayload {
embed?: EmbedPayload embed?: EmbedPayload
allowed_mentions?: AllowedMentionsPayload allowed_mentions?: AllowedMentionsPayload
flags?: number flags?: number
components?: MessageComponentPayload[]
} }
export interface CreateMessagePayload extends EditMessagePayload { export interface CreateMessagePayload extends EditMessagePayload {
@ -408,7 +401,6 @@ export interface CreateMessagePayload extends EditMessagePayload {
message_reference?: MessageReference message_reference?: MessageReference
file?: MessageAttachment file?: MessageAttachment
files?: MessageAttachment[] files?: MessageAttachment[]
components?: MessageComponentPayload[]
} }
export interface CreateWebhookMessageBasePayload { export interface CreateWebhookMessageBasePayload {

View file

@ -4,10 +4,6 @@ import {
MessagePayload MessagePayload
} from './channel.ts' } from './channel.ts'
import type { MemberPayload } from './guild.ts' import type { MemberPayload } from './guild.ts'
import {
InteractionMessageComponentData,
MessageComponentData
} from './messageComponents.ts'
import type { InteractionApplicationCommandData } from './slashCommands.ts' import type { InteractionApplicationCommandData } from './slashCommands.ts'
import type { UserPayload } from './user.ts' import type { UserPayload } from './user.ts'
@ -15,9 +11,7 @@ export enum InteractionType {
/** Ping sent by the API (HTTP-only) */ /** Ping sent by the API (HTTP-only) */
PING = 1, PING = 1,
/** Slash Command Interaction */ /** Slash Command Interaction */
APPLICATION_COMMAND = 2, APPLICATION_COMMAND = 2
/** Message Component Interaction */
MESSAGE_COMPONENT = 3
} }
export interface InteractionMemberPayload extends MemberPayload { export interface InteractionMemberPayload extends MemberPayload {
@ -39,7 +33,7 @@ export interface InteractionPayload {
/** /**
* Data sent with the interaction. Undefined only when Interaction is PING (http-only).* * Data sent with the interaction. Undefined only when Interaction is PING (http-only).*
*/ */
data?: InteractionApplicationCommandData | InteractionMessageComponentData data?: InteractionApplicationCommandData
/** ID of the Guild in which Interaction was invoked */ /** ID of the Guild in which Interaction was invoked */
guild_id?: string guild_id?: string
/** ID of the Channel in which Interaction was invoked */ /** ID of the Channel in which Interaction was invoked */
@ -75,7 +69,6 @@ export interface InteractionResponseDataPayload {
/** Allowed Mentions object */ /** Allowed Mentions object */
allowed_mentions?: AllowedMentionsPayload allowed_mentions?: AllowedMentionsPayload
flags?: number flags?: number
components?: MessageComponentData[]
} }
export enum InteractionResponseFlags { export enum InteractionResponseFlags {

View file

@ -1,45 +0,0 @@
export enum MessageComponentType {
ActionRow = 1,
Button = 2
}
export enum ButtonStyle {
Primary = 1,
Secondary = 2,
Success = 3,
Destructive = 4,
Link = 5
}
export interface MessageComponentEmoji {
id?: string
name?: string
animated?: boolean
}
export interface MessageComponentPayload {
type: MessageComponentType
components?: MessageComponentPayload[]
label?: string
style?: ButtonStyle
url?: string
custom_id?: string
emoji?: MessageComponentEmoji
disabled?: boolean
}
export interface MessageComponentData {
type: MessageComponentType
components?: MessageComponentData[]
label?: string
style?: ButtonStyle
url?: string
customID?: string
emoji?: MessageComponentEmoji
disabled?: boolean
}
export interface InteractionMessageComponentData {
custom_id: string
component_type: MessageComponentType
}

View file

@ -1,6 +1,5 @@
import { InteractionType } from '../../mod.ts' import { InteractionType } from '../../mod.ts'
import { Interaction } from '../structures/interactions.ts' import { Interaction } from '../structures/interactions.ts'
import { MessageComponentInteraction } from '../structures/messageComponents.ts'
import { SlashCommandInteraction } from '../structures/slash.ts' import { SlashCommandInteraction } from '../structures/slash.ts'
export function isSlashCommandInteraction( export function isSlashCommandInteraction(
@ -8,9 +7,3 @@ export function isSlashCommandInteraction(
): d is SlashCommandInteraction { ): d is SlashCommandInteraction {
return d.type === InteractionType.APPLICATION_COMMAND return d.type === InteractionType.APPLICATION_COMMAND
} }
export function isMessageComponentInteraction(
d: Interaction
): d is MessageComponentInteraction {
return d.type === InteractionType.MESSAGE_COMPONENT
}