fix: interaction transformer. bump rc4

This commit is contained in:
Skillz4Killz
2021-11-14 17:24:07 +00:00
committed by GitHub
parent 476cc5e717
commit eb4bc94aea
6 changed files with 175 additions and 146 deletions

View File

@@ -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<Interaction>): 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<InteractionDataResolved>,
guildId?: bigint
) {
const transformed: {
messages?: Collection<bigint, DiscordenoMessage>;
users?: Collection<bigint, DiscordenoUser>;
members?: Collection<bigint, DiscordenoMember>;
roles?: Collection<bigint, DiscordenoRole>;
channels?: Collection<bigint, { id: bigint; name: string; type: ChannelTypes; permissions: bigint }>;
} = {};
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<bigint, DiscordenoMessage>;
/** The Ids and User objects */
users?: Collection<bigint, DiscordenoUser>;
/** The Ids and partial Member objects */
members?: Collection<bigint, DiscordenoMember>;
/** The Ids and Role objects */
roles?: Collection<bigint, DiscordenoRole>;
/** 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;
};
}

View File

@@ -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<string, Message>;
/** The Ids and User objects */
users?: Record<string, User>;
/** The Ids and partial Member objects */
members?: Record<string, Omit<InteractionGuildMember, "user" | "deaf" | "mute">>;
/** The Ids and Role objects */
roles?: Record<string, Role>;
/** The Ids and partial Channel objects */
channels?: Record<string, Pick<Channel, "id" | "name" | "type" | "permissions">>;
};
/** The params + values from the user */
options?: ApplicationCommandInteractionDataOption[];
options?: InteractionDataOption[];
/** The target id if this is a context menu command. */
targetId?: string;
}

View File

@@ -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<T extends DiscordApplicationCommandOptionTypes, V = unknown> {
/** 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<ApplicationCommandInteractionDataOptionBase<DiscordApplicationCommandOptionTypes.SubCommand>, "value"> {
/** Present if this option is a group or subcommand */
options?: ApplicationCommandInteractionDataOptionWithValue[];
}
export interface ApplicationCommandInteractionDataOptionSubCommandGroup
extends Omit<
ApplicationCommandInteractionDataOptionBase<DiscordApplicationCommandOptionTypes.SubCommandGroup>,
"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;
};

View File

@@ -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<string, Message>;
/** The Ids and User objects */

View File

@@ -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<DiscordInteractionTypes.Ping, undefined>;
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<Interaction, "id" | "applicationId" | "guildId" | "channelId" | "member" | "user" | "message"> {
/** 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<InteractionGuildMember, "roles" | "user"> & {
/** Array of role object ids */
roles: bigint[];
/** The user this guild member represents */
user: Omit<User, "id"> & {
/** The user's id */
id: bigint;
};
};
/** User object for the invoking user, if invoked in a DM */
user: Omit<User, "id"> & { id: bigint };
/** For the message the button was attached to */
message?: DiscordenoMessage;
data?: InteractionData;
}

View File

@@ -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})`;