Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for user-installable apps #22

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 15 additions & 1 deletion 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,11 @@ export declare namespace ApplicationCommand {
description: string;
options?: Array<Option.Structure>;
default_member_permissions: string | null;
/** @deprecated Use {@link GlobalStructure.contexts} instead */
dm_permission: boolean;
nsfw?: boolean;
integration_types?: Array<ApplicationIntegrationType>;
contexts?: Array<InteractionContextType>;
version: number;
}

Expand All @@ -59,6 +69,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 +126,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
15 changes: 15 additions & 0 deletions packages/core/src/typings/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export declare namespace Message {
*/
flags?: number;
referenced_message?: Structure | null;
interaction_metadata?: InteractionMetadataStructure;
/** @deprecated Use {@link Structure.interaction_metadata} instead */
interaction?: InteractionStructure;
thread?: Channel.Structure;
/** This does not exist without the intent */
Expand All @@ -76,6 +78,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