zumito-framework 1.2.4 → 1.2.6

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.
@@ -18,6 +18,7 @@ import path from 'path';
18
18
  import { EventManager } from './services/EventManager.js';
19
19
  import { CommandManager } from './services/CommandManager.js';
20
20
  import { ModuleManager } from './services/ModuleManager.js';
21
+ import { ServiceContainer } from './services/ServiceContainer.js';
21
22
  // import better-logging
22
23
  betterLogging(console);
23
24
  /**
@@ -135,6 +136,9 @@ export class ZumitoFramework {
135
136
  if (settings.logLevel) {
136
137
  console.logLevel = settings.logLevel;
137
138
  }
139
+ // Register this class instance to service container
140
+ ServiceContainer.addService(ZumitoFramework, [], true, this);
141
+ ServiceContainer.addService(TranslationManager, [], true, this.translations);
138
142
  this.initialize()
139
143
  .then(() => {
140
144
  if (callback)
@@ -278,6 +282,7 @@ export class ZumitoFramework {
278
282
  intents: this.settings.discordClientOptions.intents,
279
283
  });
280
284
  this.client.login(this.settings.discordClientOptions.token);
285
+ ServiceContainer.addService(Client, [], true, this.client);
281
286
  await new Promise((resolve) => {
282
287
  this.client.on('ready', () => {
283
288
  // Bot emoji
@@ -12,7 +12,13 @@ export interface CommandParameters {
12
12
  message?: Message;
13
13
  interaction?: CommandInteraction;
14
14
  args: Map<string, any>;
15
+ /**
16
+ * @deprecated The client should be obtained from `ServiceContainer.get(Client);`
17
+ */
15
18
  client: Client;
19
+ /**
20
+ * @deprecated The frameworkInstance should be obtained from `ServiceContainer.get(ZumitoFramework);`
21
+ */
16
22
  framework: ZumitoFramework;
17
23
  guildSettings?: any;
18
24
  trans: (key: string, params?: any) => string;
package/dist/index.d.ts CHANGED
@@ -21,4 +21,5 @@ import { TranslationManager } from './services/TranslationManager.js';
21
21
  import { ZumitoFramework } from './ZumitoFramework.js';
22
22
  import * as discord from 'discord.js';
23
23
  import { EventParameters } from './definitions/parameters/EventParameters.js';
24
- export { ZumitoFramework, FrameworkSettings, Command, Module, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, SelectMenuParameters, CommandType, CommandArgDefinition, CommandChoiceDefinition, ButtonPressed, ButtonPressedParams, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, PresenceDataRule, RuledPresenceData, StatusManagerOptions, discord, EventParameters, };
24
+ import { ServiceContainer } from './services/ServiceContainer.js';
25
+ export { ZumitoFramework, FrameworkSettings, Command, Module, CommandParameters, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, SelectMenuParameters, CommandType, CommandArgDefinition, CommandChoiceDefinition, ButtonPressed, ButtonPressedParams, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, PresenceDataRule, RuledPresenceData, StatusManagerOptions, discord, EventParameters, ServiceContainer };
package/dist/index.js CHANGED
@@ -13,4 +13,7 @@ import { Translation } from './definitions/Translation.js';
13
13
  import { TranslationManager } from './services/TranslationManager.js';
14
14
  import { ZumitoFramework } from './ZumitoFramework.js';
15
15
  import * as discord from 'discord.js';
16
- export { ZumitoFramework, Command, Module, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, CommandType, ButtonPressed, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, discord, };
16
+ import { ServiceContainer } from './services/ServiceContainer.js';
17
+ ServiceContainer.addService(TextFormatter, []);
18
+ ServiceContainer.addService(EmojiFallback, []);
19
+ export { ZumitoFramework, Command, Module, CommandArguments, FrameworkEvent, Translation, TranslationManager, ApiResponse, CommandType, ButtonPressed, TextFormatter, EmojiFallback, DatabaseConfigLoader, DatabaseModel, discord, ServiceContainer };
@@ -0,0 +1,8 @@
1
+ declare class ServiceContainerManager {
2
+ private services;
3
+ addService(serviceClass: any, dependencies: string[], singleton?: boolean, instance?: any): void;
4
+ getService(serviceClass: any): any;
5
+ addInstance(serviceClass: any, instance: any): void;
6
+ }
7
+ export declare const ServiceContainer: ServiceContainerManager;
8
+ export {};
@@ -0,0 +1,29 @@
1
+ class ServiceContainerManager {
2
+ services = new Map();
3
+ addService(serviceClass, dependencies, singleton = false, instance) {
4
+ this.services.set(serviceClass.name, {
5
+ class: serviceClass,
6
+ dependencies,
7
+ singleton,
8
+ instance
9
+ });
10
+ }
11
+ getService(serviceClass) {
12
+ const service = this.services.get(serviceClass.name);
13
+ if (!service)
14
+ throw new Error(`Service ${serviceClass.name} not found`);
15
+ if (service.singleton && service.instance)
16
+ return service.instance;
17
+ const dependencies = service.dependencies.map(dependency => this.getService(dependency));
18
+ const instance = new service.class(...dependencies);
19
+ if (service.singleton)
20
+ service.instance = instance;
21
+ return instance;
22
+ }
23
+ addInstance(serviceClass, instance) {
24
+ if (!this.services.has(serviceClass.name))
25
+ return;
26
+ this.services.get(serviceClass.name).instance = instance;
27
+ }
28
+ }
29
+ export const ServiceContainer = new ServiceContainerManager();
@@ -17,6 +17,7 @@ export declare class StatusManager {
17
17
  * @private
18
18
  */
19
19
  private delegateEvents;
20
+ initialize(): void;
20
21
  /**
21
22
  * Sets the status of the bot.
22
23
  * If no presence data is provided, the status will be set to next in the queue.
@@ -18,12 +18,22 @@ export class StatusManager {
18
18
  * @private
19
19
  */
20
20
  delegateEvents() {
21
- this.framework.client.on("ready", () => {
22
- this.setStatus();
21
+ if (this.framework.client.isReady()) {
22
+ this.initialize();
23
+ }
24
+ else {
25
+ this.framework.client.on("ready", () => {
26
+ this.initialize();
27
+ });
28
+ }
29
+ }
30
+ initialize() {
31
+ this.setStatus();
32
+ if (this.options.updateInterval) {
23
33
  setInterval(() => {
24
34
  this.setStatus();
25
35
  }, this.options.updateInterval);
26
- });
36
+ }
27
37
  }
28
38
  /**
29
39
  * Sets the status of the bot.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",