From eb4bc94aea0f83b899f1624b4fddd490a21113da Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 14 Nov 2021 17:24:07 +0000 Subject: [PATCH] fix: interaction transformer. bump rc4 --- src/transformers/interaction.ts | 137 +++++++++++++++++- .../application_command_interaction_data.ts | 29 +++- ...ication_command_interaction_data_option.ts | 94 ++---------- ...ation_command_interaction_data_resolved.ts | 2 +- src/types/interactions/interaction.ts | 57 +------- src/util/constants.ts | 2 +- 6 files changed, 175 insertions(+), 146 deletions(-) diff --git a/src/transformers/interaction.ts b/src/transformers/interaction.ts index 8a6d27e03..1d3ad9465 100644 --- a/src/transformers/interaction.ts +++ b/src/transformers/interaction.ts @@ -1,14 +1,21 @@ import { Bot } from "../bot.ts"; +import { ChannelTypes } from "../types/channels/channel_types.ts"; import { - ApplicationCommandInteractionData, + InteractionData, ButtonData, DiscordInteractionTypes, Interaction, SelectMenuData, + InteractionDataResolved, + MessageComponentTypes, + InteractionDataOption, } from "../types/mod.ts"; import { SnakeCasedPropertiesDeep } from "../types/util.ts"; +import { Collection } from "../util/collection.ts"; +import { DiscordenoChannel } from "./channel.ts"; import { DiscordenoMember, DiscordenoUser } from "./member.ts"; import { DiscordenoMessage } from "./message.ts"; +import { DiscordenoRole } from "./role.ts"; export function transformInteraction(bot: Bot, payload: SnakeCasedPropertiesDeep): DiscordenoInteraction { const guildId = payload.guild_id ? bot.transformers.snowflake(payload.guild_id) : undefined; @@ -28,12 +35,96 @@ export function transformInteraction(bot: Bot, payload: SnakeCasedPropertiesDeep message: payload.message ? bot.transformers.message(bot, payload.message) : undefined, channelId: payload.channel_id ? bot.transformers.snowflake(payload.channel_id) : undefined, member: payload.member && guildId ? bot.transformers.member(bot, payload.member, guildId, user.id) : undefined, - // TODO: CamelCase INTERACTION DATA - // @ts-ignore - data: payload.data, + + // @ts-ignore figure this out + data: payload.data + ? { + componentType: payload.data.component_type, + customId: payload.data.custom_id, + values: payload.data.values, + id: payload.data.id ? bot.transformers.snowflake(payload.data.id) : undefined, + name: payload.data.name, + resolved: payload.data.resolved + ? transformInteractionDataResolved(bot, payload.data.resolved, guildId) + : undefined, + // @ts-ignore TODO: figure this out + options: payload.data.options, + targetId: payload.data.target_id ? bot.transformers.snowflake(payload.data.target_id) : undefined, + } + : undefined, }; } +export function transformInteractionDataResolved( + bot: Bot, + resolved: SnakeCasedPropertiesDeep, + guildId?: bigint +) { + const transformed: { + messages?: Collection; + users?: Collection; + members?: Collection; + roles?: Collection; + channels?: Collection; + } = {}; + + if (resolved.messages) { + transformed.messages = new Collection( + Object.entries(resolved.messages).map(([id, value]) => { + const message = bot.transformers.message(bot, value); + return [message.id, message]; + }) + ); + } + + if (resolved.users) { + transformed.users = new Collection( + Object.entries(resolved.users).map(([id, value]) => { + const user = bot.transformers.user(bot, value); + return [user.id, user]; + }) + ); + } + + if (guildId && resolved.members) { + transformed.members = new Collection( + Object.entries(resolved.members).map(([id, value]) => { + const member = bot.transformers.member(bot, value, guildId, bot.transformers.snowflake(id)); + return [member.id, member]; + }) + ); + } + + if (guildId && resolved.roles) { + transformed.roles = new Collection( + Object.entries(resolved.roles).map(([id, value]) => { + const role = bot.transformers.role(bot, { role: value, guildId }); + return [role.id, role]; + }) + ); + } + + if (resolved.channels) { + transformed.channels = new Collection( + Object.entries(resolved.channels).map(([key, value]) => { + const id = bot.transformers.snowflake(key); + const channel = value as { id: string; name: string; type: ChannelTypes; permissions: string }; + return [ + id, + { + id, + name: channel.name, + type: channel.type, + permissions: bot.transformers.snowflake(channel.permissions), + }, + ]; + }) + ); + } + + return resolved; +} + export interface DiscordenoInteraction { /** Id of the interaction */ id: bigint; @@ -56,5 +147,41 @@ export interface DiscordenoInteraction { /** Read-only property, always `1` */ version: 1; - data?: ApplicationCommandInteractionData | ButtonData | SelectMenuData; + data?: { + /** The type of component */ + componentType?: MessageComponentTypes; + /** The custom id provided for this component. */ + customId?: string; + /** The values chosen by the user. */ + values?: string[]; + /** The Id of the invoked command */ + id?: bigint; + /** The name of the invoked command */ + name?: string; + /** Converted users + roles + channels */ + resolved?: { + /** The Ids and Message objects */ + messages?: Collection; + /** The Ids and User objects */ + users?: Collection; + /** The Ids and partial Member objects */ + members?: Collection; + /** The Ids and Role objects */ + roles?: Collection; + /** The Ids and partial Channel objects */ + channels?: Collection< + bigint, + { + id: bigint; + name: string; + type: ChannelTypes; + permissions: bigint; + } + >; + }; + /** The params + values from the user */ + options?: InteractionDataOption[]; + /** The target id if this is a context menu command. */ + targetId?: bigint; + }; } diff --git a/src/types/interactions/commands/application_command_interaction_data.ts b/src/types/interactions/commands/application_command_interaction_data.ts index 5136d1f8b..0220ec1df 100644 --- a/src/types/interactions/commands/application_command_interaction_data.ts +++ b/src/types/interactions/commands/application_command_interaction_data.ts @@ -1,16 +1,33 @@ -import { ApplicationCommandInteractionDataOption } from "./application_command_interaction_data_option.ts"; -import { ApplicationCommandInteractionDataResolved } from "./application_command_interaction_data_resolved.ts"; +import { Message,User,Role,Channel, MessageComponentTypes } from "../../mod.ts"; +import { InteractionGuildMember } from "../interaction_guild_member.ts"; +import { InteractionDataOption } from "./application_command_interaction_data_option.ts"; -/** https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondata */ -export interface ApplicationCommandInteractionData { +export interface InteractionData { + /** The type of component */ + componentType?: MessageComponentTypes; + /** The custom id provided for this component. */ + customId?: string; + /** The values chosen by the user. */ + values?: string[]; /** The Id of the invoked command */ id: string; /** The name of the invoked command */ name: string; /** Converted users + roles + channels */ - resolved?: ApplicationCommandInteractionDataResolved; + resolved?: { + /** The Ids and Message objects */ + messages?: Record; + /** The Ids and User objects */ + users?: Record; + /** The Ids and partial Member objects */ + members?: Record>; + /** The Ids and Role objects */ + roles?: Record; + /** The Ids and partial Channel objects */ + channels?: Record>; + }; /** The params + values from the user */ - options?: ApplicationCommandInteractionDataOption[]; + options?: InteractionDataOption[]; /** The target id if this is a context menu command. */ targetId?: string; } diff --git a/src/types/interactions/commands/application_command_interaction_data_option.ts b/src/types/interactions/commands/application_command_interaction_data_option.ts index ce4a7c141..7497e5531 100644 --- a/src/types/interactions/commands/application_command_interaction_data_option.ts +++ b/src/types/interactions/commands/application_command_interaction_data_option.ts @@ -1,83 +1,15 @@ -import { DiscordApplicationCommandOptionTypes } from "./application_command_option_types.ts"; +import { GuildMember, Channel, Role } from "../../mod.ts"; +import { ApplicationCommandOptionTypes } from "./application_command_option_types.ts"; -/** https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondataoption */ -export type ApplicationCommandInteractionDataOption = - | ApplicationCommandInteractionDataOptionSubCommand - | ApplicationCommandInteractionDataOptionSubCommandGroup - | ApplicationCommandInteractionDataOptionWithValue; - -export type ApplicationCommandInteractionDataOptionWithValue = - | ApplicationCommandInteractionDataOptionString - | ApplicationCommandInteractionDataOptionInteger - | ApplicationCommandInteractionDataOptionNumber - | ApplicationCommandInteractionDataOptionBoolean - | ApplicationCommandInteractionDataOptionUser - | ApplicationCommandInteractionDataOptionChannel - | ApplicationCommandInteractionDataOptionRole - | ApplicationCommandInteractionDataOptionMentionable; - -interface ApplicationCommandInteractionDataOptionBase { - /** The name of the parameter */ +export type InteractionDataOption = { + /** the name of the parameter */ name: string; - /** Type of the option */ - type: T; - /** The value of the pair */ - value: V; - /** Whether the user has focused this option for autocompletion. */ - focused?: true; -} - -export interface ApplicationCommandInteractionDataOptionSubCommand - extends Omit, "value"> { - /** Present if this option is a group or subcommand */ - options?: ApplicationCommandInteractionDataOptionWithValue[]; -} - -export interface ApplicationCommandInteractionDataOptionSubCommandGroup - extends Omit< - ApplicationCommandInteractionDataOptionBase, - "value" - > { - /** Present if this option is a group or subcommand */ - options?: ApplicationCommandInteractionDataOptionSubCommand[]; -} - -export type ApplicationCommandInteractionDataOptionString = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.String, - string ->; - -export type ApplicationCommandInteractionDataOptionInteger = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.Integer, - number ->; - -export type ApplicationCommandInteractionDataOptionNumber = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.Number, - number ->; - -export type ApplicationCommandInteractionDataOptionBoolean = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.Boolean, - boolean ->; - -export type ApplicationCommandInteractionDataOptionUser = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.User, - string ->; - -export type ApplicationCommandInteractionDataOptionChannel = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.Channel, - string ->; - -export type ApplicationCommandInteractionDataOptionRole = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.Role, - string ->; - -export type ApplicationCommandInteractionDataOptionMentionable = ApplicationCommandInteractionDataOptionBase< - DiscordApplicationCommandOptionTypes.Mentionable, - string ->; + /** value of application command option type */ + type: ApplicationCommandOptionTypes; + /** the value of the pair */ + value?: string | boolean | number | GuildMember | Channel | Role; + /** present if this option is a group or subcommand */ + options?: InteractionDataOption[]; + /** true if this option is the currently focused option for autocomplete */ + focused?: boolean; +}; diff --git a/src/types/interactions/commands/application_command_interaction_data_resolved.ts b/src/types/interactions/commands/application_command_interaction_data_resolved.ts index 1b1653711..a307f3dba 100644 --- a/src/types/interactions/commands/application_command_interaction_data_resolved.ts +++ b/src/types/interactions/commands/application_command_interaction_data_resolved.ts @@ -4,7 +4,7 @@ import { Role } from "../../permissions/role.ts"; import { User } from "../../users/user.ts"; import { InteractionGuildMember } from "../interaction_guild_member.ts"; -export interface ApplicationCommandInteractionDataResolved { +export interface InteractionDataResolved { /** The Ids and Message objects */ messages?: Record; /** The Ids and User objects */ diff --git a/src/types/interactions/interaction.ts b/src/types/interactions/interaction.ts index a6e308ac3..eca64374b 100644 --- a/src/types/interactions/interaction.ts +++ b/src/types/interactions/interaction.ts @@ -1,37 +1,16 @@ import { Message } from "../messages/message.ts"; import { User } from "../users/user.ts"; -import { ApplicationCommandInteractionData } from "./commands/application_command_interaction_data.ts"; +import { InteractionData } from "./commands/application_command_interaction_data.ts"; import { InteractionGuildMember } from "./interaction_guild_member.ts"; -import { DiscordInteractionTypes } from "./interaction_types.ts"; -import { SelectMenuData } from "../messages/components/select_data.ts"; -import { ButtonData } from "../messages/components/button_data.ts"; -import { DiscordenoMessage } from "../../transformers/message.ts"; +import { InteractionTypes } from "./interaction_types.ts"; -/** https://discord.com/developers/docs/interactions/slash-commands#interaction */ -export type Interaction = PingInteraction | SlashCommandInteraction | ComponentInteraction; - -export type PingInteraction = BaseInteraction; - -export type SlashCommandInteraction = BaseInteraction< - DiscordInteractionTypes.ApplicationCommand, - ApplicationCommandInteractionData ->; - -export type ComponentInteraction = BaseInteraction< - DiscordInteractionTypes.MessageComponent, - ButtonData | SelectMenuData ->; - -export interface BaseInteraction< - T extends DiscordInteractionTypes, - D extends ApplicationCommandInteractionData | ButtonData | SelectMenuData | undefined -> { +export interface Interaction { /** Id of the interaction */ id: string; /** Id of the application this interaction is for */ applicationId: string; /** The type of interaction */ - type: T; + type: InteractionTypes; /** The guild it was sent from */ guildId?: string; /** The channel it was sent from */ @@ -47,31 +26,5 @@ export interface BaseInteraction< /** For the message the button was attached to */ message?: Message; - data?: D; -} - -export interface BigInteraction - extends Omit { - /** Id of the interaction */ - id: bigint; - /** Id of the application this interaction is for */ - applicationId: bigint; - /** The guild it was sent from */ - guildId?: bigint; - /** The channel it was sent from */ - channelId?: bigint; - /** Guild member data for the invoking user, including permissions */ - member?: Omit & { - /** Array of role object ids */ - roles: bigint[]; - /** The user this guild member represents */ - user: Omit & { - /** The user's id */ - id: bigint; - }; - }; - /** User object for the invoking user, if invoked in a DM */ - user: Omit & { id: bigint }; - /** For the message the button was attached to */ - message?: DiscordenoMessage; + data?: InteractionData; } diff --git a/src/util/constants.ts b/src/util/constants.ts index d43c5b8a9..0c816f059 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -9,7 +9,7 @@ export const GATEWAY_VERSION = 9; // TODO: update this version /** https://github.com/discordeno/discordeno/releases */ -export const DISCORDENO_VERSION = "13.0.0-rc3"; +export const DISCORDENO_VERSION = "13.0.0-rc4"; /** https://discord.com/developers/docs/reference#user-agent */ export const USER_AGENT = `DiscordBot (https://github.com/discordeno/discordeno, v${DISCORDENO_VERSION})`;