@power-bots/powerbotlibrary 0.4.1 → 0.5.0
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/bot.js +2 -0
- package/dist/lang.d.ts +11 -0
- package/dist/lang.js +88 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +4 -1
- package/package.json +1 -1
package/dist/bot.js
CHANGED
|
@@ -20,6 +20,7 @@ const node_path_1 = __importDefault(require("node:path"));
|
|
|
20
20
|
const discord_js_1 = require("discord.js");
|
|
21
21
|
const log_1 = require("./log");
|
|
22
22
|
const db_1 = require("./db");
|
|
23
|
+
const lang_1 = require("./lang");
|
|
23
24
|
var db_2 = require("./db");
|
|
24
25
|
Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_2.knex; } });
|
|
25
26
|
var config_1 = require("./config");
|
|
@@ -63,6 +64,7 @@ class Bot {
|
|
|
63
64
|
this.dirname = dirname;
|
|
64
65
|
yield (0, db_1.setupDatabase)();
|
|
65
66
|
yield (0, db_1.updateDatabase)();
|
|
67
|
+
yield lang_1.Lang.setup();
|
|
66
68
|
});
|
|
67
69
|
}
|
|
68
70
|
run() {
|
package/dist/lang.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommandInteraction } from "discord.js";
|
|
2
|
+
export declare function reply(interaction: CommandInteraction, text: string, args?: Record<string, any>): Promise<void>;
|
|
3
|
+
export declare class Lang {
|
|
4
|
+
texts: any;
|
|
5
|
+
static en: Lang;
|
|
6
|
+
static embed: Lang;
|
|
7
|
+
constructor(lang: string);
|
|
8
|
+
static get folder(): string;
|
|
9
|
+
static setup(): Promise<void>;
|
|
10
|
+
get(text: string, args?: Record<string, any>): Promise<any>;
|
|
11
|
+
}
|
package/dist/lang.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Lang = void 0;
|
|
16
|
+
exports.reply = reply;
|
|
17
|
+
const path_1 = __importDefault(require("path"));
|
|
18
|
+
const fs_1 = __importDefault(require("fs"));
|
|
19
|
+
const main_1 = require("./main");
|
|
20
|
+
const discord_js_1 = require("discord.js");
|
|
21
|
+
function localize(obj, lang, args) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
if (typeof obj === "string") {
|
|
24
|
+
if (obj.startsWith("$"))
|
|
25
|
+
return lang.get(obj.replace("$", ""), args) || obj;
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(obj)) {
|
|
29
|
+
return obj.map((item) => __awaiter(this, void 0, void 0, function* () { return yield localize(item, lang, args); }));
|
|
30
|
+
}
|
|
31
|
+
if (typeof obj === "object" && obj !== null) {
|
|
32
|
+
const result = {};
|
|
33
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
34
|
+
if (key === "options")
|
|
35
|
+
break;
|
|
36
|
+
result[key] = yield localize(value, lang, args);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
return obj;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function reply(interaction, text, args) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
var _a;
|
|
46
|
+
const embed = yield Lang.embed.get(text);
|
|
47
|
+
let flags = [];
|
|
48
|
+
if ((_a = embed === null || embed === void 0 ? void 0 : embed.options) === null || _a === void 0 ? void 0 : _a.ephemeral)
|
|
49
|
+
flags.push(discord_js_1.MessageFlags.Ephemeral);
|
|
50
|
+
const newEmbed = yield localize(embed, Lang.en, args);
|
|
51
|
+
yield interaction.reply({ embeds: [newEmbed], flags: flags });
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
class Lang {
|
|
55
|
+
constructor(lang) {
|
|
56
|
+
const langFileLocation = path_1.default.join(Lang.folder, `${lang}.json`);
|
|
57
|
+
if (fs_1.default.existsSync(langFileLocation)) {
|
|
58
|
+
fs_1.default.readFile(langFileLocation, { encoding: "utf-8" }, (err, data) => {
|
|
59
|
+
this.texts = JSON.parse(data);
|
|
60
|
+
return;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
this.texts = null;
|
|
64
|
+
}
|
|
65
|
+
static get folder() {
|
|
66
|
+
return path_1.default.join(main_1.bot.dirname, "lang");
|
|
67
|
+
}
|
|
68
|
+
static setup() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
Lang.embed = new Lang("embed");
|
|
71
|
+
Lang.en = new Lang("en");
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
get(text, args) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
let newText = this.texts[text];
|
|
77
|
+
if (typeof newText === "string") {
|
|
78
|
+
if (args) {
|
|
79
|
+
for (const [name, value] of Object.entries(args)) {
|
|
80
|
+
newText = newText.replace(`{{${name}}}`, value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return newText;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.Lang = Lang;
|
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.bot = exports.ConfigTypes = exports.Config = exports.knex = void 0;
|
|
3
|
+
exports.bot = exports.reply = exports.Lang = exports.ConfigTypes = exports.Config = exports.knex = void 0;
|
|
4
4
|
const bot_1 = require("./bot");
|
|
5
5
|
var db_1 = require("./db");
|
|
6
6
|
Object.defineProperty(exports, "knex", { enumerable: true, get: function () { return db_1.knex; } });
|
|
7
7
|
var config_1 = require("./config");
|
|
8
8
|
Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return config_1.Config; } });
|
|
9
9
|
Object.defineProperty(exports, "ConfigTypes", { enumerable: true, get: function () { return config_1.ConfigTypes; } });
|
|
10
|
+
var lang_1 = require("./lang");
|
|
11
|
+
Object.defineProperty(exports, "Lang", { enumerable: true, get: function () { return lang_1.Lang; } });
|
|
12
|
+
Object.defineProperty(exports, "reply", { enumerable: true, get: function () { return lang_1.reply; } });
|
|
10
13
|
exports.bot = new bot_1.Bot();
|