horizon.js 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +89 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +248 -0
- package/dist/index.mjs +212 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Andrew (e60m5ss / wlix)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<h1 align="center">horizon.js</h1>
|
|
2
|
+
|
|
3
|
+
> Discord.js v14 command and event handler
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install horizon.js
|
|
9
|
+
# or
|
|
10
|
+
yarn add horizon.js
|
|
11
|
+
# or
|
|
12
|
+
pnpm add horizon.js
|
|
13
|
+
# or
|
|
14
|
+
bun add horizon.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
`src/index.js`
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { Client, GatewayIntentBits } from "discord.js";
|
|
23
|
+
import { CommandHandler, EventHandler } from "horizon.js";
|
|
24
|
+
import url from "node:url";
|
|
25
|
+
import path from "node:path";
|
|
26
|
+
|
|
27
|
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
|
28
|
+
const client = new Client({
|
|
29
|
+
intents: [
|
|
30
|
+
GatewayIntentBits.Guilds,
|
|
31
|
+
GatewayIntentBits.GuildMessages,
|
|
32
|
+
GatewayIntentBits.MessageContent,
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
new CommandHandler({
|
|
37
|
+
client,
|
|
38
|
+
commandsPath: path.join(__dirname, "commands"),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
new EventHandler({
|
|
42
|
+
client,
|
|
43
|
+
eventsPath: path.join(__dirname, "events"),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
client.login(TOKEN);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`src/commands/General/ping.js`
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { SlashCommandBuilder } from "discord.js";
|
|
53
|
+
|
|
54
|
+
export const data = new SlashCommandBuilder()
|
|
55
|
+
.setName("ping")
|
|
56
|
+
.setDescription("Replies with Pong");
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {import("horizon.js").SlashCommandProps} param0
|
|
60
|
+
*/
|
|
61
|
+
export async function run({ client, interaction, handler }) {
|
|
62
|
+
await interaction.reply(`Pong! Latency is ${client.ws.ping} ms.`);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`src/events/messageCreate/hello.js`
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
/**
|
|
70
|
+
* @param {import("discord.js").Message} message
|
|
71
|
+
*/
|
|
72
|
+
export default async function (message) {
|
|
73
|
+
if (message.author.bot) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (message.content === "hello") {
|
|
78
|
+
await message.reply("Howdy! 👋");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Contributing
|
|
84
|
+
|
|
85
|
+
Pull requests are always welcomed. For more major changes, please open an issue to discuss what you wish to change.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
[MIT](LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Client, ChatInputCommandInteraction, AutocompleteInteraction, MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction } from 'discord.js';
|
|
2
|
+
|
|
3
|
+
type AnyCommandProps = SlashCommandProps | AnyContextMenuCommandProps;
|
|
4
|
+
type AnyContextMenuCommandProps = MessageContextMenuCommandProps | UserContextMenuCommandProps;
|
|
5
|
+
interface ApplicationCommand {
|
|
6
|
+
filePath: string;
|
|
7
|
+
name: string;
|
|
8
|
+
data: any;
|
|
9
|
+
run: (props: AnyCommandProps) => any;
|
|
10
|
+
autocomplete?: (props: AutocompleteProps) => any;
|
|
11
|
+
category: string;
|
|
12
|
+
}
|
|
13
|
+
interface AutocompleteProps {
|
|
14
|
+
client: Client;
|
|
15
|
+
interaction: AutocompleteInteraction;
|
|
16
|
+
handler: CommandHandler;
|
|
17
|
+
}
|
|
18
|
+
interface ClientEvent {
|
|
19
|
+
event: string;
|
|
20
|
+
filePath: string;
|
|
21
|
+
run: (...args: any[]) => any;
|
|
22
|
+
once: boolean;
|
|
23
|
+
}
|
|
24
|
+
interface CommandHandlerProps {
|
|
25
|
+
client: Client;
|
|
26
|
+
commandsPath: string;
|
|
27
|
+
}
|
|
28
|
+
interface EventHandlerProps {
|
|
29
|
+
client: Client;
|
|
30
|
+
eventsPath: string;
|
|
31
|
+
}
|
|
32
|
+
interface MessageContextMenuCommandProps {
|
|
33
|
+
client: Client;
|
|
34
|
+
interaction: MessageContextMenuCommandInteraction;
|
|
35
|
+
handler: CommandHandler;
|
|
36
|
+
}
|
|
37
|
+
interface SlashCommandProps {
|
|
38
|
+
client: Client<true>;
|
|
39
|
+
interaction: ChatInputCommandInteraction;
|
|
40
|
+
handler: CommandHandler;
|
|
41
|
+
}
|
|
42
|
+
interface UserContextMenuCommandProps {
|
|
43
|
+
client: Client;
|
|
44
|
+
interaction: UserContextMenuCommandInteraction;
|
|
45
|
+
handler: CommandHandler;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare class CommandHandler {
|
|
49
|
+
#private;
|
|
50
|
+
client: Client;
|
|
51
|
+
commandsPath: string;
|
|
52
|
+
commands: ApplicationCommand[];
|
|
53
|
+
constructor(options: CommandHandlerProps);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
declare class EventHandler {
|
|
57
|
+
#private;
|
|
58
|
+
client: Client;
|
|
59
|
+
eventsPath: string;
|
|
60
|
+
events: ClientEvent[];
|
|
61
|
+
constructor({ client, eventsPath }: EventHandlerProps);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { type AnyCommandProps, type AnyContextMenuCommandProps, type ApplicationCommand, type AutocompleteProps, type ClientEvent, CommandHandler, type CommandHandlerProps, EventHandler, type EventHandlerProps, type MessageContextMenuCommandProps, type SlashCommandProps, type UserContextMenuCommandProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Client, ChatInputCommandInteraction, AutocompleteInteraction, MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction } from 'discord.js';
|
|
2
|
+
|
|
3
|
+
type AnyCommandProps = SlashCommandProps | AnyContextMenuCommandProps;
|
|
4
|
+
type AnyContextMenuCommandProps = MessageContextMenuCommandProps | UserContextMenuCommandProps;
|
|
5
|
+
interface ApplicationCommand {
|
|
6
|
+
filePath: string;
|
|
7
|
+
name: string;
|
|
8
|
+
data: any;
|
|
9
|
+
run: (props: AnyCommandProps) => any;
|
|
10
|
+
autocomplete?: (props: AutocompleteProps) => any;
|
|
11
|
+
category: string;
|
|
12
|
+
}
|
|
13
|
+
interface AutocompleteProps {
|
|
14
|
+
client: Client;
|
|
15
|
+
interaction: AutocompleteInteraction;
|
|
16
|
+
handler: CommandHandler;
|
|
17
|
+
}
|
|
18
|
+
interface ClientEvent {
|
|
19
|
+
event: string;
|
|
20
|
+
filePath: string;
|
|
21
|
+
run: (...args: any[]) => any;
|
|
22
|
+
once: boolean;
|
|
23
|
+
}
|
|
24
|
+
interface CommandHandlerProps {
|
|
25
|
+
client: Client;
|
|
26
|
+
commandsPath: string;
|
|
27
|
+
}
|
|
28
|
+
interface EventHandlerProps {
|
|
29
|
+
client: Client;
|
|
30
|
+
eventsPath: string;
|
|
31
|
+
}
|
|
32
|
+
interface MessageContextMenuCommandProps {
|
|
33
|
+
client: Client;
|
|
34
|
+
interaction: MessageContextMenuCommandInteraction;
|
|
35
|
+
handler: CommandHandler;
|
|
36
|
+
}
|
|
37
|
+
interface SlashCommandProps {
|
|
38
|
+
client: Client<true>;
|
|
39
|
+
interaction: ChatInputCommandInteraction;
|
|
40
|
+
handler: CommandHandler;
|
|
41
|
+
}
|
|
42
|
+
interface UserContextMenuCommandProps {
|
|
43
|
+
client: Client;
|
|
44
|
+
interaction: UserContextMenuCommandInteraction;
|
|
45
|
+
handler: CommandHandler;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare class CommandHandler {
|
|
49
|
+
#private;
|
|
50
|
+
client: Client;
|
|
51
|
+
commandsPath: string;
|
|
52
|
+
commands: ApplicationCommand[];
|
|
53
|
+
constructor(options: CommandHandlerProps);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
declare class EventHandler {
|
|
57
|
+
#private;
|
|
58
|
+
client: Client;
|
|
59
|
+
eventsPath: string;
|
|
60
|
+
events: ClientEvent[];
|
|
61
|
+
constructor({ client, eventsPath }: EventHandlerProps);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { type AnyCommandProps, type AnyContextMenuCommandProps, type ApplicationCommand, type AutocompleteProps, type ClientEvent, CommandHandler, type CommandHandlerProps, EventHandler, type EventHandlerProps, type MessageContextMenuCommandProps, type SlashCommandProps, type UserContextMenuCommandProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
CommandHandler: () => CommandHandler,
|
|
35
|
+
EventHandler: () => EventHandler
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
|
|
39
|
+
// src/structures/CommandHandler.ts
|
|
40
|
+
var import_node_fs = __toESM(require("node:fs"));
|
|
41
|
+
var import_node_path = __toESM(require("node:path"));
|
|
42
|
+
var import_node_url = __toESM(require("node:url"));
|
|
43
|
+
var validExtensions = [".js", ".cjs", ".mjs", ".ts", ".cts", ".mts"];
|
|
44
|
+
var CommandHandler = class {
|
|
45
|
+
static {
|
|
46
|
+
__name(this, "CommandHandler");
|
|
47
|
+
}
|
|
48
|
+
client;
|
|
49
|
+
commandsPath;
|
|
50
|
+
commands = [];
|
|
51
|
+
/**
|
|
52
|
+
* Instantiate a new CommandHandler
|
|
53
|
+
* @param options Handler options
|
|
54
|
+
*/
|
|
55
|
+
constructor(options) {
|
|
56
|
+
this.client = options.client;
|
|
57
|
+
this.commandsPath = options.commandsPath;
|
|
58
|
+
this.client.once("clientReady", async () => {
|
|
59
|
+
await this.#loadCommands(this.commandsPath);
|
|
60
|
+
await this.#registerCommands();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Loads commands from the provided directory and parent directories
|
|
65
|
+
* Only selects commands ending in validExtensions
|
|
66
|
+
* Ignores files with names that start with "."
|
|
67
|
+
*/
|
|
68
|
+
async #loadCommands(directory) {
|
|
69
|
+
const entries = import_node_fs.default.readdirSync(directory, { withFileTypes: true });
|
|
70
|
+
for (const entry of entries) {
|
|
71
|
+
if (entry.name.startsWith(".")) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const fullPath = import_node_path.default.join(directory, entry.name);
|
|
75
|
+
if (entry.isDirectory()) {
|
|
76
|
+
await this.#loadCommands(fullPath);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (!validExtensions.includes(import_node_path.default.extname(entry.name))) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const module2 = await import(import_node_url.default.pathToFileURL(fullPath).href);
|
|
83
|
+
if (!module2?.data || !module2?.run) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const name = module2.data?.name ?? (typeof module2.data?.toJSON === "function" ? module2.data.toJSON().name : null);
|
|
87
|
+
if (!name) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
this.commands.push({
|
|
91
|
+
name,
|
|
92
|
+
filePath: fullPath,
|
|
93
|
+
data: module2.data,
|
|
94
|
+
run: module2.run,
|
|
95
|
+
autocomplete: module2.autocomplete,
|
|
96
|
+
category: import_node_path.default.dirname(import_node_path.default.relative(this.commandsPath, fullPath)).split(import_node_path.default.sep)[0] || "None"
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Registers the loaded commands against Discord's API
|
|
102
|
+
* Then, listens to them via interactionCreate
|
|
103
|
+
*/
|
|
104
|
+
async #registerCommands() {
|
|
105
|
+
if (!this.client.application) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const commandData = this.commands.map(
|
|
109
|
+
({ data }) => typeof data.toJSON === "function" ? data.toJSON() : data
|
|
110
|
+
);
|
|
111
|
+
const { size } = await this.client.application.commands.set(commandData);
|
|
112
|
+
this.client.on("interactionCreate", async (interaction) => {
|
|
113
|
+
if (interaction.isChatInputCommand()) {
|
|
114
|
+
const command = this.commands.find(
|
|
115
|
+
({ name }) => name === interaction.commandName
|
|
116
|
+
);
|
|
117
|
+
if (command?.run) {
|
|
118
|
+
try {
|
|
119
|
+
await command.run({
|
|
120
|
+
client: this.client,
|
|
121
|
+
interaction,
|
|
122
|
+
handler: this
|
|
123
|
+
});
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const reply = {
|
|
126
|
+
flags: "Ephemeral",
|
|
127
|
+
content: "An unexpected error occurred, please try again later."
|
|
128
|
+
};
|
|
129
|
+
try {
|
|
130
|
+
if (interaction.replied || interaction.deferred) {
|
|
131
|
+
await interaction.followUp(reply);
|
|
132
|
+
} else {
|
|
133
|
+
await interaction.reply(reply);
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (interaction.isAutocomplete()) {
|
|
141
|
+
const command = this.commands.find(
|
|
142
|
+
({ name }) => name === interaction.commandName
|
|
143
|
+
);
|
|
144
|
+
if (command?.autocomplete) {
|
|
145
|
+
try {
|
|
146
|
+
await command.autocomplete({
|
|
147
|
+
client: this.client,
|
|
148
|
+
interaction,
|
|
149
|
+
handler: this
|
|
150
|
+
});
|
|
151
|
+
} catch {
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (interaction.isContextMenuCommand()) {
|
|
156
|
+
const command = this.commands.find(
|
|
157
|
+
({ name }) => name === interaction.commandName
|
|
158
|
+
);
|
|
159
|
+
if (command?.run) {
|
|
160
|
+
try {
|
|
161
|
+
await command.run({
|
|
162
|
+
client: this.client,
|
|
163
|
+
interaction,
|
|
164
|
+
handler: this
|
|
165
|
+
});
|
|
166
|
+
} catch {
|
|
167
|
+
const reply = {
|
|
168
|
+
flags: "Ephemeral",
|
|
169
|
+
content: "An unexpected error occurred, please try again later."
|
|
170
|
+
};
|
|
171
|
+
try {
|
|
172
|
+
if (interaction.replied || interaction.deferred) {
|
|
173
|
+
await interaction.followUp(reply);
|
|
174
|
+
} else {
|
|
175
|
+
await interaction.reply(reply);
|
|
176
|
+
}
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/structures/EventHandler.ts
|
|
187
|
+
var import_node_path2 = __toESM(require("node:path"));
|
|
188
|
+
var import_node_url2 = __toESM(require("node:url"));
|
|
189
|
+
var import_node_fs2 = __toESM(require("node:fs"));
|
|
190
|
+
var validExtensions2 = [".js", ".cjs", ".mjs", ".ts", ".cts", ".mts"];
|
|
191
|
+
var EventHandler = class {
|
|
192
|
+
static {
|
|
193
|
+
__name(this, "EventHandler");
|
|
194
|
+
}
|
|
195
|
+
client;
|
|
196
|
+
eventsPath;
|
|
197
|
+
events = [];
|
|
198
|
+
constructor({ client, eventsPath }) {
|
|
199
|
+
this.client = client;
|
|
200
|
+
this.eventsPath = eventsPath;
|
|
201
|
+
this.#loadEvents(eventsPath).then(() => {
|
|
202
|
+
this.#registerEvents();
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async #loadEvents(directory, topLevelFolder) {
|
|
206
|
+
const entries = import_node_fs2.default.readdirSync(directory, { withFileTypes: true });
|
|
207
|
+
for (const entry of entries) {
|
|
208
|
+
if (entry.name.startsWith(".")) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const fullPath = import_node_path2.default.join(directory, entry.name);
|
|
212
|
+
if (entry.isDirectory()) {
|
|
213
|
+
await this.#loadEvents(fullPath, topLevelFolder ?? entry.name);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (!validExtensions2.includes(import_node_path2.default.extname(entry.name))) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const module2 = await import(import_node_url2.default.pathToFileURL(fullPath).href);
|
|
220
|
+
if (!module2?.default) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
this.events.push({
|
|
224
|
+
event: topLevelFolder,
|
|
225
|
+
filePath: fullPath,
|
|
226
|
+
run: module2.default,
|
|
227
|
+
once: Boolean(module2.once)
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
#registerEvents() {
|
|
232
|
+
for (const event of this.events) {
|
|
233
|
+
try {
|
|
234
|
+
if (event.once) {
|
|
235
|
+
this.client.once(event.event, (...args) => event.run(...args));
|
|
236
|
+
} else {
|
|
237
|
+
this.client.on(event.event, (...args) => event.run(...args));
|
|
238
|
+
}
|
|
239
|
+
} catch (error) {
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
245
|
+
0 && (module.exports = {
|
|
246
|
+
CommandHandler,
|
|
247
|
+
EventHandler
|
|
248
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/structures/CommandHandler.ts
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import url from "node:url";
|
|
8
|
+
var validExtensions = [".js", ".cjs", ".mjs", ".ts", ".cts", ".mts"];
|
|
9
|
+
var CommandHandler = class {
|
|
10
|
+
static {
|
|
11
|
+
__name(this, "CommandHandler");
|
|
12
|
+
}
|
|
13
|
+
client;
|
|
14
|
+
commandsPath;
|
|
15
|
+
commands = [];
|
|
16
|
+
/**
|
|
17
|
+
* Instantiate a new CommandHandler
|
|
18
|
+
* @param options Handler options
|
|
19
|
+
*/
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.client = options.client;
|
|
22
|
+
this.commandsPath = options.commandsPath;
|
|
23
|
+
this.client.once("clientReady", async () => {
|
|
24
|
+
await this.#loadCommands(this.commandsPath);
|
|
25
|
+
await this.#registerCommands();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Loads commands from the provided directory and parent directories
|
|
30
|
+
* Only selects commands ending in validExtensions
|
|
31
|
+
* Ignores files with names that start with "."
|
|
32
|
+
*/
|
|
33
|
+
async #loadCommands(directory) {
|
|
34
|
+
const entries = fs.readdirSync(directory, { withFileTypes: true });
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
if (entry.name.startsWith(".")) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const fullPath = path.join(directory, entry.name);
|
|
40
|
+
if (entry.isDirectory()) {
|
|
41
|
+
await this.#loadCommands(fullPath);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!validExtensions.includes(path.extname(entry.name))) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const module = await import(url.pathToFileURL(fullPath).href);
|
|
48
|
+
if (!module?.data || !module?.run) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const name = module.data?.name ?? (typeof module.data?.toJSON === "function" ? module.data.toJSON().name : null);
|
|
52
|
+
if (!name) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
this.commands.push({
|
|
56
|
+
name,
|
|
57
|
+
filePath: fullPath,
|
|
58
|
+
data: module.data,
|
|
59
|
+
run: module.run,
|
|
60
|
+
autocomplete: module.autocomplete,
|
|
61
|
+
category: path.dirname(path.relative(this.commandsPath, fullPath)).split(path.sep)[0] || "None"
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Registers the loaded commands against Discord's API
|
|
67
|
+
* Then, listens to them via interactionCreate
|
|
68
|
+
*/
|
|
69
|
+
async #registerCommands() {
|
|
70
|
+
if (!this.client.application) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const commandData = this.commands.map(
|
|
74
|
+
({ data }) => typeof data.toJSON === "function" ? data.toJSON() : data
|
|
75
|
+
);
|
|
76
|
+
const { size } = await this.client.application.commands.set(commandData);
|
|
77
|
+
this.client.on("interactionCreate", async (interaction) => {
|
|
78
|
+
if (interaction.isChatInputCommand()) {
|
|
79
|
+
const command = this.commands.find(
|
|
80
|
+
({ name }) => name === interaction.commandName
|
|
81
|
+
);
|
|
82
|
+
if (command?.run) {
|
|
83
|
+
try {
|
|
84
|
+
await command.run({
|
|
85
|
+
client: this.client,
|
|
86
|
+
interaction,
|
|
87
|
+
handler: this
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const reply = {
|
|
91
|
+
flags: "Ephemeral",
|
|
92
|
+
content: "An unexpected error occurred, please try again later."
|
|
93
|
+
};
|
|
94
|
+
try {
|
|
95
|
+
if (interaction.replied || interaction.deferred) {
|
|
96
|
+
await interaction.followUp(reply);
|
|
97
|
+
} else {
|
|
98
|
+
await interaction.reply(reply);
|
|
99
|
+
}
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (interaction.isAutocomplete()) {
|
|
106
|
+
const command = this.commands.find(
|
|
107
|
+
({ name }) => name === interaction.commandName
|
|
108
|
+
);
|
|
109
|
+
if (command?.autocomplete) {
|
|
110
|
+
try {
|
|
111
|
+
await command.autocomplete({
|
|
112
|
+
client: this.client,
|
|
113
|
+
interaction,
|
|
114
|
+
handler: this
|
|
115
|
+
});
|
|
116
|
+
} catch {
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (interaction.isContextMenuCommand()) {
|
|
121
|
+
const command = this.commands.find(
|
|
122
|
+
({ name }) => name === interaction.commandName
|
|
123
|
+
);
|
|
124
|
+
if (command?.run) {
|
|
125
|
+
try {
|
|
126
|
+
await command.run({
|
|
127
|
+
client: this.client,
|
|
128
|
+
interaction,
|
|
129
|
+
handler: this
|
|
130
|
+
});
|
|
131
|
+
} catch {
|
|
132
|
+
const reply = {
|
|
133
|
+
flags: "Ephemeral",
|
|
134
|
+
content: "An unexpected error occurred, please try again later."
|
|
135
|
+
};
|
|
136
|
+
try {
|
|
137
|
+
if (interaction.replied || interaction.deferred) {
|
|
138
|
+
await interaction.followUp(reply);
|
|
139
|
+
} else {
|
|
140
|
+
await interaction.reply(reply);
|
|
141
|
+
}
|
|
142
|
+
} catch {
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// src/structures/EventHandler.ts
|
|
152
|
+
import path2 from "node:path";
|
|
153
|
+
import url2 from "node:url";
|
|
154
|
+
import fs2 from "node:fs";
|
|
155
|
+
var validExtensions2 = [".js", ".cjs", ".mjs", ".ts", ".cts", ".mts"];
|
|
156
|
+
var EventHandler = class {
|
|
157
|
+
static {
|
|
158
|
+
__name(this, "EventHandler");
|
|
159
|
+
}
|
|
160
|
+
client;
|
|
161
|
+
eventsPath;
|
|
162
|
+
events = [];
|
|
163
|
+
constructor({ client, eventsPath }) {
|
|
164
|
+
this.client = client;
|
|
165
|
+
this.eventsPath = eventsPath;
|
|
166
|
+
this.#loadEvents(eventsPath).then(() => {
|
|
167
|
+
this.#registerEvents();
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async #loadEvents(directory, topLevelFolder) {
|
|
171
|
+
const entries = fs2.readdirSync(directory, { withFileTypes: true });
|
|
172
|
+
for (const entry of entries) {
|
|
173
|
+
if (entry.name.startsWith(".")) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const fullPath = path2.join(directory, entry.name);
|
|
177
|
+
if (entry.isDirectory()) {
|
|
178
|
+
await this.#loadEvents(fullPath, topLevelFolder ?? entry.name);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (!validExtensions2.includes(path2.extname(entry.name))) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const module = await import(url2.pathToFileURL(fullPath).href);
|
|
185
|
+
if (!module?.default) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
this.events.push({
|
|
189
|
+
event: topLevelFolder,
|
|
190
|
+
filePath: fullPath,
|
|
191
|
+
run: module.default,
|
|
192
|
+
once: Boolean(module.once)
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
#registerEvents() {
|
|
197
|
+
for (const event of this.events) {
|
|
198
|
+
try {
|
|
199
|
+
if (event.once) {
|
|
200
|
+
this.client.once(event.event, (...args) => event.run(...args));
|
|
201
|
+
} else {
|
|
202
|
+
this.client.on(event.event, (...args) => event.run(...args));
|
|
203
|
+
}
|
|
204
|
+
} catch (error) {
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
export {
|
|
210
|
+
CommandHandler,
|
|
211
|
+
EventHandler
|
|
212
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "horizon.js",
|
|
3
|
+
"description": "Discord.js v14 command and event handler",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"url": "https://github.com/e60m5ss/horizon.js"
|
|
7
|
+
},
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "e60m5ss",
|
|
10
|
+
"url": "https://github.com/e60m5ss"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"command-handler",
|
|
14
|
+
"commands",
|
|
15
|
+
"discord",
|
|
16
|
+
"discord-bot",
|
|
17
|
+
"discord.js",
|
|
18
|
+
"event-handler",
|
|
19
|
+
"events"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"prettier": {
|
|
25
|
+
"jsxSingleQuote": false,
|
|
26
|
+
"printWidth": 85,
|
|
27
|
+
"semi": true,
|
|
28
|
+
"singleQuote": false,
|
|
29
|
+
"tabWidth": 4,
|
|
30
|
+
"trailingComma": "es5",
|
|
31
|
+
"useTabs": false
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^25.2.0",
|
|
35
|
+
"prettier": "^3.8.1",
|
|
36
|
+
"tsup": "^8.5.1",
|
|
37
|
+
"typescript": "^5.9.3"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"discord.js": "^14.25.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"format": "prettier ./src --write --ignore-path=.prettierignore",
|
|
45
|
+
"lint": "tsc --noEmit; prettier ./src --check --ignore-path=.prettierignore",
|
|
46
|
+
"prepublish": "tsup"
|
|
47
|
+
}
|
|
48
|
+
}
|