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/slash.ts'
export * from './src/structures/interactions.ts'
export * from './src/structures/messageComponents.ts'
export * from './src/types/slashCommands.ts'
export * from './src/types/interactions.ts'
export * from './src/types/messageComponents.ts'
export { GuildEmojisManager } from './src/managers/guildEmojis.ts'
export { MembersManager } from './src/managers/members.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 { Message } from '../../structures/message.ts'
import { TextChannel } from '../../structures/textChannel.ts'
import { MessageComponentInteraction } from '../../structures/messageComponents.ts'
export const interactionCreate: GatewayEventHandler = async (
gateway: Gateway,
@ -156,14 +155,6 @@ export const interactionCreate: GatewayEventHandler = async (
user,
resolved
})
} else if (d.type === InteractionType.MESSAGE_COMPONENT) {
interaction = new MessageComponentInteraction(gateway.client, d, {
member,
guild,
channel,
user,
message
})
} else {
interaction = new Interaction(gateway.client, d, {
member,

View File

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

View File

@ -17,7 +17,6 @@ import { User } from '../structures/user.ts'
import { HarmonyEventEmitter } from '../utils/events.ts'
import { encodeText, decodeText } from '../utils/encoding.ts'
import { SlashCommandsManager } from './slashCommand.ts'
import { MessageComponentInteraction } from '../structures/messageComponents.ts'
export type SlashCommandHandlerCallback = (
interaction: SlashCommandInteraction
@ -203,10 +202,7 @@ export class SlashClient extends HarmonyEventEmitter<SlashClientEvents> {
/** Process an incoming Interaction */
private async _process(
interaction:
| Interaction
| SlashCommandInteraction
| MessageComponentInteraction
interaction: Interaction | SlashCommandInteraction
): Promise<void> {
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 getChannelByType from '../utils/channel.ts'
import { BaseManager } from './base.ts'
import { transformComponent } from './_util.ts'
export type AllMessageOptions = MessageOptions | Embed
@ -101,10 +100,6 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
content: content,
embed: option?.embed,
file: option?.file,
components:
option?.components !== undefined
? transformComponent(option.components)
: undefined,
files: option?.files,
tts: option?.tts,
allowed_mentions: option?.allowedMentions,
@ -172,10 +167,6 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
embed: option?.embed !== undefined ? option.embed.toJSON() : undefined,
// Cannot upload new files with Message
// file: option?.file,
components:
option?.components !== undefined
? transformComponent(option.components)
: undefined,
tts: option?.tts,
allowed_mentions: option?.allowedMentions
})

View File

@ -1,5 +1,4 @@
import type { Client } from '../client/client.ts'
import { transformComponent } from '../managers/_util.ts'
import {
AllowedMentionsPayload,
ChannelTypes,
@ -14,10 +13,6 @@ import {
InteractionResponseType,
InteractionType
} from '../types/interactions.ts'
import {
InteractionMessageComponentData,
MessageComponentData
} from '../types/messageComponents.ts'
import {
InteractionApplicationCommandData,
InteractionChannelPayload
@ -50,7 +45,6 @@ export interface InteractionMessageOptions {
allowedMentions?: AllowedMentionsPayload
/** Whether the Message Response should be Ephemeral (only visible to User) or not */
ephemeral?: boolean
components?: MessageComponentData[]
}
export interface InteractionResponse extends InteractionMessageOptions {
@ -107,7 +101,7 @@ export class Interaction extends SnowflakeBase {
_httpResponded?: boolean
applicationID: string
/** Data sent with Interaction. Only applies to Application Command */
data?: InteractionApplicationCommandData | InteractionMessageComponentData
data?: InteractionApplicationCommandData
message?: Message
constructor(
@ -154,11 +148,7 @@ export class Interaction extends SnowflakeBase {
embeds: data.embeds,
tts: data.tts ?? false,
flags,
allowed_mentions: data.allowedMentions ?? undefined,
components:
data.components === undefined
? undefined
: transformComponent(data.components)
allowed_mentions: data.allowedMentions ?? undefined
}
: undefined
}
@ -231,7 +221,6 @@ export class Interaction extends SnowflakeBase {
embeds?: Array<Embed | EmbedPayload>
flags?: number | number[]
allowedMentions?: AllowedMentionsPayload
components?: MessageComponentData[]
}): Promise<Interaction> {
const url = WEBHOOK_MESSAGE(this.applicationID, this.token, '@original')
await this.client.rest.patch(url, {
@ -241,11 +230,7 @@ export class Interaction extends SnowflakeBase {
typeof data.flags === 'object'
? data.flags.reduce((p, a) => p | a, 0)
: data.flags,
allowed_mentions: data.allowedMentions,
components:
data.components === undefined
? undefined
: transformComponent(data.components)
allowed_mentions: data.allowedMentions
})
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 { InteractionType } from './interactions.ts'
import type { UserPayload } from './user.ts'
import type {
MessageComponentData,
MessageComponentPayload
} from './messageComponents.ts'
export interface ChannelPayload {
id: string
@ -192,7 +188,6 @@ export interface MessagePayload {
flags?: number
stickers?: MessageStickerPayload[]
interaction?: MessageInteractionPayload
components?: MessageComponentPayload[]
}
export enum AllowedMentionType {
@ -215,7 +210,6 @@ export interface MessageOptions {
files?: MessageAttachment[]
allowedMentions?: AllowedMentionsPayload
reply?: Message | MessageReference | string
components?: MessageComponentData[]
}
export interface ChannelMention {
@ -399,7 +393,6 @@ export interface EditMessagePayload {
embed?: EmbedPayload
allowed_mentions?: AllowedMentionsPayload
flags?: number
components?: MessageComponentPayload[]
}
export interface CreateMessagePayload extends EditMessagePayload {
@ -408,7 +401,6 @@ export interface CreateMessagePayload extends EditMessagePayload {
message_reference?: MessageReference
file?: MessageAttachment
files?: MessageAttachment[]
components?: MessageComponentPayload[]
}
export interface CreateWebhookMessageBasePayload {

View File

@ -4,10 +4,6 @@ import {
MessagePayload
} from './channel.ts'
import type { MemberPayload } from './guild.ts'
import {
InteractionMessageComponentData,
MessageComponentData
} from './messageComponents.ts'
import type { InteractionApplicationCommandData } from './slashCommands.ts'
import type { UserPayload } from './user.ts'
@ -15,9 +11,7 @@ export enum InteractionType {
/** Ping sent by the API (HTTP-only) */
PING = 1,
/** Slash Command Interaction */
APPLICATION_COMMAND = 2,
/** Message Component Interaction */
MESSAGE_COMPONENT = 3
APPLICATION_COMMAND = 2
}
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?: InteractionApplicationCommandData | InteractionMessageComponentData
data?: InteractionApplicationCommandData
/** ID of the Guild in which Interaction was invoked */
guild_id?: string
/** ID of the Channel in which Interaction was invoked */
@ -75,7 +69,6 @@ export interface InteractionResponseDataPayload {
/** Allowed Mentions object */
allowed_mentions?: AllowedMentionsPayload
flags?: number
components?: MessageComponentData[]
}
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 { Interaction } from '../structures/interactions.ts'
import { MessageComponentInteraction } from '../structures/messageComponents.ts'
import { SlashCommandInteraction } from '../structures/slash.ts'
export function isSlashCommandInteraction(
@ -8,9 +7,3 @@ export function isSlashCommandInteraction(
): d is SlashCommandInteraction {
return d.type === InteractionType.APPLICATION_COMMAND
}
export function isMessageComponentInteraction(
d: Interaction
): d is MessageComponentInteraction {
return d.type === InteractionType.MESSAGE_COMPONENT
}