Skip to content

Commit ab361ea

Browse files
authored
Merge pull request #75 from StelliaJS/develop
release: V1.5.4
2 parents 9e86fef + f996862 commit ab361ea

14 files changed

Lines changed: 115 additions & 86 deletions

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Recommended architecture for StelliaJS project.
1111

1212
```
1313
.
14-
├── dist // Build folder
1514
├── src/
1615
│ ├── commands/
1716
│ │ ├── contextMenus/
@@ -41,7 +40,7 @@ Recommended architecture for StelliaJS project.
4140
│ └── index.ts
4241
├── .env
4342
├── package.json
44-
├── package-lock.json
43+
├── pnpm-lock.yaml
4544
├── stellia.json
4645
└── tsconfig.json
4746
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stelliajs/framework",
3-
"version": "1.5.3",
3+
"version": "1.5.4",
44
"main": "./dist/index.js",
55
"types": "./dist/index.d.ts",
66
"scripts": {

src/client/StelliaClient.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from "node:fs";
2-
import path from "path";
3-
import { pathToFileURL } from "url";
2+
import path from "node:path";
3+
import { pathToFileURL } from "node:url";
44
import { Client, type ClientOptions, type Interaction } from "discord.js";
55
import { StelliaUtils } from "@client/index.js";
66
import {
@@ -14,8 +14,8 @@ import {
1414
SelectMenuManager
1515
} from "@managers/index.js";
1616
import {
17-
type ClientEnvironment, type GuildConfiguration,
18-
type GuildConfigurationType,
17+
type ClientEnvironment,
18+
type GuildConfiguration,
1919
type GuildsConfiguration,
2020
type Manager,
2121
type Managers
@@ -102,26 +102,24 @@ export class StelliaClient<Ready extends boolean = boolean> extends Client<Ready
102102
return new Promise((resolve, reject) => {
103103
fs.readFile(filePath, async (err, data) => {
104104
if (err) {
105-
return reject(err);
105+
return reject(new Error("stellia.json file not found"));
106106
}
107107

108-
try {
109-
const environments = JSON.parse(data.toString()).environments;
110-
if (!Object.keys(environments).includes(chosenEnvironment)) {
111-
return reject(new Error("Invalid environment"));
112-
}
113-
114-
const environmentData = environments[chosenEnvironment];
115-
const environmentPath = environmentData.production
116-
? StelliaClient.convertFilePathToProduction(environmentData.file)
117-
: environmentData.file;
118-
const environmentAbsolutePath = pathToFileURL(path.join(srcPath, "..", environmentPath)).href;
119-
const environmentFile = await import(environmentAbsolutePath);
120-
resolve(environmentFile.environment);
121-
} catch (error) {
122-
reject(error);
123-
}
124-
});
108+
const environments = JSON.parse(data.toString()).environments;
109+
if (!Object.keys(environments).includes(chosenEnvironment)) {
110+
return reject(new Error("Invalid environment"));
111+
}
112+
113+
const environmentData = environments[chosenEnvironment];
114+
const environmentPath = environmentData.production
115+
? StelliaClient.convertFilePathToProduction(environmentData.file)
116+
: environmentData.file;
117+
118+
const environmentAbsolutePath = pathToFileURL(path.join(srcPath, "..", environmentPath)).href;
119+
import(environmentAbsolutePath)
120+
.then((environmentFile) => resolve(environmentFile.environment))
121+
.catch(() => reject(new Error("Error parsing stellia.json file")));
122+
});
125123
});
126124
};
127125

@@ -133,14 +131,14 @@ export class StelliaClient<Ready extends boolean = boolean> extends Client<Ready
133131
await this.utils.handleInteraction(interaction);
134132
};
135133

136-
private areManagersLoaded = (): boolean => {
134+
private readonly areManagersLoaded = (): boolean => {
137135
const managers = Object.values(this.managers);
138136
return managers.length === 0
139137
? true
140138
: managers.every((manager: Manager) => manager.isManagerLoaded());
141139
};
142140

143-
private static convertFilePathToProduction = (filePath: string): string => {
141+
private static readonly convertFilePathToProduction = (filePath: string): string => {
144142
return filePath.replace("src", "dist").replace(".ts", ".js");
145143
};
146144
}

src/client/StelliaUtils.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class StelliaUtils {
110110
}
111111
};
112112

113-
private handleAutoCompleteInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
113+
private readonly handleAutoCompleteInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
114114
try {
115115
const autoCompleteInteraction = interaction as AutocompleteInteraction<"cached">;
116116
const autoCompleteManager = this.client.managers.autoCompletes;
@@ -132,7 +132,7 @@ export class StelliaUtils {
132132
}
133133
};
134134

135-
private handleButtonInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
135+
private readonly handleButtonInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
136136
try {
137137
const buttonInteraction = interaction as ButtonInteraction<"cached">;
138138
const buttonManager = this.client.managers.buttons;
@@ -160,7 +160,7 @@ export class StelliaUtils {
160160
}
161161
};
162162

163-
private handleCommandInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
163+
private readonly handleCommandInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
164164
try {
165165
const commandInteraction = interaction as ChatInputCommandInteraction<"cached">;
166166
const commandManager = this.client.managers.commands;
@@ -186,7 +186,7 @@ export class StelliaUtils {
186186
}
187187
};
188188

189-
private handleContextMenuInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
189+
private readonly handleContextMenuInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
190190
try {
191191
const interactionContextMenu = interaction as ContextMenuCommandInteraction<"cached">;
192192
if (interactionContextMenu.commandType === ApplicationCommandType.Message) {
@@ -201,7 +201,7 @@ export class StelliaUtils {
201201
}
202202
};
203203

204-
private handleModalInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
204+
private readonly handleModalInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
205205
try {
206206
const modalInteraction = interaction as ModalSubmitInteraction<"cached">;
207207
const modalManager = this.client.managers.modals;
@@ -229,7 +229,7 @@ export class StelliaUtils {
229229
}
230230
};
231231

232-
private handleSelectMenuInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
232+
private readonly handleSelectMenuInteraction = async (interaction: Interaction<"cached">): Promise<void> => {
233233
try {
234234
const selectMenuInteraction = interaction as AnySelectMenuInteraction<"cached">;
235235
const selectMenuManager = this.client.managers.selectMenus;
@@ -257,7 +257,7 @@ export class StelliaUtils {
257257
}
258258
};
259259

260-
private handleMessageContextMenuInteraction = async (interaction: MessageContextMenuCommandInteraction<"cached">): Promise<void> => {
260+
private readonly handleMessageContextMenuInteraction = async (interaction: MessageContextMenuCommandInteraction<"cached">): Promise<void> => {
261261
try {
262262
const contextMenuManager = this.client.managers.contextMenus;
263263
if (!contextMenuManager) return;
@@ -282,7 +282,7 @@ export class StelliaUtils {
282282
}
283283
};
284284

285-
private handleUserContextMenuInteraction = async (interaction: UserContextMenuCommandInteraction<"cached">): Promise<void> => {
285+
private readonly handleUserContextMenuInteraction = async (interaction: UserContextMenuCommandInteraction<"cached">): Promise<void> => {
286286
try {
287287
const contextMenuManager = this.client.managers.contextMenus;
288288
if (!contextMenuManager) return;

src/managers/AutoCompleteManager.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ export class AutoCompleteManager extends BaseManager {
1818
this.setManagerLoaded();
1919
}
2020

21-
public getByCustomId<AutoCompleteStructure>(id: InteractionCustomId): AutoCompleteStructure | undefined {
22-
const autoComplete = (this.interactions.get(id) as AutoCompleteStructure) ?? undefined;
21+
public getByCustomId<AutoCompleteStructure>(id: InteractionCustomId): AutoCompleteStructure | null {
22+
const autoComplete = (this.interactions.get(id) as AutoCompleteStructure) ?? null;
2323
return autoComplete;
2424
}
2525

26-
public getByRegex<AutoCompleteStructure>(id: InteractionCustomId): AutoCompleteStructure | undefined {
27-
return undefined;
26+
public getByRegex<AutoCompleteStructure>(id: InteractionCustomId): AutoCompleteStructure | null {
27+
let autoComplete = null;
28+
for (const [customId, action] of this.interactions.entries()) {
29+
if (customId instanceof RegExp && customId.test(id)) {
30+
autoComplete = action as AutoCompleteStructure;
31+
break;
32+
}
33+
}
34+
35+
return autoComplete;
2836
}
2937

3038
public getAll<AutoCompleteStructure>(): Collection<StructureCustomId, AutoCompleteStructure> {

src/managers/BaseManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export abstract class BaseManager {
2626
this.isLoaded = true;
2727
}
2828

29-
public abstract loadData(): void;
30-
public abstract getByCustomId<InteractionStructure extends AnyInteractionStructure>(id: InteractionCustomId): InteractionStructure | undefined;
31-
public abstract getByRegex<InteractionStructure extends AnyInteractionStructure>(id: InteractionCustomId): InteractionStructure | undefined;
29+
public abstract loadData(): Promise<void>;
30+
public abstract getByCustomId<InteractionStructure extends AnyInteractionStructure>(id: InteractionCustomId): InteractionStructure | null;
31+
public abstract getByRegex<InteractionStructure extends AnyInteractionStructure>(id: InteractionCustomId): InteractionStructure | null;
3232
public abstract getAll<InteractionStructure extends AnyInteractionStructure>(): Collection<StructureCustomId, InteractionStructure>;
3333
}

src/managers/ButtonManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export class ButtonManager extends BaseManager {
1818
this.setManagerLoaded();
1919
}
2020

21-
public getByCustomId<ButtonStructure>(id: InteractionCustomId): ButtonStructure | undefined {
22-
const button = (this.interactions.get(id) as ButtonStructure) ?? undefined;
21+
public getByCustomId<ButtonStructure>(id: InteractionCustomId): ButtonStructure | null {
22+
const button = (this.interactions.get(id) as ButtonStructure) ?? null;
2323
return button;
2424
}
2525

26-
public getByRegex<ButtonStructure>(id: InteractionCustomId): ButtonStructure | undefined {
27-
let button;
26+
public getByRegex<ButtonStructure>(id: InteractionCustomId): ButtonStructure | null {
27+
let button = null;
2828
for (const [customId, action] of this.interactions.entries()) {
2929
if (customId instanceof RegExp && customId.test(id)) {
3030
button = action as ButtonStructure;

src/managers/CommandManager.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ export class CommandManager extends BaseManager {
1818
this.setManagerLoaded();
1919
}
2020

21-
public getByCustomId<CommandStructure>(id: InteractionCustomId): CommandStructure | undefined {
22-
const command = (this.interactions.get(id) as CommandStructure) ?? undefined;
21+
public getByCustomId<CommandStructure>(id: InteractionCustomId): CommandStructure | null {
22+
const command = (this.interactions.get(id) as CommandStructure) ?? null;
2323
return command;
2424
}
2525

26-
public getByRegex<CommandStructure>(id: InteractionCustomId): CommandStructure | undefined {
27-
return undefined;
26+
public getByRegex<CommandStructure>(id: InteractionCustomId): CommandStructure | null {
27+
let command = null;
28+
for (const [customId, action] of this.interactions.entries()) {
29+
if (customId instanceof RegExp && customId.test(id)) {
30+
command = action as CommandStructure;
31+
break;
32+
}
33+
}
34+
35+
return command;
2836
}
2937

3038
public getAll<CommandStructure>(): Collection<StructureCustomId, CommandStructure> {

src/managers/ContextMenuManager.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ export class ContextMenuManager extends BaseManager {
1818
this.setManagerLoaded();
1919
}
2020

21-
public getByCustomId<ContextMenuStructure>(id: InteractionCustomId): ContextMenuStructure | undefined {
22-
const contextMenu = (this.interactions.get(id) as ContextMenuStructure) ?? undefined;
21+
public getByCustomId<ContextMenuStructure>(id: InteractionCustomId): ContextMenuStructure | null {
22+
const contextMenu = (this.interactions.get(id) as ContextMenuStructure) ?? null;
2323
return contextMenu;
2424
}
2525

26-
public getByRegex<ContextMenuStructure>(id: InteractionCustomId): ContextMenuStructure | undefined {
27-
return undefined;
26+
public getByRegex<ContextMenuStructure>(id: InteractionCustomId): ContextMenuStructure | null {
27+
let contextMenu = null;
28+
for (const [customId, action] of this.interactions.entries()) {
29+
if (customId instanceof RegExp && customId.test(id)) {
30+
contextMenu = action as ContextMenuStructure;
31+
break;
32+
}
33+
}
34+
35+
return contextMenu;
2836
}
2937

3038
public getAll<ContextMenuStructure>(): Collection<StructureCustomId, ContextMenuStructure> {

src/managers/EventManager.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,21 @@ export class EventManager extends BaseManager {
5151
this.setManagerLoaded();
5252
}
5353

54-
public getByCustomId<EventStructure>(id: InteractionCustomId): EventStructure | undefined {
55-
const event = (this.interactions.get(id) as EventStructure) ?? undefined;
54+
public getByCustomId<EventStructure>(id: InteractionCustomId): EventStructure | null {
55+
const event = (this.interactions.get(id) as EventStructure) ?? null;
5656
return event;
5757
}
5858

59-
public getByRegex<EventStructure>(id: InteractionCustomId): EventStructure | undefined {
60-
return undefined;
59+
public getByRegex<EventStructure>(id: InteractionCustomId): EventStructure | null {
60+
let event = null;
61+
for (const [customId, action] of this.interactions.entries()) {
62+
if (customId instanceof RegExp && customId.test(id)) {
63+
event = action as EventStructure;
64+
break;
65+
}
66+
}
67+
68+
return event;
6169
}
6270

6371
public getAll<EventStructure>(): Collection<StructureCustomId, EventStructure> {
@@ -76,7 +84,7 @@ export class EventManager extends BaseManager {
7684
}
7785
}
7886

79-
private eventHandler = (event: EventStructureWithConfiguration, ...args: ClientEventsArgs) => {
87+
private readonly eventHandler = (event: EventStructureWithConfiguration, ...args: ClientEventsArgs) => {
8088
const mainArgument = args[0];
8189
const guildConfiguration = this.getGuildConfiguration(mainArgument);
8290
if (guildConfiguration) {

0 commit comments

Comments
 (0)