Skip to content

Commit

Permalink
Add docs + lilybird 0.3 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Didas-git authored Dec 6, 2023
1 parent d17c5be commit c5758a3
Show file tree
Hide file tree
Showing 27 changed files with 940 additions and 98 deletions.
6 changes: 5 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ bun run build

echo Building create-bot package
cd ../create
bun run build
bun run build

echo Running ESLint on all packages
cd ../..
bun check
118 changes: 63 additions & 55 deletions packages/core/src/factories/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function interactionDataFactory(interaction: InteractionStructure): InteractionD
return undefined;
case InteractionType.APPLICATION_COMMAND:
case InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE: {
return new ApplicationCommandData(interaction.data);
return new ApplicationCommandData(interaction.data) as never;
}
case InteractionType.MESSAGE_COMPONENT: {
return new MessageComponentData(interaction.data);
Expand All @@ -51,11 +51,9 @@ function interactionDataFactory(interaction: InteractionStructure): InteractionD
}
}

export type InteractionData = ApplicationCommandData<undefined | string | number> | AutocompleteData | MessageComponentData | ModalSubmitData | undefined;
export type InteractionData = ApplicationCommandData | AutocompleteData | MessageComponentData | ModalSubmitData | undefined;

export interface AutocompleteData extends Omit<ApplicationCommandDataStructure, "options"> {
options: ApplicationCommandOptions<string | number>;
}
export interface AutocompleteData extends ApplicationCommandData<FocusedOption> {}

export interface InteractionReplyOptions extends ReplyOptions {
ephemeral?: boolean;
Expand Down Expand Up @@ -238,7 +236,7 @@ export class Interaction<T extends InteractionData, M extends undefined | Messag
return this.type === InteractionType.PING;
}

