zumito-framework 1.1.81 → 1.1.83
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/dist/ZumitoFramework.d.ts +13 -14
- package/dist/ZumitoFramework.js +18 -57
- package/dist/{types → definitions}/Module.d.ts +4 -7
- package/dist/definitions/Module.js +129 -0
- package/dist/{types → definitions}/Translation.d.ts +0 -1
- package/dist/{types → definitions}/Translation.js +0 -1
- package/dist/{types → definitions/commands}/Command.d.ts +1 -1
- package/dist/{types → definitions/commands}/CommandParameters.d.ts +1 -1
- package/dist/{types → definitions/parameters}/EventParameters.d.ts +1 -1
- package/dist/{types/Commands → definitions/parameters}/SelectMenu.d.ts +1 -1
- package/dist/{types → definitions/parameters}/SelectMenuParameters.d.ts +1 -1
- package/dist/index.d.ts +21 -21
- package/dist/index.js +13 -13
- package/dist/{baseModule → modules/core/baseModule}/events/discord/InteractionCreate.d.ts +3 -3
- package/dist/{baseModule → modules/core/baseModule}/events/discord/InteractionCreate.js +3 -3
- package/dist/{baseModule → modules/core/baseModule}/events/discord/MessageCreate.d.ts +2 -2
- package/dist/{baseModule → modules/core/baseModule}/events/discord/MessageCreate.js +6 -6
- package/dist/{baseModule → modules/core/baseModule}/models/Guild.d.ts +1 -1
- package/dist/{baseModule → modules/core/baseModule}/models/Guild.js +1 -1
- package/dist/services/CommandManager.d.ts +40 -0
- package/dist/services/CommandManager.js +160 -0
- package/dist/services/ModuleManager.d.ts +17 -0
- package/dist/services/ModuleManager.js +78 -0
- package/dist/{managers → services}/StatusManager.d.ts +1 -1
- package/dist/services/TranslationManager.d.ts +34 -0
- package/dist/services/TranslationManager.js +123 -0
- package/package.json +4 -4
- package/dist/TranslationManager.d.ts +0 -15
- package/dist/TranslationManager.js +0 -41
- package/dist/baseModule/index.d.ts +0 -7
- package/dist/baseModule/index.js +0 -20
- package/dist/types/Module.js +0 -210
- /package/dist/{types → definitions}/DatabaseModel.d.ts +0 -0
- /package/dist/{types → definitions}/DatabaseModel.js +0 -0
- /package/dist/{types → definitions}/FrameworkEvent.d.ts +0 -0
- /package/dist/{types → definitions}/FrameworkEvent.js +0 -0
- /package/dist/{types → definitions}/FrameworkSettings.d.ts +0 -0
- /package/dist/{types → definitions}/FrameworkSettings.js +0 -0
- /package/dist/{types → definitions}/StatusManagerOptions.d.ts +0 -0
- /package/dist/{types → definitions}/StatusManagerOptions.js +0 -0
- /package/dist/definitions/{ApiResponse.d.ts → api/ApiResponse.d.ts} +0 -0
- /package/dist/definitions/{ApiResponse.js → api/ApiResponse.js} +0 -0
- /package/dist/{types → definitions/commands}/Command.js +0 -0
- /package/dist/{types → definitions/commands}/CommandArgDefinition.d.ts +0 -0
- /package/dist/{types → definitions/commands}/CommandArgDefinition.js +0 -0
- /package/dist/{types → definitions/commands}/CommandArguments.d.ts +0 -0
- /package/dist/{types → definitions/commands}/CommandArguments.js +0 -0
- /package/dist/{types → definitions/commands}/CommandChoiceDefinition.d.ts +0 -0
- /package/dist/{types → definitions/commands}/CommandChoiceDefinition.js +0 -0
- /package/dist/{types → definitions/commands}/CommandParameters.js +0 -0
- /package/dist/{types → definitions/commands}/CommandType.d.ts +0 -0
- /package/dist/{types → definitions/commands}/CommandType.js +0 -0
- /package/dist/{types/Commands → definitions/parameters}/ButtonPressed.d.ts +0 -0
- /package/dist/{types/Commands → definitions/parameters}/ButtonPressed.js +0 -0
- /package/dist/{types/Commands → definitions/parameters}/ButtonPressedParams.d.ts +0 -0
- /package/dist/{types/Commands → definitions/parameters}/ButtonPressedParams.js +0 -0
- /package/dist/{types → definitions/parameters}/EventParameters.js +0 -0
- /package/dist/{types/Commands → definitions/parameters}/SelectMenu.js +0 -0
- /package/dist/{types → definitions/parameters}/SelectMenuParameters.js +0 -0
- /package/dist/{utils → services}/DatabaseConfigLoader.d.ts +0 -0
- /package/dist/{utils → services}/DatabaseConfigLoader.js +0 -0
- /package/dist/{utils → services}/EmojiFallback.d.ts +0 -0
- /package/dist/{utils → services}/EmojiFallback.js +0 -0
- /package/dist/{managers → services}/EmojiManager.d.ts +0 -0
- /package/dist/{managers → services}/EmojiManager.js +0 -0
- /package/dist/{managers → services}/EventManager.d.ts +0 -0
- /package/dist/{managers → services}/EventManager.js +0 -0
- /package/dist/{utils → services}/InteractionIdGenerator.d.ts +0 -0
- /package/dist/{utils → services}/InteractionIdGenerator.js +0 -0
- /package/dist/{managers → services}/StatusManager.js +0 -0
- /package/dist/{utils → services}/TextFormatter.d.ts +0 -0
- /package/dist/{utils → services}/TextFormatter.js +0 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import * as chokidar from 'chokidar';
|
|
3
|
+
import path from "path";
|
|
4
|
+
import boxen from "boxen";
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import { REST, Routes, SlashCommandBuilder } from "discord.js";
|
|
7
|
+
import { CommandType } from "../definitions/commands/CommandType.js";
|
|
8
|
+
export class CommandManager {
|
|
9
|
+
commands;
|
|
10
|
+
framework;
|
|
11
|
+
constructor(framework) {
|
|
12
|
+
this.commands = new Map;
|
|
13
|
+
this.framework = framework;
|
|
14
|
+
}
|
|
15
|
+
set(name, command) {
|
|
16
|
+
this.commands.set(name, command);
|
|
17
|
+
}
|
|
18
|
+
get(name) {
|
|
19
|
+
return this.commands.get(name);
|
|
20
|
+
}
|
|
21
|
+
getAll() {
|
|
22
|
+
return this.commands;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated
|
|
26
|
+
*/
|
|
27
|
+
get size() {
|
|
28
|
+
return this.commands.size;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Load command from file
|
|
32
|
+
* @async
|
|
33
|
+
* @public
|
|
34
|
+
* @param filePath - Absolute path to command file
|
|
35
|
+
* @returns {Promise<Command>}
|
|
36
|
+
*/
|
|
37
|
+
async loadCommandFile(filePath) {
|
|
38
|
+
// Validate file has .ts or .js extension
|
|
39
|
+
if (!filePath.endsWith('.js') && !filePath.endsWith('.ts')) {
|
|
40
|
+
throw new Error("File must be a .ts or .js");
|
|
41
|
+
}
|
|
42
|
+
// import file
|
|
43
|
+
let command = await import('file://' + filePath + '?update=' + Date.now().toString()).catch(e => {
|
|
44
|
+
console.error('[🆕🔴 ] Error loading command ' + chalk.blue(filePath.toString().replace(/^.*[\\\/]/, '').split('.').slice(0, -1).join('.')));
|
|
45
|
+
console.log(e + '\n' + e.name + '\n' + e.stack);
|
|
46
|
+
});
|
|
47
|
+
command = Object.values(command)[0];
|
|
48
|
+
command = new command();
|
|
49
|
+
this.framework.commands.set(command.constructor.name.toLowerCase(), command);
|
|
50
|
+
console.debug('[🆕🟢 ] Command ' + chalk.blue(filePath.toString().replace(/^.*[\\\/]/, '').split('.').slice(0, -1).join('.')) + ' loaded');
|
|
51
|
+
return command;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Load all command files from a folder
|
|
55
|
+
* @async
|
|
56
|
+
* @public
|
|
57
|
+
* @param folderPath - Absolute path to commands folder
|
|
58
|
+
* @returns {Promise<Map<string, Command>>}
|
|
59
|
+
*/
|
|
60
|
+
async loadCommandsFolder(folderPath) {
|
|
61
|
+
const files = fs.readdirSync(folderPath);
|
|
62
|
+
for (const file of files) {
|
|
63
|
+
if (file.endsWith('d.ts'))
|
|
64
|
+
continue;
|
|
65
|
+
if (file.endsWith('.js') || file.endsWith('.ts')) {
|
|
66
|
+
const command = await this.loadCommandFile(path.join(folderPath, file));
|
|
67
|
+
this.commands.set(command.constructor.name.toLowerCase(), command);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return this.commands;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Watch command files on a folder.
|
|
74
|
+
* It loads command when new file is created, update command when file is modified and deletes command when file is deleted.
|
|
75
|
+
* @async
|
|
76
|
+
* @public
|
|
77
|
+
* @param folderPath - Absolute path to commands folder
|
|
78
|
+
* @returns {Promise<Map<string, Command>>}
|
|
79
|
+
*/
|
|
80
|
+
watchCommandsFolder(folderPath) {
|
|
81
|
+
chokidar
|
|
82
|
+
.watch(path.resolve(folderPath), {
|
|
83
|
+
ignored: /^\./,
|
|
84
|
+
persistent: true,
|
|
85
|
+
ignoreInitial: true,
|
|
86
|
+
})
|
|
87
|
+
.on('add', (filePath) => {
|
|
88
|
+
this.loadCommandFile(filePath);
|
|
89
|
+
})
|
|
90
|
+
.on('change', (filePath) => {
|
|
91
|
+
this.loadCommandFile(filePath);
|
|
92
|
+
})
|
|
93
|
+
.on('error', (error) => {
|
|
94
|
+
console.error('[🔄🔴 ] Error reloading command');
|
|
95
|
+
console.log(boxen(error + '\n' + error.stack, { padding: 1 }));
|
|
96
|
+
});
|
|
97
|
+
// TODO: Handle file removal
|
|
98
|
+
//.on('unlink', function(path) {console.log('File', path, 'has been removed');})
|
|
99
|
+
}
|
|
100
|
+
async refreshSlashCommands() {
|
|
101
|
+
const rest = new REST({ version: '10' }).setToken(this.framework.settings.discordClientOptions.token);
|
|
102
|
+
const commands = Array.from(this.commands.values())
|
|
103
|
+
.filter((command) => command.type == CommandType.slash ||
|
|
104
|
+
command.type == CommandType.separated ||
|
|
105
|
+
command.type == CommandType.any)
|
|
106
|
+
.map((command) => {
|
|
107
|
+
const slashCommand = new SlashCommandBuilder()
|
|
108
|
+
.setName(command.name)
|
|
109
|
+
.setDescription(this.framework.translations.get('command.' + command.name + '.description', 'en'));
|
|
110
|
+
if (command.args) {
|
|
111
|
+
command.args.forEach((arg) => {
|
|
112
|
+
let method;
|
|
113
|
+
switch (arg.type) {
|
|
114
|
+
case 'string':
|
|
115
|
+
method = 'addStringOption';
|
|
116
|
+
break;
|
|
117
|
+
case 'user':
|
|
118
|
+
case 'member':
|
|
119
|
+
method = 'addUserOption';
|
|
120
|
+
break;
|
|
121
|
+
case 'channel':
|
|
122
|
+
method = 'addChannelOption';
|
|
123
|
+
break;
|
|
124
|
+
case 'role':
|
|
125
|
+
method = 'addRoleOption';
|
|
126
|
+
break;
|
|
127
|
+
default:
|
|
128
|
+
throw new Error('Invalid argument type ' + arg.type);
|
|
129
|
+
}
|
|
130
|
+
slashCommand[method]((option) => {
|
|
131
|
+
option.setName(arg.name);
|
|
132
|
+
option.setDescription(this.framework.translations.get('command.' +
|
|
133
|
+
command.name +
|
|
134
|
+
'.args.' +
|
|
135
|
+
arg.name +
|
|
136
|
+
'.description', 'en'));
|
|
137
|
+
option.setRequired(!arg.optional);
|
|
138
|
+
if (arg.choices) {
|
|
139
|
+
// if arg.choices is function, call it
|
|
140
|
+
if (typeof arg.choices == 'function') {
|
|
141
|
+
arg.choices =
|
|
142
|
+
arg.choices();
|
|
143
|
+
}
|
|
144
|
+
arg.choices.forEach((choice) => {
|
|
145
|
+
option.addChoices({
|
|
146
|
+
name: choice.name,
|
|
147
|
+
value: choice.value,
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return option;
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return slashCommand.toJSON();
|
|
156
|
+
});
|
|
157
|
+
const data = await rest.put(Routes.applicationCommands(this.framework.settings.discordClientOptions.clientId), { body: commands });
|
|
158
|
+
console.debug(`Successfully reloaded ${data.length} of ${commands.length} application (/) commands.`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ZumitoFramework } from "../ZumitoFramework.js";
|
|
2
|
+
import { Module } from "../definitions/Module.js";
|
|
3
|
+
export declare class ModuleManager {
|
|
4
|
+
protected modules: Map<string, Module>;
|
|
5
|
+
protected framework: ZumitoFramework;
|
|
6
|
+
constructor(framework: ZumitoFramework);
|
|
7
|
+
set(name: string, module: Module): void;
|
|
8
|
+
get(name: string): Module;
|
|
9
|
+
getAll(): Map<string, Module>;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated
|
|
12
|
+
*/
|
|
13
|
+
get size(): number;
|
|
14
|
+
loadModuleFile(folderPath: string): Promise<unknown>;
|
|
15
|
+
registerModule(module: InstanceType<typeof Module>): void;
|
|
16
|
+
instanceModule(module: any, rootPath: string, name?: string): Promise<Module>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Module } from "../definitions/Module.js";
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
export class ModuleManager {
|
|
5
|
+
modules;
|
|
6
|
+
framework;
|
|
7
|
+
constructor(framework) {
|
|
8
|
+
this.modules = new Map();
|
|
9
|
+
this.framework = framework;
|
|
10
|
+
}
|
|
11
|
+
set(name, module) {
|
|
12
|
+
this.modules.set(name, module);
|
|
13
|
+
}
|
|
14
|
+
get(name) {
|
|
15
|
+
return this.modules.get(name);
|
|
16
|
+
}
|
|
17
|
+
getAll() {
|
|
18
|
+
return this.modules;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated
|
|
22
|
+
*/
|
|
23
|
+
get size() {
|
|
24
|
+
return this.modules.size;
|
|
25
|
+
}
|
|
26
|
+
async loadModuleFile(folderPath) {
|
|
27
|
+
let file;
|
|
28
|
+
if (fs.existsSync(path.join(folderPath, 'index.js'))) {
|
|
29
|
+
file = path.join(folderPath, 'index.js');
|
|
30
|
+
}
|
|
31
|
+
else if (fs.existsSync(path.join(folderPath, 'index.ts'))) {
|
|
32
|
+
file = path.join(folderPath, 'index.ts');
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return Module;
|
|
36
|
+
}
|
|
37
|
+
const module = await import('file://' + file);
|
|
38
|
+
return Object.values(module)[0];
|
|
39
|
+
}
|
|
40
|
+
registerModule(module) {
|
|
41
|
+
// Register module commands
|
|
42
|
+
if (module.getCommands()) {
|
|
43
|
+
module.getCommands().forEach((command) => {
|
|
44
|
+
this.framework.commands.set(command.name, command);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
// Register module events
|
|
48
|
+
this.framework.events = new Map([...this.framework.events, ...module.getEvents()]);
|
|
49
|
+
// Register models
|
|
50
|
+
module.getModels().forEach((model) => {
|
|
51
|
+
this.framework.models.push(model);
|
|
52
|
+
});
|
|
53
|
+
/*
|
|
54
|
+
|
|
55
|
+
// Register module routes
|
|
56
|
+
this.routes = new Map([...this.routes, ...moduleInstance.getRoutes()]);
|
|
57
|
+
|
|
58
|
+
*/
|
|
59
|
+
}
|
|
60
|
+
async instanceModule(module, rootPath, name) {
|
|
61
|
+
let moduleInstance;
|
|
62
|
+
if (module.constructor) {
|
|
63
|
+
try {
|
|
64
|
+
moduleInstance = new module(rootPath, this.framework);
|
|
65
|
+
await moduleInstance.initialize();
|
|
66
|
+
this.modules.set(name || moduleInstance.constructor.name, moduleInstance);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
console.error(`[📦🔴] Error loading module ${name || moduleInstance?.constructor?.name}: ${err.message}`);
|
|
70
|
+
console.error(err.stack);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
//moduleInstance = new Module();
|
|
75
|
+
}
|
|
76
|
+
return moduleInstance;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PresenceData } from "discord.js";
|
|
2
|
-
import { StatusManagerOptions } from "../
|
|
2
|
+
import { StatusManagerOptions } from "../definitions/StatusManagerOptions";
|
|
3
3
|
import { ZumitoFramework } from "../ZumitoFramework";
|
|
4
4
|
export declare class StatusManager {
|
|
5
5
|
/**
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Translation } from '../definitions/Translation.js';
|
|
2
|
+
export declare class TranslationManager {
|
|
3
|
+
private translations;
|
|
4
|
+
private defaultLanguage;
|
|
5
|
+
private languages;
|
|
6
|
+
get(key: string, language?: string, params?: unknown): string;
|
|
7
|
+
set(key: string, language: string, text: string): void;
|
|
8
|
+
has(key: string): boolean;
|
|
9
|
+
getAll(): Map<string, Translation>;
|
|
10
|
+
setAll(translations: Map<string, Translation>): void;
|
|
11
|
+
getDefaultLanguage(): string;
|
|
12
|
+
setDefaultLanguage(language: string): void;
|
|
13
|
+
getLanguages(): string[];
|
|
14
|
+
importTranslationsJson(path: string, lang: string, json: object | string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Returns content of translation json file
|
|
17
|
+
* @async
|
|
18
|
+
* @public
|
|
19
|
+
* @param filePath - Absolute path to translations file
|
|
20
|
+
* @returns {Promise<any>}
|
|
21
|
+
*/
|
|
22
|
+
loadTranslationFile(filePath: string): Promise<object>;
|
|
23
|
+
/**
|
|
24
|
+
* load translation files from folder and subfolders
|
|
25
|
+
* @async
|
|
26
|
+
* @public
|
|
27
|
+
* @param folderPath - Absolute path to translations files folder
|
|
28
|
+
* @param [baseKey=''] - (Optional) the translation key to start from. All translations loaded will be children translations of that key
|
|
29
|
+
* @param watch - Watch folder for file modifications
|
|
30
|
+
* @returns {Promise<void>}
|
|
31
|
+
*/
|
|
32
|
+
registerTranslationsFromFolder(folderPath: string, baseKey?: string, watch?: boolean): Promise<void>;
|
|
33
|
+
watchTranslationFolder(folderPath: string, baseKey: string): void;
|
|
34
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Translation } from '../definitions/Translation.js';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import * as chokidar from 'chokidar';
|
|
5
|
+
import boxen from 'boxen';
|
|
6
|
+
import chalk from "chalk";
|
|
7
|
+
export class TranslationManager {
|
|
8
|
+
translations = new Map();
|
|
9
|
+
defaultLanguage = 'en';
|
|
10
|
+
languages = [];
|
|
11
|
+
get(key, language, params) {
|
|
12
|
+
if (this.translations.has(key)) {
|
|
13
|
+
return this.translations.get(key).get(language, params);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return key;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
set(key, language, text) {
|
|
20
|
+
if (!this.translations.has(key)) {
|
|
21
|
+
this.translations.set(key, new Translation());
|
|
22
|
+
}
|
|
23
|
+
this.translations.get(key).set(language, text);
|
|
24
|
+
if (!this.languages.includes(language))
|
|
25
|
+
this.languages.push(language);
|
|
26
|
+
}
|
|
27
|
+
has(key) {
|
|
28
|
+
return this.translations.has(key);
|
|
29
|
+
}
|
|
30
|
+
getAll() {
|
|
31
|
+
return this.translations;
|
|
32
|
+
}
|
|
33
|
+
setAll(translations) {
|
|
34
|
+
this.translations = translations;
|
|
35
|
+
}
|
|
36
|
+
getDefaultLanguage() {
|
|
37
|
+
return this.defaultLanguage;
|
|
38
|
+
}
|
|
39
|
+
setDefaultLanguage(language) {
|
|
40
|
+
this.defaultLanguage = language;
|
|
41
|
+
}
|
|
42
|
+
getLanguages() {
|
|
43
|
+
return this.languages;
|
|
44
|
+
}
|
|
45
|
+
importTranslationsJson(path, lang, json) {
|
|
46
|
+
if (typeof json === 'object') {
|
|
47
|
+
for (const key in json) {
|
|
48
|
+
this.importTranslationsJson(path + key + '.', lang, json[key]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.set(path.slice(0, -1), lang, json);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns content of translation json file
|
|
57
|
+
* @async
|
|
58
|
+
* @public
|
|
59
|
+
* @param filePath - Absolute path to translations file
|
|
60
|
+
* @returns {Promise<any>}
|
|
61
|
+
*/
|
|
62
|
+
async loadTranslationFile(filePath) {
|
|
63
|
+
const json = fs.readFileSync(filePath);
|
|
64
|
+
return JSON.parse(json);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* load translation files from folder and subfolders
|
|
68
|
+
* @async
|
|
69
|
+
* @public
|
|
70
|
+
* @param folderPath - Absolute path to translations files folder
|
|
71
|
+
* @param [baseKey=''] - (Optional) the translation key to start from. All translations loaded will be children translations of that key
|
|
72
|
+
* @param watch - Watch folder for file modifications
|
|
73
|
+
* @returns {Promise<void>}
|
|
74
|
+
*/
|
|
75
|
+
async registerTranslationsFromFolder(folderPath, baseKey = '', watch = false) {
|
|
76
|
+
if (!fs.existsSync(path.join(folderPath)))
|
|
77
|
+
return;
|
|
78
|
+
const files = fs.readdirSync(folderPath);
|
|
79
|
+
for (const file of files) {
|
|
80
|
+
if (file.endsWith('.json')) {
|
|
81
|
+
const json = await this.loadTranslationFile(path.join(folderPath, file));
|
|
82
|
+
const lang = file.slice(0, -5);
|
|
83
|
+
this.importTranslationsJson(baseKey, lang, json);
|
|
84
|
+
console.debug('[🆕🟢 ] Translations file ' + chalk.blue(path.join(folderPath, file)) + ' loaded');
|
|
85
|
+
}
|
|
86
|
+
else if (fs
|
|
87
|
+
.lstatSync(path.join(folderPath, file))
|
|
88
|
+
.isDirectory()) {
|
|
89
|
+
const key = baseKey + path.basename(path.join(folderPath, file)) + '.';
|
|
90
|
+
await this.registerTranslationsFromFolder(path.join(folderPath, file), key, watch);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (watch) {
|
|
94
|
+
this.watchTranslationFolder(folderPath, baseKey);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
watchTranslationFolder(folderPath, baseKey) {
|
|
98
|
+
chokidar
|
|
99
|
+
.watch(path.resolve(folderPath), {
|
|
100
|
+
ignored: /^\./,
|
|
101
|
+
persistent: true,
|
|
102
|
+
ignoreInitial: true,
|
|
103
|
+
})
|
|
104
|
+
.on('add', async (filePath) => {
|
|
105
|
+
const json = await this.loadTranslationFile(filePath);
|
|
106
|
+
const lang = filePath.replace(/^.*[\\/]/, '').slice(0, -5);
|
|
107
|
+
this.importTranslationsJson(baseKey, lang, json);
|
|
108
|
+
console.debug('[🆕🟢 ] Translations file ' + chalk.blue(filePath) + ' loaded');
|
|
109
|
+
})
|
|
110
|
+
.on('change', async (filePath) => {
|
|
111
|
+
const json = await this.loadTranslationFile(filePath);
|
|
112
|
+
const lang = filePath.replace(/^.*[\\/]/, '').slice(0, -5);
|
|
113
|
+
this.importTranslationsJson(baseKey, lang, json);
|
|
114
|
+
console.debug('[🆕🟢 ] Translations file ' + chalk.blue(filePath) + ' loaded');
|
|
115
|
+
})
|
|
116
|
+
.on('error', (error) => {
|
|
117
|
+
console.error('[🔄🔴 ] Error reloading translation file');
|
|
118
|
+
console.log(boxen(error + '\n' + error.stack, { padding: 1 }));
|
|
119
|
+
});
|
|
120
|
+
// TODO: Handle file removal
|
|
121
|
+
//.on('unlink', function(path) {console.log('File', path, 'has been removed');})
|
|
122
|
+
}
|
|
123
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zumito-framework",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.83",
|
|
4
4
|
"description": "Discord.js bot framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"chokidar": "^3.5.3",
|
|
30
30
|
"cookie-parser": "^1.4.6",
|
|
31
31
|
"cors": "^2.8.5",
|
|
32
|
-
"discord.js": "^14.
|
|
32
|
+
"discord.js": "^14.9.0",
|
|
33
33
|
"error-stack-parser": "^2.1.4",
|
|
34
34
|
"express": "^4.18.1",
|
|
35
35
|
"leven": "^4.0.0",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"eslint-plugin-check-file": "^2.2.0",
|
|
47
47
|
"eslint-plugin-prettier": "^4.2.1",
|
|
48
48
|
"prettier": "^2.8.3",
|
|
49
|
-
"typedoc": "^0.
|
|
50
|
-
"typescript": "^
|
|
49
|
+
"typedoc": "^0.23.14",
|
|
50
|
+
"typescript": "^4.8.3"
|
|
51
51
|
},
|
|
52
52
|
"type": "module",
|
|
53
53
|
"exports": {
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Translation } from './types/Translation.js';
|
|
2
|
-
export declare class TranslationManager {
|
|
3
|
-
private translations;
|
|
4
|
-
private defaultLanguage;
|
|
5
|
-
private languages;
|
|
6
|
-
constructor();
|
|
7
|
-
get(key: string, language?: string, params?: any): string;
|
|
8
|
-
set(key: string, language: string, text: string): void;
|
|
9
|
-
has(key: string): boolean;
|
|
10
|
-
getAll(): Map<string, Translation>;
|
|
11
|
-
setAll(translations: Map<string, Translation>): void;
|
|
12
|
-
getDefaultLanguage(): string;
|
|
13
|
-
setDefaultLanguage(language: string): void;
|
|
14
|
-
getLanguages(): string[];
|
|
15
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Translation } from './types/Translation.js';
|
|
2
|
-
export class TranslationManager {
|
|
3
|
-
translations = new Map();
|
|
4
|
-
defaultLanguage = 'en';
|
|
5
|
-
languages = [];
|
|
6
|
-
constructor() { }
|
|
7
|
-
get(key, language, params) {
|
|
8
|
-
if (this.translations.has(key)) {
|
|
9
|
-
return this.translations.get(key).get(language, params);
|
|
10
|
-
}
|
|
11
|
-
else {
|
|
12
|
-
return key;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
set(key, language, text) {
|
|
16
|
-
if (!this.translations.has(key)) {
|
|
17
|
-
this.translations.set(key, new Translation());
|
|
18
|
-
}
|
|
19
|
-
this.translations.get(key).set(language, text);
|
|
20
|
-
if (!this.languages.includes(language))
|
|
21
|
-
this.languages.push(language);
|
|
22
|
-
}
|
|
23
|
-
has(key) {
|
|
24
|
-
return this.translations.has(key);
|
|
25
|
-
}
|
|
26
|
-
getAll() {
|
|
27
|
-
return this.translations;
|
|
28
|
-
}
|
|
29
|
-
setAll(translations) {
|
|
30
|
-
this.translations = translations;
|
|
31
|
-
}
|
|
32
|
-
getDefaultLanguage() {
|
|
33
|
-
return this.defaultLanguage;
|
|
34
|
-
}
|
|
35
|
-
setDefaultLanguage(language) {
|
|
36
|
-
this.defaultLanguage = language;
|
|
37
|
-
}
|
|
38
|
-
getLanguages() {
|
|
39
|
-
return this.languages;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Module } from '../types/Module.js';
|
|
2
|
-
import { ZumitoFramework } from '../ZumitoFramework.js';
|
|
3
|
-
export declare class baseModule extends Module {
|
|
4
|
-
constructor(modulePath: string, framework: ZumitoFramework);
|
|
5
|
-
registerEvents(): Promise<any>;
|
|
6
|
-
registerModels(): Promise<void>;
|
|
7
|
-
}
|
package/dist/baseModule/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/* eslint-disable check-file/filename-naming-convention */
|
|
2
|
-
import { Module } from '../types/Module.js';
|
|
3
|
-
import { InteractionCreate } from './events/discord/InteractionCreate.js';
|
|
4
|
-
import { MessageCreate } from './events/discord/MessageCreate.js';
|
|
5
|
-
import { Guild } from './models/Guild.js';
|
|
6
|
-
export class baseModule extends Module {
|
|
7
|
-
constructor(modulePath, framework) {
|
|
8
|
-
super(modulePath, framework);
|
|
9
|
-
}
|
|
10
|
-
async registerEvents() {
|
|
11
|
-
this.events.set('interactionCreate', new InteractionCreate());
|
|
12
|
-
this.events.set('messageCreate', new MessageCreate());
|
|
13
|
-
this.events.forEach((event) => {
|
|
14
|
-
this.registerEvent(event, 'discord');
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
async registerModels() {
|
|
18
|
-
this.models.push(new Guild(this.framework));
|
|
19
|
-
}
|
|
20
|
-
}
|