@open-discord-bots/framework 0.2.17 → 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/index.d.ts +16 -15
- package/dist/api/index.js +16 -15
- package/dist/api/main.d.ts +31 -23
- package/dist/api/main.js +3 -1
- package/dist/api/modules/action.d.ts +2 -2
- package/dist/api/modules/action.js +1 -5
- package/dist/api/modules/base.d.ts +29 -11
- package/dist/api/modules/base.js +78 -80
- package/dist/api/modules/builder.d.ts +2 -11
- package/dist/api/modules/builder.js +0 -4
- package/dist/api/modules/checker.d.ts +28 -7
- package/dist/api/modules/checker.js +33 -37
- package/dist/api/modules/client.d.ts +66 -14
- package/dist/api/modules/client.js +146 -132
- package/dist/api/modules/component.d.ts +928 -0
- package/dist/api/modules/component.js +1346 -0
- package/dist/api/modules/config.d.ts +30 -2
- package/dist/api/modules/config.js +90 -7
- package/dist/api/modules/console.d.ts +16 -4
- package/dist/api/modules/console.js +25 -25
- package/dist/api/modules/cooldown.d.ts +5 -5
- package/dist/api/modules/cooldown.js +1 -17
- package/dist/api/modules/database.d.ts +21 -13
- package/dist/api/modules/database.js +0 -23
- 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 +11 -9
- package/dist/api/modules/helpmenu.js +24 -22
- package/dist/api/modules/language.d.ts +4 -3
- package/dist/api/modules/language.js +9 -16
- 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 +18 -6
- package/dist/api/modules/progressbar.js +35 -35
- package/dist/api/modules/responder.d.ts +97 -28
- package/dist/api/modules/responder.js +213 -176
- package/dist/api/modules/session.d.ts +11 -2
- package/dist/api/modules/session.js +16 -16
- package/dist/api/modules/startscreen.d.ts +2 -3
- package/dist/api/modules/startscreen.js +8 -9
- 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 +3 -2
- package/src/api/index.ts +16 -15
- package/src/api/main.ts +33 -24
- package/src/api/modules/action.ts +2 -4
- package/src/api/modules/base.ts +77 -79
- package/src/api/modules/builder.ts +2 -14
- package/src/api/modules/checker.ts +36 -37
- package/src/api/modules/client.ts +144 -136
- package/src/api/modules/component.ts +1826 -0
- package/src/api/modules/config.ts +86 -7
- package/src/api/modules/console.ts +25 -25
- package/src/api/modules/cooldown.ts +8 -13
- package/src/api/modules/database.ts +24 -32
- package/src/api/modules/event.ts +6 -10
- package/src/api/modules/fuse.ts +1 -1
- package/src/api/modules/helpmenu.ts +31 -27
- package/src/api/modules/language.ts +11 -16
- 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 +36 -37
- package/src/api/modules/responder.ts +234 -185
- package/src/api/modules/session.ts +15 -15
- package/src/api/modules/startscreen.ts +9 -10
- package/src/api/modules/statistic.ts +4 -7
- package/src/api/modules/worker.ts +3 -3
- package/src/api/modules/component.txt +0 -350
|
@@ -129,12 +129,10 @@ export type ODPermissionManagerIdConstraint = Record<string,ODPermission>
|
|
|
129
129
|
* Add new permissions using the `ODPermission` class in your plugin!
|
|
130
130
|
*/
|
|
131
131
|
export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint = ODPermissionManagerIdConstraint> extends ODManager<ODPermission> {
|
|
132
|
-
/**Alias for Open Discord debugger. */
|
|
133
|
-
#debug: ODDebugger
|
|
134
132
|
/**The function for calculating permissions in this manager. */
|
|
135
|
-
|
|
133
|
+
private calculation: ODPermissionCalculationCallback|null
|
|
136
134
|
/**An alias to the Open Discord client manager. */
|
|
137
|
-
|
|
135
|
+
private client: ODClientManager
|
|
138
136
|
/**The result which is returned when no other permissions match. (`member` by default) */
|
|
139
137
|
defaultResult: ODPermissionResult = {
|
|
140
138
|
level:ODPermissionLevel["member"],
|
|
@@ -145,14 +143,13 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
145
143
|
|
|
146
144
|
constructor(debug:ODDebugger, client:ODClientManager, useDefaultCalculation?:boolean){
|
|
147
145
|
super(debug,"permission")
|
|
148
|
-
this
|
|
149
|
-
this
|
|
150
|
-
this.#client = client
|
|
146
|
+
this.calculation = useDefaultCalculation ? this.defaultCalculation : null
|
|
147
|
+
this.client = client
|
|
151
148
|
}
|
|
152
149
|
|
|
153
150
|
/**Edit the permission calculation function in this manager. */
|
|
154
151
|
setCalculation(calculation:ODPermissionCalculationCallback){
|
|
155
|
-
this
|
|
152
|
+
this.calculation = calculation
|
|
156
153
|
}
|
|
157
154
|
/**Edit the result which is returned when no other permissions match. (`member` by default) */
|
|
158
155
|
setDefaultResult(result:ODPermissionResult){
|
|
@@ -161,8 +158,8 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
161
158
|
/**Get an `ODPermissionResult` based on a few context factors. Use `hasPermissions()` to simplify the result. */
|
|
162
159
|
getPermissions(user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
|
163
160
|
try{
|
|
164
|
-
if (!this
|
|
165
|
-
return this
|
|
161
|
+
if (!this.calculation) throw new ODSystemError("ODPermissionManager:getPermissions() => missing perms calculation")
|
|
162
|
+
return this.calculation(user,channel,guild,settings)
|
|
166
163
|
}catch(err){
|
|
167
164
|
process.emit("uncaughtException",err)
|
|
168
165
|
throw new ODSystemError("ODPermissionManager:getPermissions() => failed perms calculation")
|
|
@@ -179,15 +176,15 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
179
176
|
else throw new ODSystemError("Invalid minimum permission type at ODPermissionManager.hasPermissions()")
|
|
180
177
|
}
|
|
181
178
|
/**Check for permissions. (default calculation) */
|
|
182
|
-
async
|
|
183
|
-
const globalCalc = await this
|
|
184
|
-
const channelCalc = await this
|
|
179
|
+
private async defaultCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
|
180
|
+
const globalCalc = await this.defaultGlobalCalculation(user,channel,guild,settings)
|
|
181
|
+
const channelCalc = await this.defaultChannelCalculation(user,channel,guild,settings)
|
|
185
182
|
|
|
186
183
|
if (globalCalc.level > channelCalc.level) return globalCalc
|
|
187
184
|
else return channelCalc
|
|
188
185
|
}
|
|
189
186
|
/**Check for global permissions. Result will be compared with the channel perms in `#defaultCalculation()`. */
|
|
190
|
-
async
|
|
187
|
+
private async defaultGlobalCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
|
191
188
|
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
|
192
189
|
const allowGlobalUserScope = (settings && typeof settings.allowGlobalUserScope != "undefined") ? settings.allowGlobalUserScope : true
|
|
193
190
|
const allowGlobalRoleScope = (settings && typeof settings.allowGlobalRoleScope != "undefined") ? settings.allowGlobalRoleScope : true
|
|
@@ -219,7 +216,7 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
219
216
|
//check for global role permissions
|
|
220
217
|
if (allowGlobalRoleScope){
|
|
221
218
|
if (guild){
|
|
222
|
-
const member = await this
|
|
219
|
+
const member = await this.client.fetchGuildMember(guild,user.id)
|
|
223
220
|
if (member){
|
|
224
221
|
const memberRoles = member.roles.cache.map((role) => role.id)
|
|
225
222
|
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)
|
|
@@ -250,7 +247,7 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
250
247
|
return {...this.defaultResult}
|
|
251
248
|
}
|
|
252
249
|
/**Check for channel permissions. Result will be compared with the global perms in `#defaultCalculation()`. */
|
|
253
|
-
async
|
|
250
|
+
private async defaultChannelCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
|
254
251
|
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
|
255
252
|
const allowChannelUserScope = (settings && typeof settings.allowChannelUserScope != "undefined") ? settings.allowChannelUserScope : true
|
|
256
253
|
const allowChannelRoleScope = (settings && typeof settings.allowChannelRoleScope != "undefined") ? settings.allowChannelRoleScope : true
|
|
@@ -282,7 +279,7 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
282
279
|
|
|
283
280
|
//check for channel role permissions
|
|
284
281
|
if (allowChannelRoleScope){
|
|
285
|
-
const member = await this
|
|
282
|
+
const member = await this.client.fetchGuildMember(guild,user.id)
|
|
286
283
|
if (member){
|
|
287
284
|
const memberRoles = member.roles.cache.map((role) => role.id)
|
|
288
285
|
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)
|
|
@@ -328,12 +325,12 @@ export class ODPermissionManager<IdList extends ODPermissionManagerIdConstraint
|
|
|
328
325
|
else return {hasPerms:true,isAdmin}
|
|
329
326
|
}else{
|
|
330
327
|
if (!guild || !member){
|
|
331
|
-
this
|
|
328
|
+
this.debug?.debug("ODPermissionManager.checkCommandPerms(): Permission Error, Not in server! (#1)")
|
|
332
329
|
return {hasPerms:false,reason:"not-in-server"}
|
|
333
330
|
}
|
|
334
|
-
const role = await this
|
|
331
|
+
const role = await this.client.fetchGuildRole(guild,permissionMode)
|
|
335
332
|
if (!role){
|
|
336
|
-
this
|
|
333
|
+
this.debug?.debug("ODPermissionManager.checkCommandPerms(): Permission Error, Not in server! (#2)")
|
|
337
334
|
return {hasPerms:false,reason:"not-in-server"}
|
|
338
335
|
}
|
|
339
336
|
if (!role.members.has(member.id)) return {hasPerms:false,reason:"no-perms"}
|
|
@@ -201,7 +201,7 @@ export class ODPlugin extends ODManagerData {
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
/**Check if a npm dependency exists. */
|
|
204
|
-
|
|
204
|
+
checkDependency(id:string){
|
|
205
205
|
try{
|
|
206
206
|
import.meta.resolve(id)
|
|
207
207
|
return true
|
|
@@ -214,7 +214,7 @@ export class ODPlugin extends ODManagerData {
|
|
|
214
214
|
dependenciesInstalled(){
|
|
215
215
|
const missing: string[] = []
|
|
216
216
|
this.data.npmDependencies.forEach((d) => {
|
|
217
|
-
if (!this
|
|
217
|
+
if (!this.checkDependency(d)){
|
|
218
218
|
missing.push(d)
|
|
219
219
|
}
|
|
220
220
|
})
|
package/src/api/modules/post.ts
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
//POST MODULE
|
|
3
3
|
///////////////////////////////////////
|
|
4
4
|
import { ODId, ODManager, ODManagerData, ODNoGeneric, ODValidId } from "./base.js"
|
|
5
|
-
import { ODMessageBuildResult
|
|
5
|
+
import { ODMessageBuildResult } from "./builder.js"
|
|
6
6
|
import { ODDebugger } from "./console.js"
|
|
7
7
|
import * as discord from "discord.js"
|
|
8
|
+
import { ODResponderSendResult } from "./responder.js"
|
|
9
|
+
import { ODMessageComponentBuildResult } from "./component.js"
|
|
8
10
|
|
|
9
11
|
/**## ODPostManagerIdConstraint `type`
|
|
10
12
|
* The constraint/layout for id mappings/interfaces of the `ODPostManager` class.
|
|
@@ -20,19 +22,19 @@ export type ODPostManagerIdConstraint = Record<string,ODPost<discord.GuildBasedC
|
|
|
20
22
|
*/
|
|
21
23
|
export class ODPostManager<IdList extends ODPostManagerIdConstraint = ODPostManagerIdConstraint> extends ODManager<ODPost<discord.GuildBasedChannel>> {
|
|
22
24
|
/**A reference to the main server of the bot */
|
|
23
|
-
|
|
25
|
+
protected guild: discord.Guild|null = null
|
|
24
26
|
|
|
25
27
|
constructor(debug:ODDebugger){
|
|
26
28
|
super(debug,"post")
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
add(data:ODPost<discord.GuildBasedChannel>, overwrite?:boolean): boolean {
|
|
30
|
-
if (this
|
|
32
|
+
if (this.guild) data.useGuild(this.guild)
|
|
31
33
|
return super.add(data,overwrite)
|
|
32
34
|
}
|
|
33
35
|
/**Initialize the post manager & all posts. */
|
|
34
36
|
async init(guild:discord.Guild){
|
|
35
|
-
this
|
|
37
|
+
this.guild = guild
|
|
36
38
|
for (const post of this.getAll()){
|
|
37
39
|
post.useGuild(guild)
|
|
38
40
|
await post.init()
|
|
@@ -71,7 +73,7 @@ export class ODPostManager<IdList extends ODPostManagerIdConstraint = ODPostMana
|
|
|
71
73
|
*/
|
|
72
74
|
export class ODPost<ChannelType extends discord.GuildBasedChannel> extends ODManagerData {
|
|
73
75
|
/**A reference to the main server of the bot */
|
|
74
|
-
|
|
76
|
+
protected guild: discord.Guild|null = null
|
|
75
77
|
/**Is this post already initialized? */
|
|
76
78
|
ready: boolean = false
|
|
77
79
|
/**The discord.js channel */
|
|
@@ -86,7 +88,7 @@ export class ODPost<ChannelType extends discord.GuildBasedChannel> extends ODMan
|
|
|
86
88
|
|
|
87
89
|
/**Use a specific guild in this class for fetching the channel*/
|
|
88
90
|
useGuild(guild:discord.Guild|null){
|
|
89
|
-
this
|
|
91
|
+
this.guild = guild
|
|
90
92
|
}
|
|
91
93
|
/**Change the channel id to another channel! */
|
|
92
94
|
setChannelId(id:string){
|
|
@@ -95,22 +97,41 @@ export class ODPost<ChannelType extends discord.GuildBasedChannel> extends ODMan
|
|
|
95
97
|
/**Initialize the discord.js channel of this post. */
|
|
96
98
|
async init(){
|
|
97
99
|
if (this.ready) return
|
|
98
|
-
if (!this
|
|
100
|
+
if (!this.guild) return this.channel = null
|
|
99
101
|
try{
|
|
100
|
-
this.channel = await this
|
|
102
|
+
this.channel = await this.guild.channels.fetch(this.channelId) as ChannelType
|
|
101
103
|
}catch{
|
|
102
104
|
this.channel = null
|
|
103
105
|
}
|
|
104
106
|
this.ready = true
|
|
105
107
|
}
|
|
106
108
|
/**Send a message to this channel using the Open Discord builder system */
|
|
107
|
-
async send(
|
|
109
|
+
async send(build:ODMessageBuildResult|ODMessageComponentBuildResult): Promise<ODResponderSendResult<true>> {
|
|
108
110
|
if (!this.channel || !this.channel.isTextBased()) return {success:false,message:null}
|
|
109
111
|
try{
|
|
110
|
-
const
|
|
112
|
+
const finalMessage = this.getMessageFromBuildResult(build,"message")
|
|
113
|
+
const sent = await this.channel.send(finalMessage)
|
|
111
114
|
return {success:true,message:sent}
|
|
112
115
|
}catch{
|
|
113
116
|
return {success:false,message:null}
|
|
114
117
|
}
|
|
115
118
|
}
|
|
119
|
+
/**Get the final `messageCreateOptions` from a returned build result from builders/components. */
|
|
120
|
+
protected getMessageFromBuildResult(build:ODMessageBuildResult|ODMessageComponentBuildResult,type:"interaction"|"message"){
|
|
121
|
+
const msgFlags: number[] = []
|
|
122
|
+
let msgData: discord.MessageCreateOptions
|
|
123
|
+
if ('message' in build){
|
|
124
|
+
//USING BUILDERS (deprecated)
|
|
125
|
+
msgData = build.message
|
|
126
|
+
if (build.ephemeral) msgFlags.push(discord.MessageFlags.Ephemeral)
|
|
127
|
+
}else{
|
|
128
|
+
//USING COMPONENTS
|
|
129
|
+
msgData = build.msg
|
|
130
|
+
if (type == "interaction" && build.ephemeral) msgFlags.push(discord.MessageFlags.Ephemeral) //disabled with regular messages
|
|
131
|
+
if (build.componentsV2) msgFlags.push(discord.MessageFlags.IsComponentsV2)
|
|
132
|
+
if (build.supressEmbeds) msgFlags.push(discord.MessageFlags.SuppressEmbeds)
|
|
133
|
+
if (build.supressNotifications) msgFlags.push(discord.MessageFlags.SuppressNotifications)
|
|
134
|
+
}
|
|
135
|
+
return Object.assign(msgData,{flags:msgFlags})
|
|
136
|
+
}
|
|
116
137
|
}
|
|
@@ -102,32 +102,31 @@ export type ODProgressBarRenderFunc<Settings extends {}> = (settings:Settings,mi
|
|
|
102
102
|
*/
|
|
103
103
|
export class ODProgressBarRenderer<Settings extends {}> extends ODManagerData {
|
|
104
104
|
settings: Settings
|
|
105
|
-
|
|
105
|
+
private renderFunction: ODProgressBarRenderFunc<Settings>
|
|
106
106
|
|
|
107
|
-
constructor(id:ODValidId,
|
|
107
|
+
constructor(id:ODValidId,renderFunction:ODProgressBarRenderFunc<Settings>,settings:Settings){
|
|
108
108
|
super(id)
|
|
109
|
-
this
|
|
109
|
+
this.renderFunction = renderFunction
|
|
110
110
|
this.settings = settings
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
/**Render a progress bar using this renderer. */
|
|
114
114
|
render(min:number,max:number,value:number,prefix:string|null,suffix:string|null){
|
|
115
115
|
try {
|
|
116
|
-
return this
|
|
116
|
+
return this.renderFunction(this.settings,min,max,value,prefix,suffix)
|
|
117
117
|
}catch(err){
|
|
118
118
|
process.emit("uncaughtException",err)
|
|
119
119
|
return "<PROGRESS-BAR-ERROR>"
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
|
|
122
|
+
/**Create a clone of this progress bar renderer, but with additional settings. */
|
|
123
123
|
withAdditionalSettings(settings:Partial<Settings>): ODProgressBarRenderer<Settings> {
|
|
124
124
|
const newSettings: Settings = {...this.settings}
|
|
125
125
|
for (const key of Object.keys(settings) as (keyof Partial<Settings>)[]){
|
|
126
126
|
if (typeof settings[key] != "undefined") newSettings[key] = settings[key]
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
return new ODProgressBarRenderer(this.id,this.#render,newSettings)
|
|
129
|
+
return new ODProgressBarRenderer(this.id,this.renderFunction,newSettings)
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
132
|
|
|
@@ -139,13 +138,13 @@ export class ODProgressBarRenderer<Settings extends {}> extends ODManagerData {
|
|
|
139
138
|
*
|
|
140
139
|
* Use other classes as existing templates or create your own progress bar from scratch using this class.
|
|
141
140
|
*/
|
|
142
|
-
export class ODProgressBar extends ODManagerData {
|
|
141
|
+
export abstract class ODProgressBar extends ODManagerData {
|
|
143
142
|
/**The renderer of this progress bar. */
|
|
144
143
|
renderer: ODProgressBarRenderer<{}>
|
|
145
144
|
/**Is this progress bar currently active? */
|
|
146
|
-
|
|
145
|
+
protected active: boolean = false
|
|
147
146
|
/**A list of listeners when the progress bar stops. */
|
|
148
|
-
|
|
147
|
+
protected stopListeners: Function[] = []
|
|
149
148
|
/**The current value of the progress bar. */
|
|
150
149
|
protected value: number
|
|
151
150
|
/**The minimum value of the progress bar. */
|
|
@@ -167,50 +166,50 @@ export class ODProgressBar extends ODManagerData {
|
|
|
167
166
|
this.renderer = renderer
|
|
168
167
|
this.min = min
|
|
169
168
|
this.max = max
|
|
170
|
-
this.initialValue = this
|
|
171
|
-
this.value = this
|
|
169
|
+
this.initialValue = this.parseValue(value)
|
|
170
|
+
this.value = this.parseValue(value)
|
|
172
171
|
this.autoStop = autoStop
|
|
173
172
|
this.prefix = prefix
|
|
174
173
|
this.suffix = suffix
|
|
175
174
|
}
|
|
176
175
|
/**Parse a value in such a way that it doesn't go below/above the min/max limits. */
|
|
177
|
-
|
|
176
|
+
private parseValue(value:number){
|
|
178
177
|
if (value > this.max) return this.max
|
|
179
178
|
else if (value < this.min) return this.min
|
|
180
179
|
else return value
|
|
181
180
|
}
|
|
182
181
|
/**Render progress bar to the console. */
|
|
183
|
-
|
|
184
|
-
if (!this
|
|
182
|
+
private renderStdout(){
|
|
183
|
+
if (!this.active) return
|
|
185
184
|
readline.clearLine(process.stdout,0)
|
|
186
185
|
readline.cursorTo(process.stdout,0)
|
|
187
186
|
process.stdout.write(this.renderer.render(this.min,this.max,this.value,this.prefix,this.suffix))
|
|
188
187
|
}
|
|
189
188
|
/**Start showing this progress bar in the console. */
|
|
190
189
|
start(): boolean {
|
|
191
|
-
if (this
|
|
192
|
-
this.value = this
|
|
193
|
-
this
|
|
194
|
-
this
|
|
190
|
+
if (this.active) return false
|
|
191
|
+
this.value = this.parseValue(this.initialValue)
|
|
192
|
+
this.active = true
|
|
193
|
+
this.renderStdout()
|
|
195
194
|
return true
|
|
196
195
|
}
|
|
197
196
|
/**Update this progress bar while active. (will automatically update the progress bar in the console) */
|
|
198
197
|
protected update(value:number,stop?:boolean): boolean {
|
|
199
|
-
if (!this
|
|
200
|
-
this.value = this
|
|
201
|
-
this
|
|
198
|
+
if (!this.active) return false
|
|
199
|
+
this.value = this.parseValue(value)
|
|
200
|
+
this.renderStdout()
|
|
202
201
|
if (stop || (this.autoStop == "max" && this.value == this.max) || (this.autoStop == "min" && this.value == this.min)){
|
|
203
202
|
process.stdout.write("\n")
|
|
204
|
-
this
|
|
205
|
-
this
|
|
206
|
-
this
|
|
203
|
+
this.active = false
|
|
204
|
+
this.stopListeners.forEach((cb) => cb())
|
|
205
|
+
this.stopListeners = []
|
|
207
206
|
}
|
|
208
207
|
return true
|
|
209
208
|
}
|
|
210
209
|
/**Wait for the progress bar to finish. */
|
|
211
210
|
finished(): Promise<void> {
|
|
212
211
|
return new Promise((resolve) => {
|
|
213
|
-
this
|
|
212
|
+
this.stopListeners.push(resolve)
|
|
214
213
|
})
|
|
215
214
|
}
|
|
216
215
|
}
|
|
@@ -234,7 +233,7 @@ export class ODTimedProgressBar extends ODProgressBar {
|
|
|
234
233
|
}
|
|
235
234
|
|
|
236
235
|
/**The timer which is used. */
|
|
237
|
-
async
|
|
236
|
+
private async timer(ms:number): Promise<void> {
|
|
238
237
|
return new Promise((resolve) => {
|
|
239
238
|
setTimeout(() => {
|
|
240
239
|
resolve()
|
|
@@ -242,11 +241,11 @@ export class ODTimedProgressBar extends ODProgressBar {
|
|
|
242
241
|
})
|
|
243
242
|
}
|
|
244
243
|
/**Run the timed progress bar. */
|
|
245
|
-
async
|
|
244
|
+
private async execute(){
|
|
246
245
|
let i = 0
|
|
247
246
|
const fragment = this.time/100
|
|
248
247
|
while (i < 100){
|
|
249
|
-
await this
|
|
248
|
+
await this.timer(fragment)
|
|
250
249
|
i++
|
|
251
250
|
super.update((this.mode == "increasing") ? (i*fragment) : this.time-(i*fragment))
|
|
252
251
|
}
|
|
@@ -254,7 +253,7 @@ export class ODTimedProgressBar extends ODProgressBar {
|
|
|
254
253
|
start(){
|
|
255
254
|
const res = super.start()
|
|
256
255
|
if (!res) return false
|
|
257
|
-
this
|
|
256
|
+
this.execute()
|
|
258
257
|
return true
|
|
259
258
|
}
|
|
260
259
|
}
|
|
@@ -338,12 +337,12 @@ export class ODDefaultProgressBarRenderer extends ODProgressBarRenderer<ODDefaul
|
|
|
338
337
|
const percentage = (value-min)/(max-min)
|
|
339
338
|
const barLevel = Math.round(percentage*settings.barWidth)
|
|
340
339
|
|
|
341
|
-
const borderAnsis = this
|
|
342
|
-
const filledBarAnsis = this
|
|
343
|
-
const emptyBarAnsis = this
|
|
344
|
-
const labelAnsis = this
|
|
345
|
-
const prefixAnsis = this
|
|
346
|
-
const suffixAnsis = this
|
|
340
|
+
const borderAnsis = this.switchColorAnsis(settings.borderColor)
|
|
341
|
+
const filledBarAnsis = this.switchColorAnsis(settings.filledBarColor)
|
|
342
|
+
const emptyBarAnsis = this.switchColorAnsis(settings.emptyBarColor)
|
|
343
|
+
const labelAnsis = this.switchColorAnsis(settings.labelColor)
|
|
344
|
+
const prefixAnsis = this.switchColorAnsis(settings.prefixColor)
|
|
345
|
+
const suffixAnsis = this.switchColorAnsis(settings.suffixColor)
|
|
347
346
|
|
|
348
347
|
const leftBorder = (settings.showBorder) ? borderAnsis(settings.leftBorderChar) : ""
|
|
349
348
|
const rightBorder = (settings.showBorder) ? borderAnsis(settings.rightBorderChar) : ""
|
|
@@ -365,7 +364,7 @@ export class ODDefaultProgressBarRenderer extends ODProgressBarRenderer<ODDefaul
|
|
|
365
364
|
}
|
|
366
365
|
|
|
367
366
|
/**Switch between Ansis functions based on the specified color. */
|
|
368
|
-
|
|
367
|
+
private switchColorAnsis(c:ODValidConsoleColor|"openticket"|"openmoderation"){
|
|
369
368
|
return (c === "openticket") ? ansis.hex("#f8ba00") : (c === "openmoderation") ? ansis.hex("#1690ff") : ansis[c]
|
|
370
369
|
}
|
|
371
370
|
}
|