zumito-framework 1.1.25 → 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,7 @@ 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>;
9
+ protected models: Map<string, any>;
10
10
  constructor(path: any, framework: any);
11
11
  registerCommands(): void;
12
12
  getCommands(): Map<string, Command>;
@@ -15,6 +15,7 @@ export declare abstract class Module {
15
15
  parseEventArgs(args: any[]): any;
16
16
  getEvents(): Map<string, FrameworkEvent>;
17
17
  registerTranslations(): void;
18
- getTranslations(): Map<string, string>;
19
- parseTranslation(path: string, json: any): any;
18
+ parseTranslation(path: string, lang: string, json: any): any;
19
+ registerModels(): void;
20
+ getModels(): Map<string, any>;
20
21
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Module = void 0;
4
+ const mongoose = require("mongoose");
4
5
  const fs = require('fs');
5
6
  const path = require('path');
6
7
  class Module {
@@ -8,7 +9,7 @@ class Module {
8
9
  framework;
9
10
  commands = new Map();
10
11
  events = new Map();
11
- translations = new Map();
12
+ models = new Map();
12
13
  constructor(path, framework) {
13
14
  this.path = path;
14
15
  this.framework = framework;
@@ -81,22 +82,34 @@ class Module {
81
82
  fs.readdirSync(path.join(this.path, 'translations')).forEach(file => {
82
83
  if (file.endsWith('.json')) {
83
84
  let json = require(`${this.path}/translations/${file}`);
84
- this.parseTranslation('', json);
85
+ let lang = file.slice(0, -5);
86
+ this.parseTranslation('', lang, json);
85
87
  }
86
88
  });
87
89
  }
88
- getTranslations() {
89
- return this.translations;
90
- }
91
- parseTranslation(path, json) {
90
+ parseTranslation(path, lang, json) {
92
91
  if (typeof json === 'object') {
93
92
  for (let key in json) {
94
- this.parseTranslation(path + key + '.', json[key]);
93
+ this.parseTranslation(path + key + '.', lang, json[key]);
95
94
  }
96
95
  }
97
96
  else {
98
- this.translations.set(path.slice(0, -1), json);
97
+ this.framework.translations.set(path, lang, json);
99
98
  }
100
99
  }
100
+ registerModels() {
101
+ if (!fs.existsSync(path.join(this.path, 'models')))
102
+ return;
103
+ fs.readdirSync(path.join(this.path, 'models')).forEach(file => {
104
+ if (file.endsWith('.json')) {
105
+ let modelName = file.slice(0, -5).charAt(0).toUpperCase() + file.slice(0, -5).slice(1);
106
+ let modelDefiniton = require(`${this.path}/models/${file}`);
107
+ this.models.set(modelName, modelDefiniton);
108
+ }
109
+ });
110
+ }
111
+ getModels() {
112
+ return this.models;
113
+ }
101
114
  }
102
115
  exports.Module = Module;
@@ -1,7 +1,9 @@
1
1
  export declare class Translation {
2
2
  text: Map<string, string>;
3
3
  constructor();
4
- get(language: string): String;
4
+ get(language: string, params?: {
5
+ [key: string]: string;
6
+ }[]): String;
5
7
  set(language: string, text: string): void;
6
8
  has(language: string): boolean;
7
9
  getAll(): Map<string, string>;
@@ -4,13 +4,20 @@ exports.Translation = void 0;
4
4
  class Translation {
5
5
  text = new Map();
6
6
  constructor() { }
7
- get(language) {
7
+ get(language, params) {
8
+ let text;
8
9
  if (this.has(language)) {
9
- return this.text.get(language);
10
+ text = this.text.get(language);
10
11
  }
11
12
  else {
12
- return this.text.get('en');
13
+ text = this.text.get('en');
13
14
  }
15
+ if (params) {
16
+ Object.keys(params).forEach(key => {
17
+ text = text.replace(`{${key}}`, params[key]);
18
+ });
19
+ }
20
+ return text;
14
21
  }
15
22
  set(language, text) {
16
23
  this.text.set(language, text);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zumito-framework",
3
- "version": "1.1.25",
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",
@@ -1,12 +1,10 @@
1
1
  import { Command, CommandParameters } from "zumito-framework";
2
2
 
3
- export class Test extends Command {
4
-
5
- name: 'test';
3
+ export class {{command.charAt(0).toUpperCase() + command.slice(1)}} extends Command {
6
4
 
7
5
  execute({ message, interaction, args, client, framework }: CommandParameters): void {
8
- message.channel.send({
9
- content: "Test command executed",
6
+ (message || interaction).reply({
7
+ content: "Message content",
10
8
  });
11
9
  }
12
10
  }