Skip to content

Commit

Permalink
feat: add support for user-installable apps (Didas-git#23)
Browse files Browse the repository at this point in the history
* Initial helpers implementation

Thanks Didas-git#17 for the webhook idea.

* Remove reply options from the core

* Small update to the helpers

* Add helpers to template generator choices

* Completely change type organization

* Update modules to comply to changes

* This should not be here

* Use enum to get event name

`GatewayEvent` is not a `const enum` anymore but it shouldn't affect performance since listeners are compiled using the literals either way

* Initial builder implementation

This is far from ready but im pushing this so i can continue on my laptop

* Add support for Polls (Didas-git#21)

* feat: types for user installable apps

* feat: adopt transformers for user installable apps

* feat: adopt jsx for user-installable apps

* test: make ping command available in dms

* refactor: assign not required in interaction context type enum

* Initial helpers implementation

Thanks Didas-git#17 for the webhook idea.

* Completely change type organization

* Update modules to comply to changes

* This should not be here

* Initial builder implementation

This is far from ready but im pushing this so i can continue on my laptop

* Add support for Polls (Didas-git#21)

* feat: types for user installable apps

* feat: adopt transformers for user installable apps

* feat: adopt jsx for user-installable apps

* test: make ping command available in dms

* refactor: assign not required in interaction context type enum

* rebase

* revert(core): remove new line in pkg json

* fix: add USE_EXTERNAL_APPS permission

* refactor: remove deprecated fields

---------

Co-authored-by: DidaS <[email protected]>
  • Loading branch information
xhyrom and Didas-git authored Jul 27, 2024
1 parent 2640439 commit c1963e4
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/enums/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export const enum InteractionType {
MODAL_SUBMIT
}

export const enum InteractionContextType {
GUILD,
BOT_DM,
PRIVATE_CHANNEL
}

export const enum ApplicationCommandType {
CHAT_INPUT = 1,
USER,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/enums/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export const PermissionFlags = {
CREATE_EVENTS: 17592186044416n,
USE_EXTERNAL_SOUNDS: 35184372088832n,
SEND_VOICE_MESSAGES: 70368744177664n,
SEND_POLLS: 562949953421312n
SEND_POLLS: 562949953421312n,
USE_EXTERNAL_APPS: 1125899906842624n
} as const;
16 changes: 14 additions & 2 deletions packages/core/src/typings/application-command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { ApplicationCommandOptionType, ApplicationCommandPermissionType, ApplicationCommandType, ChannelType } from "#enums";
import type {
ApplicationCommandOptionType,
ApplicationCommandPermissionType,
ApplicationCommandType,
ApplicationIntegrationType,
ChannelType,
InteractionContextType
} from "#enums";
import type { ResolvedDataStructure } from "./others.js";
import type { Localizations as _Localizations } from "./localizations.js";

Expand Down Expand Up @@ -44,8 +51,9 @@ export declare namespace ApplicationCommand {
description: string;
options?: Array<Option.Structure>;
default_member_permissions: string | null;
dm_permission: boolean;
nsfw?: boolean;
integration_types?: Array<ApplicationIntegrationType>;
contexts?: Array<InteractionContextType>;
version: number;
}

Expand All @@ -59,6 +67,7 @@ export declare namespace ApplicationCommand {
options?: Array<Option.Structure>;
default_member_permissions: string | null;
nsfw?: boolean;
integration_types?: Array<ApplicationIntegrationType>;
version: number;
}

Expand Down Expand Up @@ -115,7 +124,10 @@ export declare namespace ApplicationCommand {
description?: string;
options?: Array<ApplicationCommandOption.Structure>;
default_member_permissions?: string | null;
/** @deprecated Use {@link ApplicationCommandJSONParams.contexts} instead */
dm_permission?: boolean | null;
integration_types?: Array<ApplicationIntegrationType> | null;
contexts?: Array<InteractionContextType> | null;
type?: ApplicationCommandType;
nsfw?: boolean;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/typings/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ export declare namespace Application {
description: string;
}

export type IntegrationTypeMap = Record<ApplicationIntegrationType, string>;
}
8 changes: 6 additions & 2 deletions packages/core/src/typings/interaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ApplicationCommandOption } from "./application-command-options.js";
import type { LilybirdAttachment, ResolvedDataStructure } from "./others.js";
import type { ApplicationCommand } from "./application-command.js";
import type { Application } from "./application.js";
import type { Channel } from "./channel.js";
import type { Message } from "./message.js";
import type { Guild } from "./guild.js";
Expand All @@ -13,7 +14,8 @@ import type {
EntitlementType,
InteractionType,
ComponentType,
Locale
Locale,
InteractionContextType
} from "#enums";

export declare namespace Interaction {
Expand All @@ -26,16 +28,18 @@ export declare namespace Interaction {
data?: DataStructure;
token: string;
version: number;
app_permissions: string;
locale: Locale;
entitlements: Array<EntitlementStructure>;
authorizing_integration_owners: Application.IntegrationTypeMap;
context?: InteractionContextType;
}

export interface GuildStructure extends Base {
guild_id: string;
channel: Partial<Channel.Structure>;
channel_id: string;
member: Guild.MemberStructure;
app_permissions: string;
guild_locale: Locale;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/typings/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export declare namespace Message {
*/
flags?: number;
referenced_message?: Structure | null;
interaction_metadata?: InteractionMetadataStructure;
interaction?: InteractionStructure;
thread?: Channel.Structure;
/** This does not exist without the intent */
Expand All @@ -76,6 +77,19 @@ export declare namespace Message {
member?: Partial<Guild.MemberStructure>;
}

/**
* @see {@link https://discord.com/developers/docs/resources/channel#message-interaction-metadata-object-message-interaction-metadata-structure}
*/
export interface InteractionMetadataStructure {
id: string;
type: InteractionType;
user: User.Structure;
authorizing_integration_owners: Application.IntegrationTypeMap;
original_response_message_id?: string;
interacted_message_id?: string;
triggering_interaction_metadata?: InteractionMetadataStructure;
}

/**
* @see {@link https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure}
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/jsx/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { ApplicationCommandOptionType, ApplicationCommandType } from "lilybird";
import type {
ApplicationCommand as LilyApplicationCommand,
PermissionFlags,
Locale
Locale,
ApplicationIntegrationType,
InteractionContextType
} from "lilybird";

export function ApplicationCommand({
name,
description,
defaultMemberPermissions,
dmPermission,
integrationTypes,
contexts,
name_localizations,
description_localizations,
nsfw,
Expand All @@ -22,6 +26,8 @@ export function ApplicationCommand({
description: string,
defaultMemberPermissions?: Array<typeof PermissionFlags[keyof typeof PermissionFlags]> | null,
dmPermission?: boolean | null,
integrationTypes?: Array<ApplicationIntegrationType> | null,
contexts?: Array<InteractionContextType> | null,
nsfw?: boolean,
name_localizations?: Record<Locale, string> | null,
description_localizations?: Record<Locale, string> | null,
Expand All @@ -34,6 +40,8 @@ export function ApplicationCommand({
name,
description,
dm_permission: dmPermission,
integration_types: integrationTypes,
contexts,
name_localizations,
description_localizations,
nsfw,
Expand Down
19 changes: 16 additions & 3 deletions packages/test/src/commands/ping.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import type { SlashCommand } from "@lilybird/handlers";
import type { ApplicationCommand } from "@lilybird/handlers";
import { ApplicationIntegrationType, InteractionContextType } from "lilybird";

export default {
post: "GLOBAL",
data: { name: "ping", description: "pong" },
data: {
name: "ping",
description: "pong",
integration_types: [
ApplicationIntegrationType.GUILD_INSTALL,
ApplicationIntegrationType.USER_INSTALL
],
contexts: [
InteractionContextType.BOT_DM,
InteractionContextType.GUILD,
InteractionContextType.PRIVATE_CHANNEL
],
},
run: async (interaction) => {
await interaction.deferReply();

Expand All @@ -17,4 +30,4 @@ export default {
depth: 1
}));
}
} satisfies SlashCommand;
} satisfies ApplicationCommand;
12 changes: 9 additions & 3 deletions packages/transformers/src/factories/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import type { ReplyOptions } from "../typings/shared.js";
import type { PartialChannel } from "./channel.js";

import type {
Application as LilyApplication,
Interaction as LilyInteraction,
Message as LilyMessage,
ResolvedDataStructure,
LilybirdAttachment,
Locale,
Client,
ApplicationCommand
ApplicationCommand,
InteractionContextType
} from "lilybird";

export function interactionFactory(client: Client, interaction: LilyInteraction.Structure): Interaction {
Expand Down Expand Up @@ -77,9 +79,12 @@ export class Interaction<T extends InteractionData = InteractionData, M extends
public readonly type: InteractionType;
public readonly token: string;
public readonly version = 1;
public readonly appPermissions: string;
/** This is only undefined if type is PING */
public readonly locale: Locale | undefined;
public readonly entitlements: Array<LilyInteraction.EntitlementStructure>;
public readonly authorizingIntegrationOwners: LilyApplication.IntegrationTypeMap;
public readonly context: InteractionContextType | undefined;
public readonly data: T;
public readonly message: M = <M>undefined;

Expand All @@ -93,8 +98,11 @@ export class Interaction<T extends InteractionData = InteractionData, M extends
this.applicationId = interaction.application_id;
this.type = interaction.type;
this.token = interaction.token;
this.appPermissions = interaction.app_permissions;
this.locale = interaction.locale;
this.entitlements = interaction.entitlements;
this.authorizingIntegrationOwners = interaction.authorizing_integration_owners;
this.context = interaction.context;

this.data = <never>data;

Expand Down Expand Up @@ -389,7 +397,6 @@ export class GuildInteraction<T extends InteractionData, M extends undefined | M
public readonly channel: PartialChannel;
public readonly channelId: string;
public readonly member: GuildMember;
public readonly appPermissions: string;
public readonly guildLocale: Locale;

public constructor(client: Client, interaction: LilyInteraction.GuildStructure, isDM: boolean, data?: T) {
Expand All @@ -399,7 +406,6 @@ export class GuildInteraction<T extends InteractionData, M extends undefined | M
this.channel = channelFactory(client, interaction.channel);
this.channelId = interaction.channel_id;
this.member = new GuildMember(client, interaction.member);
this.appPermissions = interaction.app_permissions;
this.guildLocale = interaction.guild_locale;
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/transformers/src/factories/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class Message {
public readonly messageReference: LilyMessage.ReferenceStructure | undefined;
public readonly flags: number;
public readonly referencedMessage: Message | undefined;
public readonly interactionMetadata: LilyMessage.InteractionMetadataStructure | undefined;
public readonly interaction: LilyMessage.InteractionStructure | undefined;
public readonly thread: Channel | undefined;
public readonly components: Array<LilyMessage.Component.Structure> | undefined;
Expand Down Expand Up @@ -96,6 +97,7 @@ export class Message {
this.applicationId = message.application_id;
this.messageReference = message.message_reference;
this.flags = message.flags ?? 0;
this.interactionMetadata = message.interaction_metadata;
this.interaction = message.interaction;
this.components = message.components;
this.stickerItems = message.sticker_items;
Expand Down

0 comments on commit c1963e4

Please sign in to comment.