zumito-framework 1.1.27 → 1.1.28

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.
@@ -0,0 +1,15 @@
1
+ import { Translation } from "./types/Translation";
2
+ export declare class TranslationManager {
3
+ private translations;
4
+ private defaultLanguage;
5
+ constructor();
6
+ get(key: string, language?: string, params?: {
7
+ [key: string]: string;
8
+ }[]): String;
9
+ set(key: string, language: string, text: string): void;
10
+ has(key: string): boolean;
11
+ getAll(): Map<string, Translation>;
12
+ setAll(translations: Map<string, Translation>): void;
13
+ getDefaultLanguage(): string;
14
+ setDefaultLanguage(language: string): void;
15
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TranslationManager = void 0;
4
+ const Translation_1 = require("./types/Translation");
5
+ class TranslationManager {
6
+ translations = new Map();
7
+ defaultLanguage = 'en';
8
+ constructor() { }
9
+ get(key, language, params) {
10
+ return this.translations.get(key).get(language, params);
11
+ }
12
+ set(key, language, text) {
13
+ if (!this.translations.has(key)) {
14
+ this.translations.set(key, new Translation_1.Translation());
15
+ }
16
+ this.translations.get(key).set(language, text);
17
+ }
18
+ has(key) {
19
+ return this.translations.has(key);
20
+ }
21
+ getAll() {
22
+ return this.translations;
23
+ }
24
+ setAll(translations) {
25
+ this.translations = translations;
26
+ }
27
+ getDefaultLanguage() {
28
+ return this.defaultLanguage;
29
+ }
30
+ setDefaultLanguage(language) {
31
+ this.defaultLanguage = language;
32
+ }
33
+ }
34
+ exports.TranslationManager = TranslationManager;
@@ -3,6 +3,7 @@ import { Command } from "./types/Commands";
3
3
  import { FrameworkSettings } from "./types/FrameworkSettings";
4
4
  import { Module } from "./types/Module";
5
5
  import { FrameworkEvent } from "./types/FrameworkEvent";
6
+ import { TranslationManager } from "./TranslationManager";
6
7
  /**
7
8
  * @class ZumitoFramework
8
9
  * @classdesc The main class of the framework.
@@ -18,7 +19,7 @@ export declare class ZumitoFramework {
18
19
  modules: Map<string, Module>;
19
20
  commands: Map<string, Command>;
20
21
  events: Map<string, FrameworkEvent>;
21
- translations: Map<string, string>;
22
+ translations: TranslationManager;
22
23
  routes: any;
23
24
  models: any;
24
25
  database: any;
@@ -4,6 +4,7 @@ exports.ZumitoFramework = void 0;
4
4
  const Module_1 = require("./types/Module");
5
5
  const ApiResponse_1 = require("./definitions/ApiResponse");
6
6
  const baseModule_1 = require("./baseModule");
7
+ const TranslationManager_1 = require("./TranslationManager");
7
8
  const express = require("express");
8
9
  const fs = require('fs');
9
10
  const path = require('path');
@@ -30,7 +31,7 @@ class ZumitoFramework {
30
31
  modules;
31
32
  commands;
32
33
  events;
33
- translations;
34
+ translations = new TranslationManager_1.TranslationManager();
34
35
  routes;
35
36
  models;
36
37
  database;
@@ -54,7 +55,7 @@ class ZumitoFramework {
54
55
  this.modules = new Map();
55
56
  this.commands = new Map();
56
57
  this.events = new Map();
57
- this.translations = new Map();
58
+ this.translations = new TranslationManager_1.TranslationManager();
58
59
  this.initialize().then(() => {
59
60
  if (callback)
60
61
  callback();
@@ -123,6 +124,10 @@ class ZumitoFramework {
123
124
  fs.readdirSync(modulesFolder).forEach(file => {
124
125
  this.registerModule(modulesFolder, file);
125
126
  });
127
+ this.models.forEach((modelDefiniton, modelName) => {
128
+ const schema = new mongoose.Schema(modelDefiniton);
129
+ this.models.set(modelName, mongoose.model(modelName, schema));
130
+ });
126
131
  }
127
132
  registerModule(modulesFolder, moduleName, module) {
128
133
  if (!module) {
@@ -146,16 +151,20 @@ class ZumitoFramework {
146
151
  this.commands = new Map([...this.commands, ...moduleInstance.getCommands()]);
147
152
  // Register module events
148
153
  this.events = new Map([...this.events, ...moduleInstance.getEvents()]);
149
- // Register translations
150
- this.translations = new Map([...this.translations, ...moduleInstance.getTranslations()]);
154
+ // Register models
155
+ moduleInstance.getModels().forEach((modelDefinition, modelName) => {
156
+ if (!this.models.has(modelName)) {
157
+ this.models.set(modelName, modelDefinition);
158
+ }
159
+ else {
160
+ this.models.set(modelName, MergeRecursive(this.models.get(modelName), modelDefinition));
161
+ }
162
+ });
151
163
  /*
152
164
 
153
165
  // Register module routes
154
166
  this.routes = new Map([...this.routes, ...moduleInstance.getRoutes()]);
155
167
 
156
- // Register module models
157
- this.models = new Map([...this.models, ...moduleInstance.getModels()]);
158
-
159
168
  */
160
169
  }
