Skip to content

Commit

Permalink
Improved naming on simple handler
Browse files Browse the repository at this point in the history
This includes a breaking change in the types
  • Loading branch information
Didas-git committed Apr 13, 2024
1 parent e9b3dd1 commit fde2197
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/handlers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lilybird/handlers",
"version": "0.4.0-beta.1",
"version": "0.4.0-beta.2",
"description": "Command handlers and more for lilybird",
"main": "./dist/index.js",
"author": "DidaS",
Expand Down
2 changes: 1 addition & 1 deletion packages/handlers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ process.emitWarning("Deprecated import!", {
detail: "This way of importing the simple handler has been deprecated, please use `@lilybird/handlers/simple` instead."
});

export type * from "./simple/application-command.js";
export type * from "./simple/message-commands.js";
export type * from "./simple/slash-command.js";
export type * from "./simple/events.js";

export * from "./simple/handler.js";
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import type { ApplicationCommandData, AutocompleteData, GuildInteraction, Interaction } from "@lilybird/transformers";
import type { POSTApplicationCommandStructure, Awaitable } from "lilybird";

export interface GlobalSlashCommand {
type Guild = `${number}` | Array<`${number}`>;

export interface GlobalApplicationCommand {
data: POSTApplicationCommandStructure;
post: "GLOBAL";
autocomplete?: (interaction: Interaction<AutocompleteData>) => Awaitable<any>;
run: (interaction: Interaction<ApplicationCommandData>) => Awaitable<any>;
}

export interface GuildSlashCommand {
export interface GuildApplicationCommand {
data: POSTApplicationCommandStructure;
post: Guild;
autocomplete?: (interaction: GuildInteraction<AutocompleteData>) => Awaitable<any>;
run: (interaction: GuildInteraction<ApplicationCommandData>) => Awaitable<any>;
}

export type SlashCommand = GuildSlashCommand | GlobalSlashCommand;
export type ApplicationCommand = GuildApplicationCommand | GlobalApplicationCommand;

type Guild = `${number}` | Array<`${number}`>;
24 changes: 12 additions & 12 deletions packages/handlers/src/simple/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defaultTransformers } from "@lilybird/transformers";
import { join } from "node:path";

import type { GlobalSlashCommand, GuildSlashCommand, SlashCommand } from "./slash-command.js";
import type { GlobalApplicationCommand, GuildApplicationCommand, ApplicationCommand } from "./application-command.js";
import type { DefaultTransformers, Interaction, Message } from "@lilybird/transformers";
import type { MessageCommand } from "./message-commands.js";
import type { Event } from "./events.js";
Expand All @@ -19,8 +19,8 @@ interface HandlerDirectories {
}

export class Handler {
protected readonly guildSlashCommands = new Map<string, GuildSlashCommand>();
protected readonly globalSlashCommands = new Map<string, GlobalSlashCommand>();
protected readonly guildApplicationCommands = new Map<string, GuildApplicationCommand>();
protected readonly globalApplicationCommands = new Map<string, GlobalApplicationCommand>();
protected readonly messageCommands = new Map<string, MessageCommand>();
protected readonly events = new Map<string, Event>();
protected readonly messageCommandAliases = new Map<string, string>();
Expand All @@ -36,11 +36,11 @@ export class Handler {
}

public async registerGlobalCommands(client: Client): Promise<void> {
await client.rest.bulkOverwriteGlobalApplicationCommand(client.user.id, [...this.globalSlashCommands.values()].map((e) => e.data));
await client.rest.bulkOverwriteGlobalApplicationCommand(client.user.id, [...this.globalApplicationCommands.values()].map((e) => e.data));
}

public async registerGuildCommands(client: Client): Promise<void> {
for await (const command of this.guildSlashCommands.values()) {
for await (const command of this.guildApplicationCommands.values()) {
if (Array.isArray(command.post)) {
const temp: Array<Promise<unknown>> = [];
for (let i = 0; i < command.post.length; i++) temp.push(client.rest.createGuildApplicationCommand(client.user.id, command.post[i], command.data));
Expand All @@ -58,11 +58,11 @@ export class Handler {
if (fileName.endsWith(".d.ts")) continue;

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const command: SlashCommand = (await import(join(dir, fileName))).default;
const command: ApplicationCommand = (await import(join(dir, fileName))).default;
if (typeof command === "undefined") continue;

if (fileName.startsWith("/guild") || command.post !== "GLOBAL") this.guildSlashCommands.set(command.data.name, <GuildSlashCommand>command);
else this.globalSlashCommands.set(command.data.name, command);
if (fileName.startsWith("/guild") || command.post !== "GLOBAL") this.guildApplicationCommands.set(command.data.name, <GuildApplicationCommand>command);
else this.globalApplicationCommands.set(command.data.name, command);
}

return true;
Expand Down Expand Up @@ -111,11 +111,11 @@ export class Handler {

private async onInteraction(interaction: Interaction): Promise<void> {
if (interaction.isApplicationCommandInteraction()) {
await this.globalSlashCommands.get(interaction.data.name)?.run(interaction);
if (interaction.inGuild()) await this.guildSlashCommands.get(interaction.data.name)?.run(interaction);
await this.globalApplicationCommands.get(interaction.data.name)?.run(interaction);
if (interaction.inGuild()) await this.guildApplicationCommands.get(interaction.data.name)?.run(interaction);
} else if (interaction.isAutocompleteInteraction()) {
await this.globalSlashCommands.get(interaction.data.name)?.autocomplete?.(interaction);
if (interaction.inGuild()) await this.guildSlashCommands.get(interaction.data.name)?.autocomplete?.(interaction);
await this.globalApplicationCommands.get(interaction.data.name)?.autocomplete?.(interaction);
if (interaction.inGuild()) await this.guildApplicationCommands.get(interaction.data.name)?.autocomplete?.(interaction);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/handlers/src/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type * from "./application-command.js";
export type * from "./message-commands.js";
export type * from "./slash-command.js";
export type * from "./events.js";

export * from "./handler.js";

0 comments on commit fde2197

Please sign in to comment.