Added new API comments (part 2)
This commit is contained in:
@@ -143,6 +143,14 @@ export class ODLanguageManager extends ODManager<ODLanguage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**## ODLanguage `class`
|
||||||
|
* This is an open ticket language file.
|
||||||
|
*
|
||||||
|
* It contains metadata and all translation strings available in this language.
|
||||||
|
* Register this class to an `ODLanguageManager` to use it!
|
||||||
|
*
|
||||||
|
* JSON languages should be created using the `ODJsonLanguage` class instead!
|
||||||
|
*/
|
||||||
export class ODLanguage extends ODManagerData {
|
export class ODLanguage extends ODManagerData {
|
||||||
/**The name of the file with extension. */
|
/**The name of the file with extension. */
|
||||||
file: string = ""
|
file: string = ""
|
||||||
@@ -164,6 +172,14 @@ export class ODLanguage extends ODManagerData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**## ODJsonLanguage `class`
|
||||||
|
* This is an open ticket JSON language file.
|
||||||
|
*
|
||||||
|
* It contains metadata and all translation strings from a certain JSON file (in `./languages/`).
|
||||||
|
* Register this class to an `ODLanguageManager` to use it!
|
||||||
|
*
|
||||||
|
* Use the `ODLanguage` class to use translations from non-JSON files!
|
||||||
|
*/
|
||||||
export class ODJsonLanguage extends ODLanguage {
|
export class ODJsonLanguage extends ODLanguage {
|
||||||
constructor(id:ODValidId, file:string, customPath?:string){
|
constructor(id:ODValidId, file:string, customPath?:string){
|
||||||
super(id,{})
|
super(id,{})
|
||||||
|
|||||||
@@ -5,28 +5,65 @@ import { ODId, ODValidId, ODManager, ODSystemError, ODManagerData } from "./base
|
|||||||
import * as discord from "discord.js"
|
import * as discord from "discord.js"
|
||||||
import { ODDebugger } from "./console"
|
import { ODDebugger } from "./console"
|
||||||
|
|
||||||
|
/**## ODPermissionType `type`
|
||||||
|
* All available permission types/levels. Can be used in the `ODPermission` class.
|
||||||
|
*/
|
||||||
export type ODPermissionType = "member"|"support"|"moderator"|"admin"|"owner"|"developer"
|
export type ODPermissionType = "member"|"support"|"moderator"|"admin"|"owner"|"developer"
|
||||||
|
|
||||||
|
/**## ODPermissionScope `type`
|
||||||
|
* The scope in which a certain permission is active.
|
||||||
|
*/
|
||||||
export type ODPermissionScope = "global-user"|"channel-user"|"global-role"|"channel-role"
|
export type ODPermissionScope = "global-user"|"channel-user"|"global-role"|"channel-role"
|
||||||
|
|
||||||
|
/**## ODPermissionResult `interface`
|
||||||
|
* The result returned by `ODPermissionManager.getPermissions()`.
|
||||||
|
*/
|
||||||
export interface ODPermissionResult {
|
export interface ODPermissionResult {
|
||||||
|
/**The permission type. */
|
||||||
type:ODPermissionType
|
type:ODPermissionType
|
||||||
|
/**The permission scope. */
|
||||||
scope:ODPermissionScope|"default"
|
scope:ODPermissionScope|"default"
|
||||||
|
/**The highest level available for this scope. */
|
||||||
level:ODPermissionLevel,
|
level:ODPermissionLevel,
|
||||||
|
/**The permission which returned this level. */
|
||||||
source:ODPermission|null
|
source:ODPermission|null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**## ODPermissionLevel `enum`
|
||||||
|
* All available permission types/levels. But as `enum` instead of `type`. Used to calculate the level.
|
||||||
|
*/
|
||||||
export enum ODPermissionLevel {
|
export enum ODPermissionLevel {
|
||||||
|
/**A normal member. (Default for everyone) */
|
||||||
member,
|
member,
|
||||||
|
/**Support team. Higher than a normal member. (Used for ticket-admins) */
|
||||||
support,
|
support,
|
||||||
|
/**Moderator. Higher than the support team. (Unused) */
|
||||||
moderator,
|
moderator,
|
||||||
|
/**Admin. Higher than a moderator. (Used for global-admins) */
|
||||||
admin,
|
admin,
|
||||||
|
/**Server owner. (Able to use all commands including `/stats reset`) */
|
||||||
owner,
|
owner,
|
||||||
|
/**Bot owner or all users from dev team. (Able to use all commands including `/stats reset`) */
|
||||||
developer
|
developer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**## ODPermission `class`
|
||||||
|
* This is an open ticket permission.
|
||||||
|
*
|
||||||
|
* It defines a single permission level for a specific scope (global/channel & user/role)
|
||||||
|
* These permissions only apply to commands & interactions.
|
||||||
|
* They are not related to channel permissions in the ticket system.
|
||||||
|
*
|
||||||
|
* Register this class to an `ODPermissionManager` to use it!
|
||||||
|
*/
|
||||||
export class ODPermission extends ODManagerData {
|
export class ODPermission extends ODManagerData {
|
||||||
|
/**The scope of this permission. */
|
||||||
readonly scope: ODPermissionScope
|
readonly scope: ODPermissionScope
|
||||||
|
/**The type/level of this permission. */
|
||||||
readonly permission: ODPermissionType
|
readonly permission: ODPermissionType
|
||||||
|
/**The user/role of this permission. */
|
||||||
readonly value: discord.Role|discord.User
|
readonly value: discord.Role|discord.User
|
||||||
|
/**The channel that this permission applies to. (`null` when global) */
|
||||||
readonly channel: discord.Channel|null
|
readonly channel: discord.Channel|null
|
||||||
|
|
||||||
constructor(id:ODValidId, scope:"global-user", permission:ODPermissionType, value:discord.User)
|
constructor(id:ODValidId, scope:"global-user", permission:ODPermissionType, value:discord.User)
|
||||||
@@ -42,18 +79,39 @@ export class ODPermission extends ODManagerData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**## ODPermissionSettings `interface`
|
||||||
|
* Optional settings for the `getPermissions()` method in the `ODPermissionManager`.
|
||||||
|
*/
|
||||||
export interface ODPermissionSettings {
|
export interface ODPermissionSettings {
|
||||||
|
/**Include permissions from the global user scope. */
|
||||||
allowGlobalUserScope?:boolean,
|
allowGlobalUserScope?:boolean,
|
||||||
|
/**Include permissions from the global role scope. */
|
||||||
allowGlobalRoleScope?:boolean,
|
allowGlobalRoleScope?:boolean,
|
||||||
|
/**Include permissions from the channel user scope. */
|
||||||
allowChannelUserScope?:boolean,
|
allowChannelUserScope?:boolean,
|
||||||
|
/**Include permissions from the channel role scope. */
|
||||||
allowChannelRoleScope?:boolean,
|
allowChannelRoleScope?:boolean,
|
||||||
|
/**Only include permissions of which the id matches this regex. */
|
||||||
idRegex?:RegExp
|
idRegex?:RegExp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**## ODPermissionCalculationCallback `type`
|
||||||
|
* The callback of the permission calculation. (Used in `ODPermissionManager`)
|
||||||
|
*/
|
||||||
export type ODPermissionCalculationCallback = (user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null) => Promise<ODPermissionResult>
|
export type ODPermissionCalculationCallback = (user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null) => Promise<ODPermissionResult>
|
||||||
|
|
||||||
|
/**## ODPermissionManager `class`
|
||||||
|
* This is an open ticket permission manager.
|
||||||
|
*
|
||||||
|
* It manages all permissions in the bot!
|
||||||
|
* Use the `getPermissions()` and `hasPermissions()` methods to get user perms.
|
||||||
|
*
|
||||||
|
* Add new permissions using the `ODPermission` class in your plugin!
|
||||||
|
*/
|
||||||
export class ODPermissionManager extends ODManager<ODPermission> {
|
export class ODPermissionManager extends ODManager<ODPermission> {
|
||||||
|
/**The function for calculating permissions in this manager. */
|
||||||
#calculation: ODPermissionCalculationCallback|null
|
#calculation: ODPermissionCalculationCallback|null
|
||||||
|
/**The result which is returned when no other permissions match. (`member` by default) */
|
||||||
defaultResult: ODPermissionResult = {
|
defaultResult: ODPermissionResult = {
|
||||||
level:ODPermissionLevel["member"],
|
level:ODPermissionLevel["member"],
|
||||||
scope:"default",
|
scope:"default",
|
||||||
@@ -66,12 +124,15 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
|||||||
this.#calculation = useDefaultCalculation ? this.#defaultCalculation : null
|
this.#calculation = useDefaultCalculation ? this.#defaultCalculation : null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**Edit the permission calculation function in this manager. */
|
||||||
setCalculation(calculation:ODPermissionCalculationCallback){
|
setCalculation(calculation:ODPermissionCalculationCallback){
|
||||||
this.#calculation = calculation
|
this.#calculation = calculation
|
||||||
}
|
}
|
||||||
|
/**Edit the result which is returned when no other permissions match. (`member` by default) */
|
||||||
setDefaultResult(result:ODPermissionResult){
|
setDefaultResult(result:ODPermissionResult){
|
||||||
this.defaultResult = result
|
this.defaultResult = result
|
||||||
}
|
}
|
||||||
|
/**Get an `ODPermissionResult` based on a few context factors. Use `hasPermissions()` to simplify the result. */
|
||||||
getPermissions(user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
getPermissions(user:discord.User, channel?:discord.Channel|null, guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||||
try{
|
try{
|
||||||
if (!this.#calculation) throw new ODSystemError("ODPermissionManager:getPermissions() => missing perms calculation")
|
if (!this.#calculation) throw new ODSystemError("ODPermissionManager:getPermissions() => missing perms calculation")
|
||||||
@@ -81,6 +142,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
|||||||
throw new ODSystemError("ODPermissionManager:getPermissions() => failed perms calculation")
|
throw new ODSystemError("ODPermissionManager:getPermissions() => failed perms calculation")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**Simplifies the `ODPermissionResult` returned from `getPermissions()` and returns a boolean to check if the user matches the required permissions. */
|
||||||
hasPermissions(minimum:ODPermissionType, data:ODPermissionResult){
|
hasPermissions(minimum:ODPermissionType, data:ODPermissionResult){
|
||||||
if (minimum == "member") return true
|
if (minimum == "member") return true
|
||||||
else if (minimum == "support") return (data.level >= ODPermissionLevel["support"])
|
else if (minimum == "support") return (data.level >= ODPermissionLevel["support"])
|
||||||
@@ -90,6 +152,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
|||||||
else if (minimum == "developer") return (data.level >= ODPermissionLevel["developer"])
|
else if (minimum == "developer") return (data.level >= ODPermissionLevel["developer"])
|
||||||
else throw new ODSystemError("Invalid minimum permission type at ODPermissionManager.hasPermissions()")
|
else throw new ODSystemError("Invalid minimum permission type at ODPermissionManager.hasPermissions()")
|
||||||
}
|
}
|
||||||
|
/**Check for permissions. (default calculation) */
|
||||||
async #defaultCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
async #defaultCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||||
const globalCalc = await this.#defaultGlobalCalculation(user,channel,guild,settings)
|
const globalCalc = await this.#defaultGlobalCalculation(user,channel,guild,settings)
|
||||||
const channelCalc = await this.#defaultChannelCalculation(user,channel,guild,settings)
|
const channelCalc = await this.#defaultChannelCalculation(user,channel,guild,settings)
|
||||||
@@ -97,7 +160,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
|||||||
if (globalCalc.level > channelCalc.level) return globalCalc
|
if (globalCalc.level > channelCalc.level) return globalCalc
|
||||||
else return channelCalc
|
else return channelCalc
|
||||||
}
|
}
|
||||||
/**Check for global permissions. Then this result can be compared with the channel one. */
|
/**Check for global permissions. Result will be compared with the channel perms in `#defaultCalculation()`. */
|
||||||
async #defaultGlobalCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
async #defaultGlobalCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||||
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
||||||
const allowGlobalUserScope = (settings && typeof settings.allowGlobalUserScope != "undefined") ? settings.allowGlobalUserScope : true
|
const allowGlobalUserScope = (settings && typeof settings.allowGlobalUserScope != "undefined") ? settings.allowGlobalUserScope : true
|
||||||
@@ -160,7 +223,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
|||||||
//spread result to prevent accidental referencing
|
//spread result to prevent accidental referencing
|
||||||
return {...this.defaultResult}
|
return {...this.defaultResult}
|
||||||
}
|
}
|
||||||
/**Check for channel permissions. Then this result can be compared with the global one. */
|
/**Check for channel permissions. Result will be compared with the global perms in `#defaultCalculation()`. */
|
||||||
async #defaultChannelCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
async #defaultChannelCalculation(user:discord.User,channel?:discord.Channel|null,guild?:discord.Guild|null, settings?:ODPermissionSettings|null): Promise<ODPermissionResult> {
|
||||||
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
const idRegex = (settings && typeof settings.idRegex != "undefined") ? settings.idRegex : null
|
||||||
const allowChannelUserScope = (settings && typeof settings.allowChannelUserScope != "undefined") ? settings.allowChannelUserScope : true
|
const allowChannelUserScope = (settings && typeof settings.allowChannelUserScope != "undefined") ? settings.allowChannelUserScope : true
|
||||||
@@ -220,7 +283,7 @@ export class ODPermissionManager extends ODManager<ODPermission> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//spread result to prevent accidental referencing
|
//spread result to prevent accidental modification because of referencing
|
||||||
return {...this.defaultResult}
|
return {...this.defaultResult}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user