161
170
  initializeDiscordClient() {
@@ -194,3 +203,21 @@ class ZumitoFramework {
194
203
  }
195
204
  }
196
205
  exports.ZumitoFramework = ZumitoFramework;
206
+ function MergeRecursive(obj1, obj2) {
207
+ for (var p in obj2) {
208
+ try {
209
+ // Property in destination object set; update its value.
210
+ if (obj2[p].constructor == Object) {
211
+ obj1[p] = MergeRecursive(obj1[p], obj2[p]);
212
+ }
213
+ else {
214
+ obj1[p] = obj2[p];
215
+ }
216
+ }
217
+ catch (e) {
218
+ // Property in destination object not set; create it and set its value.
219
+ obj1[p] = obj2[p];
220
+ }
221
+ }
222
+ return obj1;
223
+ }
@@ -6,7 +6,6 @@ export declare abstract class Module {
6
6
  protected framework: ZumitoFramework;
7
7
  protected commands: Map<string, Command>;
8
8
  protected events: Map<string, FrameworkEvent>;
9
- protected translations: Map<string, string>;
10
9
  protected models: Map<string, any>;
11
10
  constructor(path: any, framework: any);
12
11
  registerCommands(): void;
@@ -16,8 +15,7 @@ export declare abstract class Module {
16
15
  parseEventArgs(args: any[]): any;
17
16
  getEvents(): Map<string, FrameworkEvent>;
18
17
  registerTranslations(): void;
19
- getTranslations(): Map<string, string>;
20
- parseTranslation(path: string, json: any): any;
18
+ parseTranslation(path: string, lang: string, json: any): any;
21
19
  registerModels(): void;
22
20
  getModels(): Map<string, any>;
23
21
  }
@@ -9,7 +9,6 @@ class Module {
9
9
  framework;
10
10
  commands = new Map();
11
11
  events = new Map();
12
- translations = new Map();
13
12
  models = new Map();
14
13
  constructor(path, framework) {
15
14
  this.path = path;
@@ -83,21 +82,19 @@ class Module {
83
82
  fs.readdirSync(path.join(this.path, 'translations')).forEach(file => {
84
83
  if (file.endsWith('.json')) {
85
84
  let json = require(`${this.path}/translations/${file}`);
86
- this.parseTranslation('', json);
85
+ let lang = file.slice(0, -5);
86
+ this.parseTranslation('', lang, json);
87
87
  }
88
88
  });
89
89
  }
90
- getTranslations() {
91
- return this.translations;
92
- }
93
- parseTranslation(path, json) {
90
+ parseTranslation(path, lang, json) {
94
91
  if (typeof json === 'object') {
95
92
  for (let key in json) {
96
- this.parseTranslation(path + key + '.', json[key]);
93
+ this.parseTranslation(path + key + '.', lang, json[key]);
97
94
  }
98
95
  }
99
96
  else {
100
- this.translations.set(path.slice(0, -1), json);
97
+ this.framework.translations.set(path, lang, json);
101
98
  }
102
99
  }
103
100
  registerModels() {
@@ -107,8 +104,7 @@ class Module {
107
104
  if (file.endsWith('.json')) {
108
105
  let modelName = file.slice(0, -5).charAt(0).toUpperCase() + file.slice(0, -5).slice(1);
109
106
  let modelDefiniton = require(`${this.path}/models/${file}`);
110
- const schema = new mongoose.Schema(modelDefiniton);
111
- this.models.set(modelDefiniton.name, mongoose.model(modelName, schema));
107
+ this.models.set(modelName, modelDefiniton);
112
108
  }
113
109
  });
114
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.1.27",
3
+ "version": "1.1.28",
4
4
  "description": "Discord.js bot framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",