public isApplicationCommandInteraction(): this is Interaction<ApplicationCommandData<undefined>> {
public isApplicationCommandInteraction(): this is Interaction<ApplicationCommandData> {
return this.type === InteractionType.APPLICATION_COMMAND;
}

Expand All @@ -265,7 +263,7 @@ export class Interaction<T extends InteractionData, M extends undefined | Messag

export interface GuildInteraction<T extends InteractionData, M extends undefined | MessageStructure = undefined> extends Interaction<T, M> {
isPingInteraction: () => this is GuildInteraction<undefined>;
isApplicationCommandInteraction: () => this is GuildInteraction<ApplicationCommandData<undefined>>;
isApplicationCommandInteraction: () => this is GuildInteraction<ApplicationCommandData>;
isAutocompleteInteraction: () => this is GuildInteraction<AutocompleteData>;
isMessageComponentInteraction: () => this is GuildInteraction<MessageComponentData, MessageStructure>;
isModalSubmitInteraction: () => this is GuildInteraction<ModalSubmitData>;
Expand Down Expand Up @@ -298,7 +296,7 @@ export class GuildInteraction<T extends InteractionData, M extends undefined | M

export interface DMInteraction<T extends InteractionData, M extends undefined | MessageStructure = undefined> extends Interaction<T, M> {
isPingInteraction: () => this is DMInteraction<undefined>;
isApplicationCommandInteraction: () => this is DMInteraction<ApplicationCommandData<undefined>>;
isApplicationCommandInteraction: () => this is DMInteraction<ApplicationCommandData>;
isAutocompleteInteraction: () => this is DMInteraction<AutocompleteData>;
isMessageComponentInteraction: () => this is DMInteraction<MessageComponentData, MessageStructure>;
isModalSubmitInteraction: () => this is DMInteraction<ModalSubmitData>;
Expand All @@ -319,43 +317,22 @@ export class DMInteraction<T extends InteractionData, M extends undefined | Mess
}
}

interface GuildApplicationCommandData<T> extends ApplicationCommandData<T> {
interface GuildApplicationCommandData<T extends undefined | FocusedOption> extends ApplicationCommandData<T> {
readonly guildId: string;
}

interface UIApplicationCommandData<T> extends ApplicationCommandData<T> {
interface UIApplicationCommandData<T extends undefined | FocusedOption> extends ApplicationCommandData<T> {
readonly targetId: string;
}

export class ApplicationCommandData<T> {
export class ApplicationCommandData<T extends undefined | FocusedOption = undefined> {
public readonly id: string;
public readonly name: string;
public readonly type: ApplicationCommandType;
public readonly resolved?: ResolvedDataStructure;
public readonly options: ApplicationCommandOptions<T>;
public readonly guildId?: string;
public readonly targetId?: string;

public constructor(data: ApplicationCommandDataStructure) {
this.id = data.id;
this.name = data.name;
this.type = data.type;
this.resolved = data.resolved;
this.options = new ApplicationCommandOptions(data.options);
this.guildId = data.guild_id;
this.targetId = data.target_id;
}

public isGuildApplicationCommand(): this is GuildApplicationCommandData<T> {
return typeof this.guildId !== "undefined";
}

public isUIApplicationCommand(): this is UIApplicationCommandData<T> {
return typeof this.targetId !== "undefined";
}
}

class ApplicationCommandOptions<T> {
readonly #stringOptions = new Map<string, string | undefined>();
readonly #numberOptions = new Map<string, number | undefined>();
readonly #integerOptions = new Map<string, number | undefined>();
Expand All @@ -364,13 +341,21 @@ class ApplicationCommandOptions<T> {
readonly #channelOptions = new Map<string, string | undefined>();
readonly #roleOptions = new Map<string, string | undefined>();
readonly #mentionableOptions = new Map<string, string | undefined>();
readonly #attachmentOptions = new Map<string, string | undefined>();

#focused!: T;
#focused!: FocusedOption;
#subCommandGroup: string | undefined;
#subCommand: string | undefined;

public constructor(options: ApplicationCommandDataStructure["options"]) {
this.#parseOptions(options);
public constructor(data: ApplicationCommandDataStructure) {
this.id = data.id;
this.name = data.name;
this.type = data.type;
this.resolved = data.resolved;
this.guildId = data.guild_id;
this.targetId = data.target_id;

this.#parseOptions(data.options);
}

#parseOptions(options: ApplicationCommandDataStructure["options"]): void {
Expand All @@ -380,7 +365,8 @@ class ApplicationCommandOptions<T> {
const option = options[i];

if (option.focused) {
this.#focused = <T>option.value;
if (typeof option.value === "undefined") continue;
this.#focused = { name: option.name, value: option.value };
continue;
}

Expand All @@ -397,69 +383,75 @@ class ApplicationCommandOptions<T> {
}

case ApplicationCommandOptionType.STRING: {
if (typeof option.value !== "string")
throw new Error("Something unexpected happened");
if (typeof option.value !== "string") throw new Error("Something unexpected happened");

this.#stringOptions.set(option.name, option.value);
break;
}

case ApplicationCommandOptionType.INTEGER: {
if (typeof option.value !== "number")
throw new Error("Something unexpected happened");
if (typeof option.value !== "number") throw new Error("Something unexpected happened");

this.#integerOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.NUMBER: {
if (typeof option.value !== "number")
throw new Error("Something unexpected happened");
if (typeof option.value !== "number") throw new Error("Something unexpected happened");

this.#numberOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.BOOLEAN: {
if (typeof option.value !== "boolean")
throw new Error("Something unexpected happened");
if (typeof option.value !== "boolean") throw new Error("Something unexpected happened");

this.#booleanOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.USER: {
if (typeof option.value !== "string")
throw new Error("Something unexpected happened");
if (typeof option.value !== "string") throw new Error("Something unexpected happened");

this.#userOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.CHANNEL: {
if (typeof option.value !== "string")
throw new Error("Something unexpected happened");
if (typeof option.value !== "string") throw new Error("Something unexpected happened");

this.#channelOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.ROLE: {
if (typeof option.value !== "string")
throw new Error("Something unexpected happened");
if (typeof option.value !== "string") throw new Error("Something unexpected happened");

this.#roleOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.MENTIONABLE: {
if (typeof option.value !== "string")
throw new Error("Something unexpected happened");
if (typeof option.value !== "string") throw new Error("Something unexpected happened");

this.#mentionableOptions.set(option.name, option.value);
break;
}
case ApplicationCommandOptionType.ATTACHMENT:
case ApplicationCommandOptionType.ATTACHMENT: {
if (typeof this.resolved?.attachments === "undefined") throw new Error("Something unexpected happened");
if (typeof option.value !== "string") throw new Error("Something unexpected happened");

this.#attachmentOptions.set(option.name, this.resolved.attachments[option.value].url);
break;
}
}
}
}

public get focused(): T {
return this.#focused;
public isGuildApplicationCommand(): this is GuildApplicationCommandData<T> {
return typeof this.guildId !== "undefined";
}

public isUIApplicationCommand(): this is UIApplicationCommandData<T> {
return typeof this.targetId !== "undefined";
}

public getFocused<F extends string | number | boolean = string | number | boolean>(): ParseFocusedReturnType<T, F> {
return this.#focused as never;
}

public get subCommand(): string | undefined {
Expand Down Expand Up @@ -541,8 +533,24 @@ class ApplicationCommandOptions<T> {

return this.#mentionableOptions.get(name);
}

public getAttachment(name: string): string | undefined;
public getAttachment(name: string, assert: true): string;
public getAttachment(name: string, assert = false): string | undefined {
if (assert)
if (!this.#attachmentOptions.has(name)) throw new NotFoundError("Attachment");

return this.#attachmentOptions.get(name);
}
}

interface FocusedOption<T extends string | number | boolean = string | number | boolean> {
name: string;
value: T;
}

type ParseFocusedReturnType<T extends undefined | FocusedOption, F extends string | number | boolean> = T extends undefined ? undefined : FocusedOption<F>;

class NotFoundError extends Error {
public constructor(type: string) {
super();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/rest/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class REST {
return this.#makeRequest("GET", `applications/${clientId}/commands?with_localizations=${withLocalizations}`);
}

public async createGlobalApplicationCommands(clientId: string, body: POSTApplicationCommandStructure): Promise<LocalizationGlobalApplicationCommandStructure> {
public async createGlobalApplicationCommand(clientId: string, body: POSTApplicationCommandStructure): Promise<LocalizationGlobalApplicationCommandStructure> {
return this.#makeRequest("POST", `applications/${clientId}/commands`, body);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/create/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ignorePatterns": [
"src/index.ts"
"src/index.ts",
"src/index-template.ts"
],
"extends": [
"../../.eslintrc.json"
Expand Down
3 changes: 3 additions & 0 deletions packages/create/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
},
"include": [
"src/**/*"
],
"exclude": [
"src/index-template.ts"
]
}
90 changes: 86 additions & 4 deletions packages/docs/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,100 @@ export default defineConfig({
tag: "meta",
attrs: {
name: "theme-color",
content: "#ff00ef"
content: "#f49ac2"
}
}
],
sidebar: [
{
label: "Philosophy",
link: "philosophy"
},
{
label: "Guides",
autogenerate: { directory: "guides" }
items: [
{
label: "Getting Started",
link: "/guides/getting-started"
},
{
label: "Manual Setup",
link: "/guides/manual-setup"
},
{
label: "Registering Commands",
link: "/guides/registering-commands"
},
{
label: "Receiving Commands",
link: "/guides/receiving-commands"
}
]
},
{
label: "Reference",
autogenerate: { directory: "reference" }
label: "Documentation",
items: [
{
label: "JSX Components",
collapsed: true,
badge: {
text: "Beta",
variant: "danger"
},
items: [
{
label: "Configuring JSX",
link: "/docs/jsx/configuring-jsx"
},
{
label: "Embeds",
link: "docs/jsx/embed"
},
{
label: "Application Commands",
link: "docs/jsx/command"
},
{
label: "Message Components",
link: "docs/jsx/components"
},
{
label: "Attachments",
link: "docs/jsx/attachment"
}
]
},
{
label: "Handlers",
collapsed: true,
badge: {
text: "Beta",
variant: "danger"
},
items: [
{
label: "API",
link: "docs/handlers/the-api",
badge: {
text: "Internals",
variant: "success"
}
},
{
label: "Handling Application Commands",
link: "docs/handlers/application-commands"
},
{
label: "Handling Events",
link: "docs/handlers/events"
},
{
label: "Handling Message Commands",
link: "docs/handlers/message-commands"
}
]
}
]
}
]
})
Expand Down
Loading

0 comments on commit c5758a3

Please sign in to comment.