@open-discord-bots/framework 0.3.0 → 0.3.1
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/api/main.js +1 -1
- package/dist/api/modules/base.d.ts +27 -9
- package/dist/api/modules/base.js +78 -80
- package/dist/api/modules/builder.d.ts +0 -9
- package/dist/api/modules/checker.d.ts +26 -5
- package/dist/api/modules/checker.js +31 -31
- package/dist/api/modules/client.d.ts +66 -14
- package/dist/api/modules/client.js +146 -132
- package/dist/api/modules/component.d.ts +8 -2
- package/dist/api/modules/component.js +8 -6
- package/dist/api/modules/config.d.ts +0 -1
- package/dist/api/modules/config.js +9 -7
- package/dist/api/modules/console.d.ts +16 -4
- package/dist/api/modules/console.js +25 -25
- package/dist/api/modules/event.d.ts +4 -2
- package/dist/api/modules/event.js +8 -10
- package/dist/api/modules/fuse.d.ts +1 -1
- package/dist/api/modules/helpmenu.d.ts +2 -2
- package/dist/api/modules/helpmenu.js +4 -7
- package/dist/api/modules/language.d.ts +2 -1
- package/dist/api/modules/language.js +6 -9
- package/dist/api/modules/permission.d.ts +10 -1
- package/dist/api/modules/permission.js +17 -20
- package/dist/api/modules/plugin.d.ts +2 -1
- package/dist/api/modules/plugin.js +2 -2
- package/dist/api/modules/post.d.ts +12 -4
- package/dist/api/modules/post.js +36 -10
- package/dist/api/modules/progressbar.d.ts +16 -5
- package/dist/api/modules/progressbar.js +34 -34
- package/dist/api/modules/responder.d.ts +95 -26
- package/dist/api/modules/responder.js +213 -172
- package/dist/api/modules/session.d.ts +10 -1
- package/dist/api/modules/session.js +15 -15
- package/dist/api/modules/startscreen.d.ts +0 -1
- package/dist/api/modules/startscreen.js +3 -6
- package/dist/api/modules/statistic.d.ts +2 -1
- package/dist/api/modules/statistic.js +4 -7
- package/dist/api/modules/worker.d.ts +2 -1
- package/dist/api/modules/worker.js +3 -3
- package/package.json +1 -1
- package/src/api/main.ts +1 -1
- package/src/api/modules/base.ts +75 -77
- package/src/api/modules/builder.ts +0 -10
- package/src/api/modules/checker.ts +31 -31
- package/src/api/modules/client.ts +144 -136
- package/src/api/modules/component.ts +11 -7
- package/src/api/modules/config.ts +8 -6
- package/src/api/modules/console.ts +25 -25
- package/src/api/modules/event.ts +6 -10
- package/src/api/modules/fuse.ts +1 -1
- package/src/api/modules/helpmenu.ts +4 -7
- package/src/api/modules/language.ts +6 -9
- package/src/api/modules/permission.ts +17 -20
- package/src/api/modules/plugin.ts +2 -2
- package/src/api/modules/post.ts +31 -10
- package/src/api/modules/progressbar.ts +34 -34
- package/src/api/modules/responder.ts +232 -181
- package/src/api/modules/session.ts +14 -14
- package/src/api/modules/startscreen.ts +3 -6
- package/src/api/modules/statistic.ts +4 -7
- package/src/api/modules/worker.ts +3 -3
|
@@ -56,12 +56,10 @@ export class ODPermission extends ODManagerData {
|
|
|
56
56
|
* Add new permissions using the `ODPermission` class in your plugin!
|
|
57
57
|
*/
|
|
58
58
|
export class ODPermissionManager extends ODManager {
|
|
59
|
-
/**Alias for Open Discord debugger. */
|
|
60
|
-
#debug;
|
|
61
59
|
/**The function for calculating permissions in this manager. */
|
|
62
|
-
|
|
60
|
+
calculation;
|
|
63
61
|
/**An alias to the Open Discord client manager. */
|
|
64
|
-
|
|
62
|
+
client;
|
|
65
63
|
/**The result which is returned when no other permissions match. (`member` by default) */
|
|
66
64
|
defaultResult = {
|
|
67
65
|
level: ODPermissionLevel["member"],
|
|
@@ -71,13 +69,12 @@ export class ODPermissionManager extends ODManager {
|
|
|
71
69
|
};
|
|
72
70
|
constructor(debug, client, useDefaultCalculation) {
|
|
73
71
|
super(debug, "permission");
|
|
74
|
-
this
|
|
75
|
-
this
|
|
76
|
-
this.#client = client;
|
|
72
|
+
this.calculation = useDefaultCalculation ? this.defaultCalculation : null;
|
|
73
|
+
this.client = client;
|
|
77
74
|
}
|
|
78
75
|
/**Edit the permission calculation function in this manager. */
|
|
79
76
|
setCalculation(calculation) {
|
|
80
|
-
this
|
|
77
|
+
this.calculation = calculation;
|
|
81
78
|
}
|
|
82
79
|
/**Edit the result which is returned when no other permissions match. (`member` by default) */
|
|
83
80
|
setDefaultResult(result) {
|
|
@@ -86,9 +83,9 @@ export class ODPermissionManager extends ODManager {
|
|
|
86
83
|
/**Get an `ODPermissionResult` based on a few context factors. Use `hasPermissions()` to simplify the result. */
|
|
87
84
|
getPermissions(user, channel, guild, settings) {
|
|
88
85
|
try {
|
|
89
|
-
if (!this
|
|
86
|
+
if (!this.calculation)
|
|
90
87
|
throw new ODSystemError("ODPermissionManager:getPermissions() => missing perms calculation");
|
|
91
|
-
return this
|
|
88
|
+
return this.calculation(user, channel, guild, settings);
|
|
92
89
|
}
|
|
93
90
|
catch (err) {
|
|
94
91
|
process.emit("uncaughtException", err);
|
|
@@ -113,16 +110,16 @@ export class ODPermissionManager extends ODManager {
|
|
|
113
110
|
throw new ODSystemError("Invalid minimum permission type at ODPermissionManager.hasPermissions()");
|
|
114
111
|
}
|
|
115
112
|
/**Check for permissions. (default calculation) */
|
|
116
|
-
async
|
|
117
|
-
const globalCalc = await this
|
|
118
|
-
const channelCalc = await this
|
|
113
|
+
async defaultCalculation(user, channel, guild, settings) {
|
|
114
|
+
const globalCalc = await this.defaultGlobalCalculation(user, channel, guild, settings);
|
|
115
|
+
const channelCalc = await this.defaultChannelCalculation(user, channel, guild, settings);
|
|
119
116
|
if (globalCalc.level > channelCalc.level)
|
|
120
117
|
return globalCalc;
|
|
121
118
|
else
|
|
122
119
|
return channelCalc;
|
|
123
120
|
}
|
|
124
121
|
/**Check for global permissions. Result will be compared with the channel perms in `#defaultCalculation()`. */
|
|
125
|
-
async
|
|
122
|
+
async defaultGlobalCalculation(user, channel, guild, settings) {
|
|
126
123
|
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null;
|
|
127
124
|
const allowGlobalUserScope = (settings && typeof settings.allowGlobalUserScope != "undefined") ? settings.allowGlobalUserScope : true;
|
|
128
125
|
const allowGlobalRoleScope = (settings && typeof settings.allowGlobalRoleScope != "undefined") ? settings.allowGlobalRoleScope : true;
|
|
@@ -152,7 +149,7 @@ export class ODPermissionManager extends ODManager {
|
|
|
152
149
|
//check for global role permissions
|
|
153
150
|
if (allowGlobalRoleScope) {
|
|
154
151
|
if (guild) {
|
|
155
|
-
const member = await this
|
|
152
|
+
const member = await this.client.fetchGuildMember(guild, user.id);
|
|
156
153
|
if (member) {
|
|
157
154
|
const memberRoles = member.roles.cache.map((role) => role.id);
|
|
158
155
|
const roles = this.getFiltered((permission) => (!idRegex || (idRegex && idRegex.test(permission.id.value))) && permission.scope == "global-role" && (permission.value instanceof discord.Role) && memberRoles.includes(permission.value.id) && permission.value.guild.id == guild.id);
|
|
@@ -182,7 +179,7 @@ export class ODPermissionManager extends ODManager {
|
|
|
182
179
|
return { ...this.defaultResult };
|
|
183
180
|
}
|
|
184
181
|
/**Check for channel permissions. Result will be compared with the global perms in `#defaultCalculation()`. */
|
|
185
|
-
async
|
|
182
|
+
async defaultChannelCalculation(user, channel, guild, settings) {
|
|
186
183
|
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null;
|
|
187
184
|
const allowChannelUserScope = (settings && typeof settings.allowChannelUserScope != "undefined") ? settings.allowChannelUserScope : true;
|
|
188
185
|
const allowChannelRoleScope = (settings && typeof settings.allowChannelRoleScope != "undefined") ? settings.allowChannelRoleScope : true;
|
|
@@ -212,7 +209,7 @@ export class ODPermissionManager extends ODManager {
|
|
|
212
209
|
}
|
|
213
210
|
//check for channel role permissions
|
|
214
211
|
if (allowChannelRoleScope) {
|
|
215
|
-
const member = await this
|
|
212
|
+
const member = await this.client.fetchGuildMember(guild, user.id);
|
|
216
213
|
if (member) {
|
|
217
214
|
const memberRoles = member.roles.cache.map((role) => role.id);
|
|
218
215
|
const roles = this.getFiltered((permission) => (!idRegex || (idRegex && idRegex.test(permission.id.value))) && permission.scope == "channel-role" && permission.channel && (permission.channel.id == channel.id) && (permission.value instanceof discord.Role) && memberRoles.includes(permission.value.id) && permission.value.guild.id == guild.id);
|
|
@@ -259,12 +256,12 @@ export class ODPermissionManager extends ODManager {
|
|
|
259
256
|
}
|
|
260
257
|
else {
|
|
261
258
|
if (!guild || !member) {
|
|
262
|
-
this
|
|
259
|
+
this.debug?.debug("ODPermissionManager.checkCommandPerms(): Permission Error, Not in server! (#1)");
|
|
263
260
|
return { hasPerms: false, reason: "not-in-server" };
|
|
264
261
|
}
|
|
265
|
-
const role = await this
|
|
262
|
+
const role = await this.client.fetchGuildRole(guild, permissionMode);
|
|
266
263
|
if (!role) {
|
|
267
|
-
this
|
|
264
|
+
this.debug?.debug("ODPermissionManager.checkCommandPerms(): Permission Error, Not in server! (#2)");
|
|
268
265
|
return { hasPerms: false, reason: "not-in-server" };
|
|
269
266
|
}
|
|
270
267
|
if (!role.members.has(member.id))
|
|
@@ -97,7 +97,6 @@ export interface ODPluginDetails {
|
|
|
97
97
|
* Don't re-execute plugins which are already enabled! It might break the bot or plugin.
|
|
98
98
|
*/
|
|
99
99
|
export declare class ODPlugin extends ODManagerData {
|
|
100
|
-
#private;
|
|
101
100
|
/**The name of the directory of this plugin. (same as id) */
|
|
102
101
|
dir: string;
|
|
103
102
|
/**All plugin data found in the `plugin.json` file. */
|
|
@@ -123,6 +122,8 @@ export declare class ODPlugin extends ODManagerData {
|
|
|
123
122
|
getStartFile(): string;
|
|
124
123
|
/**Execute this plugin. Returns `false` on crash. */
|
|
125
124
|
execute(debug: ODDebugger, force?: boolean): Promise<boolean>;
|
|
125
|
+
/**Check if a npm dependency exists. */
|
|
126
|
+
checkDependency(id: string): boolean;
|
|
126
127
|
/**Get a list of all missing npm dependencies that are required for this plugin. */
|
|
127
128
|
dependenciesInstalled(): string[];
|
|
128
129
|
/**Get a list of all missing plugins that are required for this plugin. */
|
|
@@ -109,7 +109,7 @@ export class ODPlugin extends ODManagerData {
|
|
|
109
109
|
return true;
|
|
110
110
|
}
|
|
111
111
|
/**Check if a npm dependency exists. */
|
|
112
|
-
|
|
112
|
+
checkDependency(id) {
|
|
113
113
|
try {
|
|
114
114
|
import.meta.resolve(id);
|
|
115
115
|
return true;
|
|
@@ -122,7 +122,7 @@ export class ODPlugin extends ODManagerData {
|
|
|
122
122
|
dependenciesInstalled() {
|
|
123
123
|
const missing = [];
|
|
124
124
|
this.data.npmDependencies.forEach((d) => {
|
|
125
|
-
if (!this
|
|
125
|
+
if (!this.checkDependency(d)) {
|
|
126
126
|
missing.push(d);
|
|
127
127
|
}
|
|
128
128
|
});
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ODManager, ODManagerData, ODNoGeneric, ODValidId } from "./base.js";
|
|
2
|
-
import { ODMessageBuildResult
|
|
2
|
+
import { ODMessageBuildResult } from "./builder.js";
|
|
3
3
|
import { ODDebugger } from "./console.js";
|
|
4
4
|
import * as discord from "discord.js";
|
|
5
|
+
import { ODResponderSendResult } from "./responder.js";
|
|
6
|
+
import { ODMessageComponentBuildResult } from "./component.js";
|
|
5
7
|
/**## ODPostManagerIdConstraint `type`
|
|
6
8
|
* The constraint/layout for id mappings/interfaces of the `ODPostManager` class.
|
|
7
9
|
*/
|
|
@@ -14,7 +16,8 @@ export type ODPostManagerIdConstraint = Record<string, ODPost<discord.GuildBased
|
|
|
14
16
|
* You can use this to get the logs channel of the bot (or some other static channel/category).
|
|
15
17
|
*/
|
|
16
18
|
export declare class ODPostManager<IdList extends ODPostManagerIdConstraint = ODPostManagerIdConstraint> extends ODManager<ODPost<discord.GuildBasedChannel>> {
|
|
17
|
-
|
|
19
|
+
/**A reference to the main server of the bot */
|
|
20
|
+
protected guild: discord.Guild | null;
|
|
18
21
|
constructor(debug: ODDebugger);
|
|
19
22
|
add(data: ODPost<discord.GuildBasedChannel>, overwrite?: boolean): boolean;
|
|
20
23
|
/**Initialize the post manager & all posts. */
|
|
@@ -35,7 +38,8 @@ export declare class ODPostManager<IdList extends ODPostManagerIdConstraint = OD
|
|
|
35
38
|
* This class also contains utilities for sending messages via the Open Discord builders.
|
|
36
39
|
*/
|
|
37
40
|
export declare class ODPost<ChannelType extends discord.GuildBasedChannel> extends ODManagerData {
|
|
38
|
-
|
|
41
|
+
/**A reference to the main server of the bot */
|
|
42
|
+
protected guild: discord.Guild | null;
|
|
39
43
|
/**Is this post already initialized? */
|
|
40
44
|
ready: boolean;
|
|
41
45
|
/**The discord.js channel */
|
|
@@ -50,5 +54,9 @@ export declare class ODPost<ChannelType extends discord.GuildBasedChannel> exten
|
|
|
50
54
|
/**Initialize the discord.js channel of this post. */
|
|
51
55
|
init(): Promise<null | undefined>;
|
|
52
56
|
/**Send a message to this channel using the Open Discord builder system */
|
|
53
|
-
send(
|
|
57
|
+
send(build: ODMessageBuildResult | ODMessageComponentBuildResult): Promise<ODResponderSendResult<true>>;
|
|
58
|
+
/**Get the final `messageCreateOptions` from a returned build result from builders/components. */
|
|
59
|
+
protected getMessageFromBuildResult(build: ODMessageBuildResult | ODMessageComponentBuildResult, type: "interaction" | "message"): discord.MessageCreateOptions & {
|
|
60
|
+
flags: number[];
|
|
61
|
+
};
|
|
54
62
|
}
|
package/dist/api/modules/post.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
//POST MODULE
|
|
3
3
|
///////////////////////////////////////
|
|
4
4
|
import { ODManager, ODManagerData } from "./base.js";
|
|
5
|
+
import * as discord from "discord.js";
|
|
5
6
|
/**## ODPostManager `class`
|
|
6
7
|
* This is an Open Discord post manager.
|
|
7
8
|
*
|
|
@@ -11,18 +12,18 @@ import { ODManager, ODManagerData } from "./base.js";
|
|
|
11
12
|
*/
|
|
12
13
|
export class ODPostManager extends ODManager {
|
|
13
14
|
/**A reference to the main server of the bot */
|
|
14
|
-
|
|
15
|
+
guild = null;
|
|
15
16
|
constructor(debug) {
|
|
16
17
|
super(debug, "post");
|
|
17
18
|
}
|
|
18
19
|
add(data, overwrite) {
|
|
19
|
-
if (this
|
|
20
|
-
data.useGuild(this
|
|
20
|
+
if (this.guild)
|
|
21
|
+
data.useGuild(this.guild);
|
|
21
22
|
return super.add(data, overwrite);
|
|
22
23
|
}
|
|
23
24
|
/**Initialize the post manager & all posts. */
|
|
24
25
|
async init(guild) {
|
|
25
|
-
this
|
|
26
|
+
this.guild = guild;
|
|
26
27
|
for (const post of this.getAll()) {
|
|
27
28
|
post.useGuild(guild);
|
|
28
29
|
await post.init();
|
|
@@ -48,7 +49,7 @@ export class ODPostManager extends ODManager {
|
|
|
48
49
|
*/
|
|
49
50
|
export class ODPost extends ODManagerData {
|
|
50
51
|
/**A reference to the main server of the bot */
|
|
51
|
-
|
|
52
|
+
guild = null;
|
|
52
53
|
/**Is this post already initialized? */
|
|
53
54
|
ready = false;
|
|
54
55
|
/**The discord.js channel */
|
|
@@ -61,7 +62,7 @@ export class ODPost extends ODManagerData {
|
|
|
61
62
|
}
|
|
62
63
|
/**Use a specific guild in this class for fetching the channel*/
|
|
63
64
|
useGuild(guild) {
|
|
64
|
-
this
|
|
65
|
+
this.guild = guild;
|
|
65
66
|
}
|
|
66
67
|
/**Change the channel id to another channel! */
|
|
67
68
|
setChannelId(id) {
|
|
@@ -71,10 +72,10 @@ export class ODPost extends ODManagerData {
|
|
|
71
72
|
async init() {
|
|
72
73
|
if (this.ready)
|
|
73
74
|
return;
|
|
74
|
-
if (!this
|
|
75
|
+
if (!this.guild)
|
|
75
76
|
return this.channel = null;
|
|
76
77
|
try {
|
|
77
|
-
this.channel = await this
|
|
78
|
+
this.channel = await this.guild.channels.fetch(this.channelId);
|
|
78
79
|
}
|
|
79
80
|
catch {
|
|
80
81
|
this.channel = null;
|
|
@@ -82,15 +83,40 @@ export class ODPost extends ODManagerData {
|
|
|
82
83
|
this.ready = true;
|
|
83
84
|
}
|
|
84
85
|
/**Send a message to this channel using the Open Discord builder system */
|
|
85
|
-
async send(
|
|
86
|
+
async send(build) {
|
|
86
87
|
if (!this.channel || !this.channel.isTextBased())
|
|
87
88
|
return { success: false, message: null };
|
|
88
89
|
try {
|
|
89
|
-
const
|
|
90
|
+
const finalMessage = this.getMessageFromBuildResult(build, "message");
|
|
91
|
+
const sent = await this.channel.send(finalMessage);
|
|
90
92
|
return { success: true, message: sent };
|
|
91
93
|
}
|
|
92
94
|
catch {
|
|
93
95
|
return { success: false, message: null };
|
|
94
96
|
}
|
|
95
97
|
}
|
|
98
|
+
/**Get the final `messageCreateOptions` from a returned build result from builders/components. */
|
|
99
|
+
getMessageFromBuildResult(build, type) {
|
|
100
|
+
const msgFlags = [];
|
|
101
|
+
let msgData;
|
|
102
|
+
if ('message' in build) {
|
|
103
|
+
//USING BUILDERS (deprecated)
|
|
104
|
+
msgData = build.message;
|
|
105
|
+
if (build.ephemeral)
|
|
106
|
+
msgFlags.push(discord.MessageFlags.Ephemeral);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
//USING COMPONENTS
|
|
110
|
+
msgData = build.msg;
|
|
111
|
+
if (type == "interaction" && build.ephemeral)
|
|
112
|
+
msgFlags.push(discord.MessageFlags.Ephemeral); //disabled with regular messages
|
|
113
|
+
if (build.componentsV2)
|
|
114
|
+
msgFlags.push(discord.MessageFlags.IsComponentsV2);
|
|
115
|
+
if (build.supressEmbeds)
|
|
116
|
+
msgFlags.push(discord.MessageFlags.SuppressEmbeds);
|
|
117
|
+
if (build.supressNotifications)
|
|
118
|
+
msgFlags.push(discord.MessageFlags.SuppressNotifications);
|
|
119
|
+
}
|
|
120
|
+
return Object.assign(msgData, { flags: msgFlags });
|
|
121
|
+
}
|
|
96
122
|
}
|
|
@@ -54,9 +54,9 @@ export type ODProgressBarRenderFunc<Settings extends {}> = (settings: Settings,
|
|
|
54
54
|
* There are already a lot of default options available if you just want an easy progress bar!
|
|
55
55
|
*/
|
|
56
56
|
export declare class ODProgressBarRenderer<Settings extends {}> extends ODManagerData {
|
|
57
|
-
#private;
|
|
58
57
|
settings: Settings;
|
|
59
|
-
|
|
58
|
+
private renderFunction;
|
|
59
|
+
constructor(id: ODValidId, renderFunction: ODProgressBarRenderFunc<Settings>, settings: Settings);
|
|
60
60
|
/**Render a progress bar using this renderer. */
|
|
61
61
|
render(min: number, max: number, value: number, prefix: string | null, suffix: string | null): string;
|
|
62
62
|
/**Create a clone of this progress bar renderer, but with additional settings. */
|
|
@@ -71,9 +71,12 @@ export declare class ODProgressBarRenderer<Settings extends {}> extends ODManage
|
|
|
71
71
|
* Use other classes as existing templates or create your own progress bar from scratch using this class.
|
|
72
72
|
*/
|
|
73
73
|
export declare abstract class ODProgressBar extends ODManagerData {
|
|
74
|
-
#private;
|
|
75
74
|
/**The renderer of this progress bar. */
|
|
76
75
|
renderer: ODProgressBarRenderer<{}>;
|
|
76
|
+
/**Is this progress bar currently active? */
|
|
77
|
+
protected active: boolean;
|
|
78
|
+
/**A list of listeners when the progress bar stops. */
|
|
79
|
+
protected stopListeners: Function[];
|
|
77
80
|
/**The current value of the progress bar. */
|
|
78
81
|
protected value: number;
|
|
79
82
|
/**The minimum value of the progress bar. */
|
|
@@ -89,6 +92,10 @@ export declare abstract class ODProgressBar extends ODManagerData {
|
|
|
89
92
|
/**Enable automatic stopping when reaching `min` or `max`. */
|
|
90
93
|
autoStop: null | "min" | "max";
|
|
91
94
|
constructor(id: ODValidId, renderer: ODProgressBarRenderer<{}>, min: number, max: number, value: number, autoStop: null | "min" | "max", prefix: string | null, suffix: string | null);
|
|
95
|
+
/**Parse a value in such a way that it doesn't go below/above the min/max limits. */
|
|
96
|
+
private parseValue;
|
|
97
|
+
/**Render progress bar to the console. */
|
|
98
|
+
private renderStdout;
|
|
92
99
|
/**Start showing this progress bar in the console. */
|
|
93
100
|
start(): boolean;
|
|
94
101
|
/**Update this progress bar while active. (will automatically update the progress bar in the console) */
|
|
@@ -103,12 +110,15 @@ export declare abstract class ODProgressBar extends ODManagerData {
|
|
|
103
110
|
* You can set a fixed duration (milliseconds) in the constructor.
|
|
104
111
|
*/
|
|
105
112
|
export declare class ODTimedProgressBar extends ODProgressBar {
|
|
106
|
-
#private;
|
|
107
113
|
/**The time in milliseconds. */
|
|
108
114
|
time: number;
|
|
109
115
|
/**The mode of the timer. */
|
|
110
116
|
mode: "increasing" | "decreasing";
|
|
111
117
|
constructor(id: ODValidId, renderer: ODProgressBarRenderer<{}>, time: number, mode: "increasing" | "decreasing", prefix: string | null, suffix: string | null);
|
|
118
|
+
/**The timer which is used. */
|
|
119
|
+
private timer;
|
|
120
|
+
/**Run the timed progress bar. */
|
|
121
|
+
private execute;
|
|
112
122
|
start(): boolean;
|
|
113
123
|
}
|
|
114
124
|
/**## ODManualProgressBar `class`
|
|
@@ -170,6 +180,7 @@ export interface ODDefaultProgressBarRendererSettings {
|
|
|
170
180
|
showBorder: boolean;
|
|
171
181
|
}
|
|
172
182
|
export declare class ODDefaultProgressBarRenderer extends ODProgressBarRenderer<ODDefaultProgressBarRendererSettings> {
|
|
173
|
-
#private;
|
|
174
183
|
constructor(id: ODValidId, settings: ODDefaultProgressBarRendererSettings);
|
|
184
|
+
/**Switch between Ansis functions based on the specified color. */
|
|
185
|
+
private switchColorAnsis;
|
|
175
186
|
}
|
|
@@ -58,16 +58,16 @@ export class ODProgressBarManager extends ODManager {
|
|
|
58
58
|
*/
|
|
59
59
|
export class ODProgressBarRenderer extends ODManagerData {
|
|
60
60
|
settings;
|
|
61
|
-
|
|
62
|
-
constructor(id,
|
|
61
|
+
renderFunction;
|
|
62
|
+
constructor(id, renderFunction, settings) {
|
|
63
63
|
super(id);
|
|
64
|
-
this
|
|
64
|
+
this.renderFunction = renderFunction;
|
|
65
65
|
this.settings = settings;
|
|
66
66
|
}
|
|
67
67
|
/**Render a progress bar using this renderer. */
|
|
68
68
|
render(min, max, value, prefix, suffix) {
|
|
69
69
|
try {
|
|
70
|
-
return this
|
|
70
|
+
return this.renderFunction(this.settings, min, max, value, prefix, suffix);
|
|
71
71
|
}
|
|
72
72
|
catch (err) {
|
|
73
73
|
process.emit("uncaughtException", err);
|
|
@@ -81,7 +81,7 @@ export class ODProgressBarRenderer extends ODManagerData {
|
|
|
81
81
|
if (typeof settings[key] != "undefined")
|
|
82
82
|
newSettings[key] = settings[key];
|
|
83
83
|
}
|
|
84
|
-
return new ODProgressBarRenderer(this.id, this
|
|
84
|
+
return new ODProgressBarRenderer(this.id, this.renderFunction, newSettings);
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
/**## ODProgressBar `class`
|
|
@@ -96,9 +96,9 @@ export class ODProgressBar extends ODManagerData {
|
|
|
96
96
|
/**The renderer of this progress bar. */
|
|
97
97
|
renderer;
|
|
98
98
|
/**Is this progress bar currently active? */
|
|
99
|
-
|
|
99
|
+
active = false;
|
|
100
100
|
/**A list of listeners when the progress bar stops. */
|
|
101
|
-
|
|
101
|
+
stopListeners = [];
|
|
102
102
|
/**The current value of the progress bar. */
|
|
103
103
|
value;
|
|
104
104
|
/**The minimum value of the progress bar. */
|
|
@@ -118,14 +118,14 @@ export class ODProgressBar extends ODManagerData {
|
|
|
118
118
|
this.renderer = renderer;
|
|
119
119
|
this.min = min;
|
|
120
120
|
this.max = max;
|
|
121
|
-
this.initialValue = this
|
|
122
|
-
this.value = this
|
|
121
|
+
this.initialValue = this.parseValue(value);
|
|
122
|
+
this.value = this.parseValue(value);
|
|
123
123
|
this.autoStop = autoStop;
|
|
124
124
|
this.prefix = prefix;
|
|
125
125
|
this.suffix = suffix;
|
|
126
126
|
}
|
|
127
127
|
/**Parse a value in such a way that it doesn't go below/above the min/max limits. */
|
|
128
|
-
|
|
128
|
+
parseValue(value) {
|
|
129
129
|
if (value > this.max)
|
|
130
130
|
return this.max;
|
|
131
131
|
else if (value < this.min)
|
|
@@ -134,8 +134,8 @@ export class ODProgressBar extends ODManagerData {
|
|
|
134
134
|
return value;
|
|
135
135
|
}
|
|
136
136
|
/**Render progress bar to the console. */
|
|
137
|
-
|
|
138
|
-
if (!this
|
|
137
|
+
renderStdout() {
|
|
138
|
+
if (!this.active)
|
|
139
139
|
return;
|
|
140
140
|
readline.clearLine(process.stdout, 0);
|
|
141
141
|
readline.cursorTo(process.stdout, 0);
|
|
@@ -143,31 +143,31 @@ export class ODProgressBar extends ODManagerData {
|
|
|
143
143
|
}
|
|
144
144
|
/**Start showing this progress bar in the console. */
|
|
145
145
|
start() {
|
|
146
|
-
if (this
|
|
146
|
+
if (this.active)
|
|
147
147
|
return false;
|
|
148
|
-
this.value = this
|
|
149
|
-
this
|
|
150
|
-
this
|
|
148
|
+
this.value = this.parseValue(this.initialValue);
|
|
149
|
+
this.active = true;
|
|
150
|
+
this.renderStdout();
|
|
151
151
|
return true;
|
|
152
152
|
}
|
|
153
153
|
/**Update this progress bar while active. (will automatically update the progress bar in the console) */
|
|
154
154
|
update(value, stop) {
|
|
155
|
-
if (!this
|
|
155
|
+
if (!this.active)
|
|
156
156
|
return false;
|
|
157
|
-
this.value = this
|
|
158
|
-
this
|
|
157
|
+
this.value = this.parseValue(value);
|
|
158
|
+
this.renderStdout();
|
|
159
159
|
if (stop || (this.autoStop == "max" && this.value == this.max) || (this.autoStop == "min" && this.value == this.min)) {
|
|
160
160
|
process.stdout.write("\n");
|
|
161
|
-
this
|
|
162
|
-
this
|
|
163
|
-
this
|
|
161
|
+
this.active = false;
|
|
162
|
+
this.stopListeners.forEach((cb) => cb());
|
|
163
|
+
this.stopListeners = [];
|
|
164
164
|
}
|
|
165
165
|
return true;
|
|
166
166
|
}
|
|
167
167
|
/**Wait for the progress bar to finish. */
|
|
168
168
|
finished() {
|
|
169
169
|
return new Promise((resolve) => {
|
|
170
|
-
this
|
|
170
|
+
this.stopListeners.push(resolve);
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
173
|
}
|
|
@@ -188,7 +188,7 @@ export class ODTimedProgressBar extends ODProgressBar {
|
|
|
188
188
|
this.mode = mode;
|
|
189
189
|
}
|
|
190
190
|
/**The timer which is used. */
|
|
191
|
-
async
|
|
191
|
+
async timer(ms) {
|
|
192
192
|
return new Promise((resolve) => {
|
|
193
193
|
setTimeout(() => {
|
|
194
194
|
resolve();
|
|
@@ -196,11 +196,11 @@ export class ODTimedProgressBar extends ODProgressBar {
|
|
|
196
196
|
});
|
|
197
197
|
}
|
|
198
198
|
/**Run the timed progress bar. */
|
|
199
|
-
async
|
|
199
|
+
async execute() {
|
|
200
200
|
let i = 0;
|
|
201
201
|
const fragment = this.time / 100;
|
|
202
202
|
while (i < 100) {
|
|
203
|
-
await this
|
|
203
|
+
await this.timer(fragment);
|
|
204
204
|
i++;
|
|
205
205
|
super.update((this.mode == "increasing") ? (i * fragment) : this.time - (i * fragment));
|
|
206
206
|
}
|
|
@@ -209,7 +209,7 @@ export class ODTimedProgressBar extends ODProgressBar {
|
|
|
209
209
|
const res = super.start();
|
|
210
210
|
if (!res)
|
|
211
211
|
return false;
|
|
212
|
-
this
|
|
212
|
+
this.execute();
|
|
213
213
|
return true;
|
|
214
214
|
}
|
|
215
215
|
}
|
|
@@ -245,12 +245,12 @@ export class ODDefaultProgressBarRenderer extends ODProgressBarRenderer {
|
|
|
245
245
|
super(id, (settings, min, max, value, rawPrefix, rawSuffix) => {
|
|
246
246
|
const percentage = (value - min) / (max - min);
|
|
247
247
|
const barLevel = Math.round(percentage * settings.barWidth);
|
|
248
|
-
const borderAnsis = this
|
|
249
|
-
const filledBarAnsis = this
|
|
250
|
-
const emptyBarAnsis = this
|
|
251
|
-
const labelAnsis = this
|
|
252
|
-
const prefixAnsis = this
|
|
253
|
-
const suffixAnsis = this
|
|
248
|
+
const borderAnsis = this.switchColorAnsis(settings.borderColor);
|
|
249
|
+
const filledBarAnsis = this.switchColorAnsis(settings.filledBarColor);
|
|
250
|
+
const emptyBarAnsis = this.switchColorAnsis(settings.emptyBarColor);
|
|
251
|
+
const labelAnsis = this.switchColorAnsis(settings.labelColor);
|
|
252
|
+
const prefixAnsis = this.switchColorAnsis(settings.prefixColor);
|
|
253
|
+
const suffixAnsis = this.switchColorAnsis(settings.suffixColor);
|
|
254
254
|
const leftBorder = (settings.showBorder) ? borderAnsis(settings.leftBorderChar) : "";
|
|
255
255
|
const rightBorder = (settings.showBorder) ? borderAnsis(settings.rightBorderChar) : "";
|
|
256
256
|
const bar = (settings.showBar) ? filledBarAnsis(settings.filledBarChar.repeat(barLevel)) + emptyBarAnsis(settings.emptyBarChar.repeat(settings.barWidth - barLevel)) : "";
|
|
@@ -276,7 +276,7 @@ export class ODDefaultProgressBarRenderer extends ODProgressBarRenderer {
|
|
|
276
276
|
}, settings);
|
|
277
277
|
}
|
|
278
278
|
/**Switch between Ansis functions based on the specified color. */
|
|
279
|
-
|
|
279
|
+
switchColorAnsis(c) {
|
|
280
280
|
return (c === "openticket") ? ansis.hex("#f8ba00") : (c === "openmoderation") ? ansis.hex("#1690ff") : ansis[c];
|
|
281
281
|
}
|
|
282
282
|
}
|